我正在尝试这个:
Type ThreadContextType = typeof(Application).GetNestedType("ThreadContext", System.Reflection.BindingFlags.NonPublic);
MethodInfo FDoIdleMi = ThreadContextType.GetMethod("FDoIdle", BindingFlags.NonPublic |
BindingFlags.Instance, null, new Type[] { typeof(Int32) }, null);
ThreadContextType 正常,但 FDoIdleMi 为 null。我知道 GetMethod 调用有问题,因为 FDoIdle 来自 UnsafeNativeMethods.IMsoComponent 接口(interface)。
如何做到这一点?谢谢。
最佳答案
您需要完全限定方法名称,因为它们使用显式接口(interface)实现:
Type type = typeof( Application ).GetNestedType( "ThreadContext",
BindingFlags.NonPublic );
MethodInfo doIdle = type.GetMethod(
"System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FDoIdle",
BindingFlags.NonPublic | BindingFlags.Instance );
郑重声明,反射(reflection)非公开成员通常是不好的做法,但您可能已经知道这一点。
编辑本着授人以鱼的精神,我通过在类型对象上调用GetMethods(...)
并检查返回的数组来解决这个问题查看这些方法是如何命名的。果然,这些名称包含了完整的命名空间规范。
关于c# - 如何获取ThreadContext类中的MethodInfo?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/215213/