如何从C#调用F#类型扩展(静态成员函数)

FSharp代码结构如下(我不控制源代码)。

namespace FS [] type TestType() = static member IrrelevantFunction() = 0 [] module Extensions = type TestType with //How do we call this from C# static member NeedToCallThis() = 0 module Caller = let CallIt() = //F# can call it TestType.NeedToCallThis() 

C#调用代码如下

 public void Caller() { TestType.IrrelevantFunction(); //We want to call this //TestType.NeedToCallThis(); //Metadata: //namespace FS //{ // [Microsoft.FSharp.Core.AutoOpenAttribute] // [Microsoft.FSharp.Core.CompilationMappingAttribute] // public static class Extensions // { // public static int TestType.NeedToCallThis.Static(); // } //} //None of these compile //TestType.NeedToCallThis(); //Extensions.TestType.NeedToCallThis.Static(); //Extensions.TestType.NeedToCallThis(); } 

我不相信可以直接从C#调用该方法而不使用reflection,因为编译的方法名称不是C#中的有效方法名称。

使用reflection,您可以通过以下方式调用它:

 var result = typeof(FS.Extensions).GetMethod("TestType.NeedToCallThis.Static").Invoke(null,null);