如何实例化没有可访问构造函数的自定义 EventArgs 类?

发布于 2024-08-27 19:01:54 字数 123 浏览 6 评论 0原文

我有一个问题;我正在使用一个外部库,其中一个特定事件有其自己的自定义事件参数;没有构造函数。如果我想使用这些事件参数抛出我自己的事件,我该怎么做?

如果有人问我,我会提供更多细节,但我不确定我到底应该提供什么。 :)

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

哭泣的笑容 2024-09-03 19:01:54

其他答案建议了一些方法(或黑客)如何做到这一点。

但是,我想说,如果该库的作者没有为您提供任何方法来创建其自定义 EventArgs 类的新实例,那么您就不应该这样做。如果您想创建自己的事件,那么您应该定义一个新的 delegate 和新的 EventArgs 类型(即使您复制库中已定义的类) 。

这样做有充分的理由:

  • 库可能会发生变化,并且它们提供的自定义 EventArgs 类型可能不再满足您的需求。
  • 该库可能需要将一些特殊对象或特殊 ID 传递给其 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 new delegate and new EventArgs type (even if you were duplicating the class that is already defined in the library).

There are good reasons for this:

  • The library may change and the custom EventArgs type they provide may no longer fit your needs.
  • The library may need to pass some special object or special ID to their 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?

萌化 2024-09-03 19:01:54

创建一个继承该 EventArgs 类的新类型并使用它。现有的 EventArgs 类型很可能是抽象的,因此您可以创建一个继承它的新类型,并且 C# 的多态支持将允许您将新类型传递给所有需要基类型实例的方法。

Create a new type that inherits from that EventArgs class and use that. Most likely the existing EventArgs type is abstract so you can create a new type that will inherit from it and C#'s polymorphic support will allow you to pass your new type to all methods that expect an instance of the base type.

迷雾森÷林ヴ 2024-09-03 19:01:54

使用 Reflector 来了解外部库如何实例化它。

Use Reflector to find out how the external library instantiates it.

离不开的别离 2024-09-03 19:01:54

显然,该库的设计者将构造函数设置为内部,以防止您完全执行您想要执行的操作 - 无论好坏。所以你所做的任何事情都将是黑客行为。

您可以使用反射来创建类的实例 - 请参阅Activator.CreateInstance

更大的黑客可能是重新使用您接收和存储的 args 对象的实例,但这很危险 - 谁知道它可能包含哪些内部数据。

Clearly, the designers of the library made the constructor internal to prevent you doing exactly what you're trying to do - for better or for worse. So anything you do will be a hack.

You could use Reflection to create an instance the class - see Activator.CreateInstance.

An even bigger hack might be to re-use an instance of the args object that you received and stored, but that's dangerous - who knows what internal data it might contain.

握住我的手 2024-09-03 19:01:54

您可以尝试使用 System.Runtime.Serialization.FormatterServices 获取该类的实例。例子:

public class Foo
{
    private Foo()
    {
    }
}

...

Foo foo = (Foo)FormatterServices.GetSafeUninitializedObject(typeof(Foo));

You can try to obtain an instance of the class using System.Runtime.Serialization.FormatterServices. Example:

public class Foo
{
    private Foo()
    {
    }
}

...

Foo foo = (Foo)FormatterServices.GetSafeUninitializedObject(typeof(Foo));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文