开发者

Generic built-in EventArgs to hold only one property?

I have a number of EventArgs classes with only one field and an appropriate property to read it:

public class SomeEventArgs : EventArgs
{
    private readonly Foo f;
    public SomeEventArg开发者_运维问答s(Foo f)
    {
        this.f = f;
    }
    public Foo Foo
    {
        get { return this.f; }
    }    
}

Is there any built-in, generic class to implement such behavior or I have to roll my own?

public class GenericEventArgs<T> : EventArgs
{
    private readonly T value;
    public GenericEventArgs(T v)
    {
        this.value = v;
    }
    public T Value
    {
        get { return this.value; }
    }    
}

P.S. I wrote a suggestion on Microsoft Connect


If there is one, it certainly isn't well publicised! (i.e. it's not something you're meant to use for general purpose stuff, like Func<T>.) I've thought about the same thing myself before now. It's not a rare requirement, IMO.

One downside of this is that it doesn't have a meaningful name for the property, of course - it's like an EventArgs equivalent of Tuple. But if you have several different use cases for this and it will actually be obvious what the meaning is, go for it :)


On this page at bottom you can see all classes are inherited from EventArgs class: http://msdn.microsoft.com/en-us/library/system.eventargs.aspx

The most appropriate is ReturnEventArgs http://msdn.microsoft.com/en-us/library/ms615572.aspx but this class is located in PresentationFramework.dll, that is only referenced by WPF projects.

So I recommend to create your own one.


I do not think there is.
Looks like you are not the only one to ask himself this question.
Take a look here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