.NET 有内置的 EventArgs吗?

发布于 2024-09-11 11:36:29 字数 500 浏览 3 评论 0原文

我正准备为带有单个参数的事件参数创建一个通用的 EventArgs 类:

public class EventArg<T> : EventArgs
{
    // Property variable
    private readonly T p_EventData;

    // Constructor
    public EventArg(T data)
    {
        p_EventData = data;
    }

    // Property for EventArgs argument
    public T Data
    {
        get { return p_EventData; }
    }
}

在我这样做之前,C# 的语言中是否内置了相同的功能?我记得C# 2.0出来的时候好像遇到过类似的东西,但是现在找不到了。

或者换句话说,我是否必须创建自己的通用 EventArgs 类,还是 C# 提供了一个?感谢您的帮助。

I am getting ready to create a generic EventArgs class for event args that carry a single argument:

public class EventArg<T> : EventArgs
{
    // Property variable
    private readonly T p_EventData;

    // Constructor
    public EventArg(T data)
    {
        p_EventData = data;
    }

    // Property for EventArgs argument
    public T Data
    {
        get { return p_EventData; }
    }
}

Before I do that, does C# have the same feature built in to the language? I seem to recall coming across something like that when C# 2.0 came out, but now I can't find it.

Or to put it another way, do I have to create my own generic EventArgs class, or does C# provide one? Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

灵芸 2024-09-18 11:36:29

不。您可能正在考虑 EventHandler,它允许您为任何特定类型的 EventArgs 定义委托。

不过,我个人认为 EventArgs 不太合适。在我看来,事件参数中用作“有效负载”的信息应该是一个自定义类,以使其用法和预期属性非常清晰。使用通用类将阻止您放置有意义的名称。 (“数据”代表什么?)

No. You probably were thinking of EventHandler<T>, which allows you to define the delegate for any specific type of EventArgs.

I personally don't feel that EventArgs<T> is quite as good of a fit, though. The information used as a "payload" in the event args should be, in my opinion, a custom class to make its usage and expected properties very clear. Using a generic class will prevent you from being able to put meaningful names into place. (What does "Data" represent?)

╭ゆ眷念 2024-09-18 11:36:29

我必须说我不理解这里所有的“纯粹主义者”。
即,如果您已经定义了一个包类 - 它具有所有细节、属性等 - 为什么黑客创建一个额外的不必要的类只是为了能够遵循事件/参数机制、签名样式?
事实是 - 并非 .NET 中的所有内容 - 或者就此而言“缺失”的内容 - 都是“好的” - MS 多年来一直在“纠正”自己......
我想说就去创建一个 - 就像我一样 - 因为我需要它 - 这节省了我很多时间,

I must say I don't understand all the 'purists' here.
i.e. if you already have a bag class defined - which has all the specifics, properties etc. - why the hack create one extra unnecessary class just to be able to follow the event/args mechanism, signature style?
thing is - not everything that is in .NET - or is 'missing from' for that matter - is 'good' - MS's been 'correcting' itself for years...
I'd say just go and create one - like I did - cause I needed it just like that - and saved me lot of time,

半衬遮猫 2024-09-18 11:36:29

它确实存在。至少,现在是这样。

您可以找到 DataEventArgs 在一些不同的 Microsoft 程序集/命名空间中,例如 Microsoft.Practices。 Prism.Events。然而,您可能觉得这些命名空间不适合包含在您的项目中,因此您可能只使用自己的实现。

It does exist. At least, it does now.

You can find DataEventArgs<TData> in some different Microsoft assemblies/namespaces, for instance Microsoft.Practices.Prism.Events. However these are namespaces that you might not find natural to include in your project so you might just use your own implementation.

兲鉂ぱ嘚淚 2024-09-18 11:36:29

如果您选择不使用 Prism,但仍想尝试通用EventArgs 方法。

public class GenericEventArgs<T> : EventArgs
{
    public T EventData { get; private set; }

    public GenericEventArgs(T EventData)
    {
        this.EventData = EventData;
    }
}

// 使用以下示例代码来声明 ObjAdded 事件

public event EventHandler<GenericEventArgs<TargetObjType>> ObjAdded;

// 使用以下示例代码来引发 ObjAdded 事件

private void OnObjAdded(TargetObjType TargetObj)
{
    if (ObjAdded!= null)
    {
        ObjAdded.Invoke(this, new GenericEventArgs<TargetObjType>(TargetObj));
    }
}

