C# GetMethod doesn't return a parent method

百般思念 提交于 2019-12-01 15:21:15

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

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));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!