Reflection: get static method from parent class

I have a task to get a static method using reflection as follows:

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

If the class contains MyMethod , everything works correctly, but in case the parent class contains MyMethod , I get null :(. How can I call the static method from the parent using reflection, like the code described above? Thanks.

+4
source share
2 answers

Try using the BindingFlags.FlattenHierarchy binding BindingFlags.FlattenHierarchy . (I have not tried it myself, so I apologize if I spend your time.)

+5
source

it’s very simple to get an object of the type that describes the parent class and execute the above code on that object, which will provide you with the MethodInfo object that you need. Call the methodInfo object by passing it the myType object for the instance parameter

0
source

Source: https://habr.com/ru/post/1309537/


All Articles