我有以下的C#类:
public class Foo {
protected virtual void Bar (){
}
}
public class Baz : Foo {
protected override void Bar (){
}
}
如果我审视它们,方法Bar总是在那里:
[<EntryPoint>]
let main args =
let methodFromFoo = typedefof<Foo>.GetMethod("Bar", BindingFlags.Instance ||| BindingFlags.NonPublic)
if (methodFromFoo <> null) then
Console.WriteLine ("methodFromFoo is not null")
else
Console.WriteLine ("methodFromFoo is null")
let methodFromBaz = typedefof<Baz>.GetMethod("Bar", BindingFlags.Instance ||| BindingFlags.NonPublic)
if (methodFromBaz <> null) then
Console.WriteLine ("methodFromBaz is not null")
else
Console.WriteLine ("methodFromBaz is null")
结果是对于两种情况都是不是null
。
但是,当我使用F#类进行相同操作时,结果是不同的:
type FSharpBaz() =
inherit Foo()
override this.Bar () =
Console.WriteLine ("yeh")
[<EntryPoint>]
let main args =
let methodFromFsharp = typedefof<FSharpBaz>.GetMethod("Bar", BindingFlags.Instance ||| BindingFlags.NonPublic)
if (methodFromFsharp <> null) then
Console.WriteLine ("methodFromFsharp is not null")
else
Console.WriteLine ("methodFromFsharp is null")
但为什么?如果我的类是使用F#创建的,我不能通过反射获得方法吗?我感到非常沮丧。