gpt4 book ai didi

c# - 通过反射获取事件参数

转载 作者:行者123 更新时间:2023-11-30 19:14:23 25 4
gpt4 key购买 nike

我不知道如何获取事件的参数类型。

例如,我只能看到使用 MethodInfo 来获取参数,但我有 EventInfo 或 FieldInfo。

我想要的是能够从中获得“ bool 值”:

Public Event EventName(ByVal sender As Object, ByVal value As Boolean)

理论上我可以尝试像 GetRaiseMethod() 这样的方法,但那是行不通的(因为该方法根据 this link 返回 null),即使它这样做了,它也需要首先绑定(bind)一个方法,这意味着测试套件只是确认事件在初始化时具有特定类型的参数。

有什么想法吗?

最佳答案

假设事件 EventName 是在类 DeclaringClass 中声明的,并且该事件至少有 to 个参数,您可以获得第二个参数的类型,如下所示。否则您可能会收到异常。

Type secondEventHandlerParameterType = 
typeof(DeclaringClass).
GetEvent("EventName").
EventHandlerType.
GetMethod("Invoke").
GetParameters()[1].
ParameterType;

关于c# - 通过反射获取事件参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/749956/

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