如何实例化没有可访问构造函数的自定义 EventArgs 类?
我有一个问题;我正在使用一个外部库,其中一个特定事件有其自己的自定义事件参数;没有构造函数。如果我想使用这些事件参数抛出我自己的事件,我该怎么做?
如果有人问我,我会提供更多细节,但我不确定我到底应该提供什么。 :)
I have a problem; I'm using an external library where one particular event has its own custom eventargs; with no constructor. If I want to throw my own event using these eventargs, how can I?
I'll give more detail if asked, but I'm not sure what exactly I should be giving. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(5)
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
其他答案建议了一些方法(或黑客)如何做到这一点。
但是,我想说,如果该库的作者没有为您提供任何方法来创建其自定义
EventArgs
类的新实例,那么您就不应该这样做。如果您想创建自己的事件,那么您应该定义一个新的delegate
和新的EventArgs
类型(即使您复制库中已定义的类) 。这样做有充分的理由:
EventArgs
类型可能不再满足您的需求。EventArgs
,因此您可能无法正确创建实例。由于您只能从定义事件的类中触发事件,因此您可能在类中定义了一个新事件(使用
event
关键字),因此没有真正的理由不能' t 声明你自己的代表。或者您能否提供有关如何触发该事件的更多详细信息?Other answers suggest some ways (or hacks) how you could do that.
However, I'd say that if the author of the library didn't give you any way to create a new instance of their custom
EventArgs
class, then you shouldn't be doing that. If you want to create your own event, then you should define a newdelegate
and newEventArgs
type (even if you were duplicating the class that is already defined in the library).There are good reasons for this:
EventArgs
type they provide may no longer fit your needs.EventArgs
, so you may not be able to create the instance correctly.Since you can only trigger an event from a class where it is defined, you're probably defining a new event in the class (using the
event
keyword), so there is no real reason why you couldn't declare your own delegate. Or could you provide more details on how are you triggering the event?