// 最后您可以订阅您的 ObjAdded em> 事件

SubscriberObj.ObjAdded +=  (object sender, GenericEventArgs<TargetObjType> e) =>
{
    // Here you can explore your e.EventData properties
};

In case you choose not to use Prism, but still would like to try a generic EventArgs approach.

public class GenericEventArgs<T> : EventArgs
{
    public T EventData { get; private set; }

    public GenericEventArgs(T EventData)
    {
        this.EventData = EventData;
    }
}

// Use the following sample code to declare ObjAdded event

public event EventHandler<GenericEventArgs<TargetObjType>> ObjAdded;

// Use the following sample code to raise ObjAdded event

private void OnObjAdded(TargetObjType TargetObj)
{
    if (ObjAdded!= null)
    {
        ObjAdded.Invoke(this, new GenericEventArgs<TargetObjType>(TargetObj));
    }
}

// And finnaly you can subscribe your ObjAdded event

SubscriberObj.ObjAdded +=  (object sender, GenericEventArgs<TargetObjType> e) =>
{
    // Here you can explore your e.EventData properties
};
十二 2024-09-18 11:36:29

没有内置的通用参数。
如果您遵循 Microsoft EventHandler 模式,那么您可以像您建议的那样实现派生的 EventArgs:

public class MyStringChangedEventArgs : EventArgs 
{ 
  public string OldValue { get; set; }
}

但是 - 如果您的团队风格指南接受简化 - 您的项目可以使用轻量级事件,如下所示:

public event Action<object, string> MyStringChanged;

用法:

// How to rise
private void OnMyStringChanged(string e)
{
    Action<object, string> handler = MyStringChanged;    // thread safeness
    if (handler != null)
    {
        handler(this, e);
    }
}

// How to handle
myObject.MyStringChanged += (sender, e) => Console.WriteLine(e);

通常 PoC 项目使用后一种方法。然而,在专业应用程序中,请注意外汇警察的理由#CA1009:https:/ /msdn.microsoft.com/en-us/library/ms182133.aspx

THERE IS NO BUILT-IN GENERIC ARGS.
If you follow Microsoft EventHandler pattern, then you implement your derived EventArgs like you suggested:

public class MyStringChangedEventArgs : EventArgs 
{ 
  public string OldValue { get; set; }
}

HOWEVER - if your team style guide accepts a simplification - your project can use a lightweight events, like this:

public event Action<object, string> MyStringChanged;

usage :

// How to rise
private void OnMyStringChanged(string e)
{
    Action<object, string> handler = MyStringChanged;    // thread safeness
    if (handler != null)
    {
        handler(this, e);
    }
}

// How to handle
myObject.MyStringChanged += (sender, e) => Console.WriteLine(e);

Usually a PoC projects use the latter approach. In professional applicatons, however, be aware of FX cop justification #CA1009: https://msdn.microsoft.com/en-us/library/ms182133.aspx

隐诗 2024-09-18 11:36:29

泛型类型的问题在于,即使 DerivedType 继承自 BaseType,EventArgs(DerivedType) 也不会继承自 EventArgs(BaseType)。因此,使用 EventArgs(BaseType) 将阻止以后使用该类型的派生版本。

The problem with a generic type is that even if DerivedType inherits from BaseType, EventArgs(DerivedType) would not inherit from EventArgs(BaseType). Using EventArgs(BaseType) would thus prevent later using a derived version of the type.

2024-09-18 11:36:29

这个不存在的原因是因为最终发生的事情是你实现这个,然后当你去填写 T 时,你应该创建一个具有强类型明确属性的类,充当事件参数的数据包,但是在实现的一半过程中,您意识到没有理由不让该类继承自 EventArgs 并称其为好的。

除非您只想要一个字符串或类似的基本数据包,在这种情况下,.NET 中可能有标准的 EventArgs 类,它们旨在满足您想要达到的任何简单目的。

The reason this does not exist is because what would end up happening is you implement this, and then when you go to fill in the T you should create a class with strongly typed unambiguous properties that acts as the data bag for your event arg, but halfway through implementing that you realize there's no reason you don't just make that class inherit from EventArgs and call it good.

Unless you just want a string or something similarly basic for your data bag, in which case there are probably EventArgs classes standard in .NET which are meant to serve whatever simple purpose you're getting at.

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