Llame al código F # desde C #


80

Estoy jugando con F # y C #, y me gustaría llamar al código F # desde C #.

Logré que funcionara al revés en Visual Studio al tener dos proyectos en la misma solución y agregar una referencia del código C # al proyecto F #. Después de hacer esto, pude llamar al código C # e incluso recorrerlo mientras depuraba.

Lo que estoy tratando de hacer es el código F # de C # en lugar del código C # de F #. Agregué una referencia al proyecto de F # al proyecto de C #, pero no está funcionando como lo hacía antes. Me gustaría saber si esto es posible sin hacerlo manualmente.


9
A menos que tenga problemas específicos, agregar una referencia a un proyecto de F # desde un C # hoy "simplemente funciona". No hay nada extraordinario aquí, ya que esa es una de las promesas o beneficios fundamentales de la arquitectura .NET (independiente del lenguaje, MSIL, etc.) De hecho, lo contrario sería extraño. ¿Qué más esperas por esta recompensa?
Simon Mourier

Respuestas:


57

A continuación se muestra un ejemplo práctico de cómo llamar a F # desde C #.

Como se encontró, no pude agregar una referencia seleccionando en la pestaña "Agregar referencia ... Proyectos". En su lugar, tuve que hacerlo manualmente, navegando hasta el ensamblado F # en la pestaña "Agregar referencia ... Examinar".

------ MÓDULO F # -----

// First implement a foldl function, with the signature (a->b->a) -> a -> [b] -> a
// Now use your foldl function to implement a map function, with the signature (a->b) -> [a] -> [b]
// Finally use your map function to convert an array of strings to upper case
//
// Test cases are in TestFoldMapUCase.cs
//
// Note: F# provides standard implementations of the fold and map operations, but the 
// exercise here is to build them up from primitive elements...

module FoldMapUCase.Zumbro
#light


let AlwaysTwo =
   2

let rec foldl fn seed vals = 
   match vals with
   | head :: tail -> foldl fn (fn seed head) tail
   | _ -> seed


let map fn vals =
   let gn lst x =
      fn( x ) :: lst
   List.rev (foldl gn [] vals)


let ucase vals =
   map String.uppercase vals

----- PRUEBAS DE LA UNIDAD C # PARA EL MÓDULO -----

// Test cases for FoldMapUCase.fs
//
// For this example, I have written my NUnit test cases in C#.  This requires constructing some F#
// types in order to invoke the F# functions under test.


using System;
using Microsoft.FSharp.Core;
using Microsoft.FSharp.Collections;
using NUnit.Framework;

namespace FoldMapUCase
{
    [TestFixture]
    public class TestFoldMapUCase
    {
        public TestFoldMapUCase()
        {            
        }

        [Test]
        public void CheckAlwaysTwo()
        {
            // simple example to show how to access F# function from C#
            int n = Zumbro.AlwaysTwo;
            Assert.AreEqual(2, n);
        }

        class Helper<T>
        {
            public static List<T> mkList(params T[] ar)
            {
                List<T> foo = List<T>.Nil;
                for (int n = ar.Length - 1; n >= 0; n--)
                    foo = List<T>.Cons(ar[n], foo);
                return foo;
            }
        }


        [Test]
        public void foldl1()
        {
            int seed = 64;
            List<int> values = Helper<int>.mkList( 4, 2, 4 );
            FastFunc<int, FastFunc<int,int>> fn =
                FuncConvert.ToFastFunc( (Converter<int,int,int>) delegate( int a, int b ) { return a/b; } );

            int result = Zumbro.foldl<int, int>( fn, seed, values);
            Assert.AreEqual(2, result);
        }

        [Test]
        public void foldl0()
        {
            string seed = "hi mom";
            List<string> values = Helper<string>.mkList();
            FastFunc<string, FastFunc<string, string>> fn =
                FuncConvert.ToFastFunc((Converter<string, string, string>)delegate(string a, string b) { throw new Exception("should never be invoked"); });

            string result = Zumbro.foldl<string, string>(fn, seed, values);
            Assert.AreEqual(seed, result);
        }

        [Test]
        public void map()
        {
            FastFunc<int, int> fn =
                FuncConvert.ToFastFunc((Converter<int, int>)delegate(int a) { return a*a; });

            List<int> vals = Helper<int>.mkList(1, 2, 3);
            List<int> res = Zumbro.map<int, int>(fn, vals);

            Assert.AreEqual(res.Length, 3);
            Assert.AreEqual(1, res.Head);
            Assert.AreEqual(4, res.Tail.Head);
            Assert.AreEqual(9, res.Tail.Tail.Head);
        }

        [Test]
        public void ucase()
        {
            List<string> vals = Helper<string>.mkList("arnold", "BOB", "crAIg");
            List<string> exp = Helper<string>.mkList( "ARNOLD", "BOB", "CRAIG" );
            List<string> res = Zumbro.ucase(vals);
            Assert.AreEqual(exp.Length, res.Length);
            Assert.AreEqual(exp.Head, res.Head);
            Assert.AreEqual(exp.Tail.Head, res.Tail.Head);
            Assert.AreEqual(exp.Tail.Tail.Head, res.Tail.Tail.Head);
        }

    }
}

1
Thank you. "I did have to do it manually, by browsing to the F# assembly in the 'Add Reference ... Browse' tab." is what worked for me.
ZeroKelvin

27

It should 'just work', though you might have to build the F# project before a project-to-project reference from C# works (I forget).

A common source of issues is namespaces/modules. If your F# code does not start with a namespace declaration, it gets put in a module with the same name as the filename, so that e.g. from C# your type might appear as "Program.Foo" rather than just "Foo" (if Foo is an F# type defined in Program.fs).


2
Thank you for the information regarding the module names :) .
ZeroKelvin

2
Yeah I need to blog that one, it causes a lot of confusion.
Brian

An additional issue is triggered, when the Fsharp project (generator of the dll reference) is in the same solution with the Csharp (consumer project)
George Kargakis

6

From this link they seem to have a number of possible solutions, but the one that seemed the simplest was a comment:

F# Code:

type FCallback = delegate of int*int -> int;;
type FCallback =
  delegate of int * int -> int

let f3 (f:FCallback) a b = f.Invoke(a,b);;
val f3 : FCallback -> int -> int -> int

C# Code:

int a = Module1.f3(Module1.f2, 10, 20); // method gets converted to the delegate automatically in C#

I get an error on the val line : val f3 : FCallback -> int -> int -> int "Error 1 Unexpected keyword 'val' in definition. Expected incomplete structured construct at or before this point or other token."
Tom Stickel

4

// Test.fs :

module meGlobal

type meList() = 
    member this.quicksort = function
        | [] -> []  //  if list is empty return list
        | first::rest -> 
            let smaller,larger = List.partition((>=) first) rest
        List.concat[this.quicksort smaller; [first]; this.quicksort larger]

// Test.cs :

List<int> A = new List<int> { 13, 23, 7, 2 };
meGlobal.meList S = new meGlobal.meList();

var cquicksort = Microsoft.FSharp.Core.FSharpFunc<FSharpList<IComparable>,     FSharpList<IComparable>>.ToConverter(S.quicksort);

FSharpList<IComparable> FI = ListModule.OfSeq(A.Cast<IComparable>());
var R = cquicksort(FI);
Al usar nuestro sitio, usted reconoce que ha leído y comprende nuestra Política de Cookies y Política de Privacidad.
Licensed under cc by-sa 3.0 with attribution required.