C# GetMethod doesn't return a parent method

后端 未结 2 784
一生所求
一生所求 2020-12-20 12:27

I have the following class tree:

public class A
{
    public static object GetMe(SomeOtherClass something)
    {
        return something.Foo();
    }
}

pub         


        
相关标签:
2条回答
  • 2020-12-20 13:23

    You need to pass BindingFlags.FlattenHierarchy flag to GetMethod in order to search up the hierarchy:

    typeof(C).GetMethod("GetMe", BindingFlags.FlattenHierarchy, null, new Type[] { typeof(SomeOtherClass) }, null)).Invoke(null, parameter));
    
    0 讨论(0)
  • 2020-12-20 13:28

    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.)

    0 讨论(0)
提交回复
热议问题