C# F#类未实现接口功能
我是F#的新手,正在尝试。我正在尝试实现一个F#接口 这是我的F#文件: 当在C#中使用它并按F12时,给我这个C# F#类未实现接口功能,c#,f#,C#,F#,我是F#的新手,正在尝试。我正在尝试实现一个F#接口 这是我的F#文件: 当在C#中使用它并按F12时,给我这个 [CompilationMapping(SourceConstructFlags.ObjectType)] public class AuthMathematics : IAuthMathematics { public AuthMathematics(int a, int b); public int A { get; } public int B { g
[CompilationMapping(SourceConstructFlags.ObjectType)]
public class AuthMathematics : IAuthMathematics
{
public AuthMathematics(int a, int b);
public int A { get; }
public int B { get; }
}
[CompilationMapping(SourceConstructFlags.ObjectType)]
public interface IAuthMathematics
{
int Sum();
}
我的求和函数和属性初始化在哪里?当您从C#点击F12时(我假设这是Visual Studio,对吧?),它不会显示源代码(显然,因为源代码是F#),而是使用元数据来重建用C#编写的代码的外观。当它这样做的时候,它只显示public
和protected
东西,因为这些东西是你唯一可以使用的
同时,F#中的接口实现总是编译为,也称为“private”,因此它们不会出现在元数据视图中
当然,属性初始值设定项是构造函数主体的一部分,因此自然也不会显示它们
作为参考,您的F#实现在C#中的外观如下:
您可以创建一个F#类,它看起来像一个具有隐式接口成员实现的C#类。由于F#中没有隐式实现,因此必须定义公共成员并显式实现接口。结果是:
namespace Services.Auth.Domain
type IAuthMathematics =
abstract Sum : unit -> int
type AuthMathematics(a : int, b : int) =
member this.A = a
member this.B = b
member this.Sum() = this.A + this.B
interface IAuthMathematics with
member this.Sum() = this.Sum()
这是非常有用的,因为它允许您直接使用
Sum()
方法和AuthMathematics
引用,而不必强制转换到IAuthMathematics
尝试使用不同的反编译器来反编译F#Assembly不确定F#,但是,您的F#代码是否能够实现Sum方法的显式接口?如果我没记错的话,当用F12显示项目本身没有声明的类型的类声明时,它没有列出显式接口实现的方法。。。
public class AuthMathematics : IAuthMathematics
{
public AuthMathematics(int a, int b) {
A = a;
B = b;
}
public int A { get; private set; }
public int B { get; private set; }
int IAuthMathematics.Sum() { return A + B; }
}
namespace Services.Auth.Domain
type IAuthMathematics =
abstract Sum : unit -> int
type AuthMathematics(a : int, b : int) =
member this.A = a
member this.B = b
member this.Sum() = this.A + this.B
interface IAuthMathematics with
member this.Sum() = this.Sum()