views:

44

answers:

3

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

+1  A: 

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

Jon Skeet
@Itay's link follows to interesting name: just `EventArgs<T>`. It's very interesting why FCL doesn't contain yet such very useful class!
abatishchev
+1  A: 

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

Itay
+1  A: 

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.

STO