Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 SomeEventArgs(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

like image 241
abatishchev Avatar asked Jun 19 '10 19:06

abatishchev


2 Answers

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 :)

like image 157
Jon Skeet Avatar answered Oct 18 '22 02:10

Jon Skeet


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.

like image 33
STO Avatar answered Oct 18 '22 01:10

STO