.NET 是否有内置的 EventArgs<T>? [英] Does .NET have a built-in EventArgs<T>?
问题描述
我正准备为带有单个参数的事件参数创建一个通用的 EventArgs 类:
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; }
}
}
在我这样做之前,C# 是否具有内置到该语言中的相同功能?我好像记得在 C# 2.0 出来的时候遇到过类似的东西,但现在我找不到了.
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.
或者换句话说,我是否必须创建自己的通用 EventArgs 类,或者 C# 是否提供了一个?感谢您的帮助.
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.
推荐答案
没有.您可能正在考虑 EventHandler
,它允许您为任何特定类型的 EventArgs 定义委托.
No. You probably were thinking of EventHandler<T>
, which allows you to define the delegate for any specific type of EventArgs.
不过,我个人并不认为 EventArgs
非常合适.用作有效载荷"的信息被用作有效载荷".在我看来,如果 args 应该是一个自定义类,以使其用法和预期属性非常清楚.使用泛型类将阻止您将有意义的名称放在适当的位置.(数据"代表什么?)
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?)
这篇关于.NET 是否有内置的 EventArgs<T>?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!