在F#中,我们可以创建一个这样的函数:
let ``add x and y`` x y = x + y
我可以像这样称呼它:
``add x and y`` 1 2
有没有办法从C#端调用上面的函数?我甚至无法在对象浏览器中看到它。
答案 0 :(得分:17)
您可以使用valid属性将任何CompiledName F#函数名称公开为C#作为任何C#有效函数名称:
namespace Library1
module Test =
[<CompiledName("Whatever")>]
let ``add a and b`` x y = x + y
然后在C#中:
using Library1;
...............
System.Console.WriteLine(Test.Whatever(2,2));
来自NickL的评论FOLLOW-UP 03/05/2016 ,至少适用于F#3.1:
从功能转移到成员会带来一些“ifs and buts”。
首先,CompiledName
属性如果从纯member
使用,则不会使用namespace
进行编译。仅仅编译需要在module
内使用。
在F#记录的module
和装饰方法 member
中使用时,无论两个刻度之间的内容如何显示,它都能正常工作。但是,当装饰F#record member
的属性 CompiledName
时,只有当双刻度之间的内容类似于某些合法值名称时,才能看到交叉汇编:
module M
type MyRecord =
{ myField: string }
[<CompiledName "Whatever">]
member x.``Blah Blah blah``() = x.myField
[<CompiledName "Another">]
member x.``ABC`` = x.myField
然后从C#开始,以下工作正常:
var recInC = new M.MyRecord("Testing...");
Console.WriteLine(recInC.Whatever());
Console.WriteLine(recInC.Another);
此类不一致提示潜在的issues。
答案 1 :(得分:6)
反思可能是唯一的方法,但使用它并不一定难看。只需将它全部包装在一个类中即可进行反射。
public static class MyModuleWrapper
{
// it would be easier to create a delegate once and reuse it
private static Lazy<Func<int, int, int>> addXAndY = new Lazy<Func<int, int, int>>(() =>
(Func<int, int, int>)Delegate.CreateDelegate(typeof(Func<int, int, int>), typeof(MyModule).GetMethod("add x and y"))
);
public static int AddXAndY(int x, int y)
{
return addXAndY.Value(x, y);
}
// pass other methods through.
public static int OtherMethod(int x, int y)
{
return MyModule.OtherMethod(x, y);
}
}
然后像平常一样使用它。
var sum = MyModuleWrapper.AddXAndY(1, 2);
var otherValue = MyModuleWrapper.OtherMethod(1, 2); // use the wrapper instead
我不确定需要更改什么或者如果涉及多态类型,但希望您能够理解并可以应用必要的更改。