C#GetMethod不返回父类的方法 [英] C# GetMethod doesn't return a parent method
问题描述
我有以下分类:
public class A
{
public static object GetMe(SomeOtherClass something)
{
return something.Foo();
}
}
public class B:A
{
public static new object GetMe(SomeOtherClass something)
{
return something.Bar();
}
}
public class C:B
{
}
public class SomeOtherClass
{
}
由于 SomeOtherClass参数=新SomeOtherClass()
)这个作品:
typeof(B).GetMethod("GetMe", new Type[] { typeof(SomeOtherClass) })).Invoke(null, parameter));
不过这样的:
But this:
typeof(C).GetMethod("GetMe", new Type[] { typeof(SomeOtherClass) })).Invoke(null, parameter));
抛出一个的NullReferenceException
,而我希望它会打电话比上面完全相同的方法。
throws a NullReferenceException
, while I wish it would call the exact same method than above.
我试过几个绑定标志无济于事。任何帮助吗?
I've tried several binding flags to no avail. Any help?
推荐答案
您应该使用一个的重载的拍摄的BindingFlags
参数,包括 FlattenHierarchy
。
You should use one the overloads taking a BindingFlags
parameter, and include FlattenHierarchy
.
指定公共和保护的静态成员了层次应返回。在继承类私有静态成员没有回来。静态成员包括字段,方法,事件和属性。嵌套类型没有回来。
Specifies that public and protected static members up the hierarchy should be returned. Private static members in inherited classes are not returned. Static members include fields, methods, events, and properties. Nested types are not returned.
(编辑,以消除对私有静态方法,现在的问题已经改变,使他们的公共点。)
(Edited to remove the point about private static methods, now the question has been changed to make them public.)
这篇关于C#GetMethod不返回父类的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!