kno*_*cte 4 c# reflection f# system.reflection c#-to-f#
我有以下C#类:
public class Foo {
protected virtual void Bar (){
}
}
public class Baz : Foo {
protected override void Bar (){
}
}
Run Code Online (Sandbox Code Playgroud)
如果我反省它们,方法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")
Run Code Online (Sandbox Code Playgroud)
结果是这is not 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")
Run Code Online (Sandbox Code Playgroud)
但为什么???如果我的课程是用F#制作的,我不能通过反射获得方法吗?我很沮丧.