gpt4 book ai didi

c# - 如何在 ThreadContext 类中获取 MethodInfo?

转载 作者:行者123 更新时间:2023-11-30 22:51:03 25 4
gpt4 key购买 nike

我正在尝试这个:

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 为空。我知道 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 );

郑重声明,反射(reflect)非公开成员通常是不好的做法,但您可能已经知道这一点。

编辑 本着授人以鱼的精神,我通过对类型对象调用 GetMethods(...) 并检查返回的数组来解决这个问题查看这些方法是如何命名的。果然,这些名称包含了完整的命名空间规范。

关于c# - 如何在 ThreadContext 类中获取 MethodInfo?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/215213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com