简体   繁体   English

反射:从父类中获取静态方法

[英]Reflection: get Static method from the parent class

I have task to get static method using reflection like this : 我有任务使用这样的反射获取静态方法:

myType.GetMethod("MyMethod",BindingFlags.Static | BindingFlags.Public | BindingFlags.InvokeMethod);

In case if class contains MyMethod all works correctly, but in case if parent class contains MyMethod I receive null :(. How can I call static method from the parent using reflection like code that I describe above? Thanks. 如果类包含MyMethod一切正常,但是如果父类包含MyMethod我会收到null :(。如何使用我在上面描述的代码之类的反射调用父类的静态方法?谢谢。

Try using the BindingFlags.FlattenHierarchy binding attribute. 尝试使用BindingFlags.FlattenHierarchy绑定属性。 (I haven't tried it myself, so my apologies if I waste your time.) (我自己没试过,所以如果我浪费你的时间,我很抱歉。)

very simple get the type object that describes the parent class and execute the above code on that object that will give you the MethodInfo object you need. 非常简单地获取描述父类的类型对象,并在该对象上执行上述代码,该代码将为您提供所需的MethodInfo对象。 Invoke the methodInfo object passing it an object of myType for the instance parameter 调用methodInfo对象,为实例参数传递myType对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM