2009-01-25 80 views
54

我正在玩F#和C#,并想从C#调用F#代码。从C调用F#代码

我设法让它在Visual Studio中以另一种方式工作,方法是在同一个解决方案中有两个项目,并将C#代码的引用添加到F#项目。做完这些之后,我可以调用C#代码,甚至在调试时一步步完成。

我想要做的是F#代码从C#而不是C#代码从F#。我在F#项目中添加了一个对C#项目的引用,但它不像之前那样工作。我想知道这是否可能,而无需手动进行。

+3

除非有特定的问题,增加从C#一个今天“只是工作的F#项目的引用”。这里没有什么特别的,因为这是.NET架构(语言不可知论,MSIL等)的基本承诺或好处之一。事实上,相反会很奇怪。你对这个赏金还有什么期望? – 2018-03-09 07:19:23

回答

43

(编辑:我最初链接到这些文件外部,但我的SVN主机不再允许匿名访问。因此,代码现在内联在这回答r)

下面是从C#调用F#的一个工作示例。

正如您所遇到的,我无法通过从“添加引用...项目”选项卡中进行选择来添加引用。相反,我必须通过在“添加引用...浏览”选项卡中浏览到F#程序集来手动执行此操作。

------ 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 

----- C#单元测试用于模块-----

// 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); 
     } 

    } 
} 
+0

谢谢。 “我必须通过浏览到'添加引用...浏览'选项卡中的F#程序集手动执行此操作。”是什么为我工作。 – ZeroKelvin 2009-01-26 00:52:01

2

this link他们似乎有一些可能的解决方案,但似乎最简单的是this comment之一:

F#代码:

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#代码:

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

我在val行上得到一个错误:val f3:FCallback - > int - > int - > int“错误定义中意外的关键字'val'在此点或其他标记之前或之前预期的不完整结构化构造。 – 2011-11-14 20:50:29

18

它不应该'只是工作“,尽管在从C#作品引用项目之前可能必须构建F#项目(我忘记了)。

问题的一个常见来源是名称空间/模块。如果你的F#代码不是以一个名字空间声明开始的,它将被放入一个与文件名同名的模块中,例如,从C#开始,你的类型可能会显示为“Program.Foo”而不是“Foo”(如果Foo是Program.fs中定义的F#类型)。

+1

感谢您提供有关模块名称的信息:)。 – ZeroKelvin 2009-01-26 01:18:22

2

// 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);