GenericEventArgs

 Hello,

 

You've probably encountered the need to create EventArgs' sub classes to pass in events you created.

For every event you needed to create different EventArgs class with some parameters.

 

This come to reduce the classes and use some generic classes.

 

 

I've created 4 ActionEventArgs and 5 FuncEventArgs to mimic the Action and Func delegates.
ActionEventArgs containing 1 to 4 parameters passed in the EventArgs class.
FuncEventArgs containing Result and up to 4 parameters.
 
The Parameters are read only.
The Result can (and should) be modified by the event handler.
 
Action Event Args
public class ActionEventArgs<T1> : EventArgs
{
    public T1 Parameter1
    {
        get;
        private set;
    }

    public ActionEventArgs(T1 param)
    {
        Parameter1 = param;
    }
}
 
public class ActionEventArgs<T1, T2> : ActionEventArgs<T1>
{
    public T2 Parameter2
    {
        get;
        private set;
    }

    public ActionEventArgs(T1 param1, T2 param2) : base(param1)
    {
        Parameter2 = param2;
    }
}
 
public class ActionEventArgs<T1, T2, T3> : ActionEventArgs<T1, T2>
{
    public T3 Parameter3
    {
        get;
        private set;
    }

    public ActionEventArgs(T1 param1, T2 param2, T3 param3) : base(param1, param2)
    {
        Parameter3 = param3;
    }
}
 
public class ActionEventArgs<T1, T2, T3, T4> : ActionEventArgs<T1, T2, T3>
{
    public T4 Parameter4
    {
        get;
        private set;
    }

    public ActionEventArgs(T1 param1, T2 param2, T3 param3, T4 param4) : base(param1, param2, param3)
    {
        Parameter4 = param4;
    }
}
 
Func Event Args
public class FuncEventArgs<TResult> : EventArgs
{
    public TResult Result { get; set; }

    public FuncEventArgs()
    {
        Result = default(TResult);
    }
}
 
public class FuncEventArgs<T1, TResult> : FuncEventArgs<TResult>
{
    public T1 Parameter1
    {
        get;
        private set;
    }

    public FuncEventArgs(T1 param) : base()
    {
        Parameter1 = param;
    }
}
 
public class FuncEventArgs<T1, T2, TResult> : FuncEventArgs<T1, TResult>
{
    public T2 Parameter2
    {
        get;
        private set;
    }

    public FuncEventArgs(T1 param1, T2 param2) : base(param1)
    {
        Parameter2 = param2;
    }
}
 
public class FuncEventArgs<T1, T2, T3, TResult> : FuncEventArgs<T1, T2, TResult>
{
    public T3 Parameter3
    {
        get;
        private set;
    }

    public FuncEventArgs(T1 param1, T2 param2, T3 param3) : base(param1, param2)
    {
        Parameter3 = param3;
    }
}
 
public class FuncEventArgs<T1, T2, T3, T4, TResult> : FuncEventArgs<T1, T2, T3, TResult>
{
    public T4 Parameter4
    {
        get;
        private set;
    }

    public FuncEventArgs(T1 param1, T2 param2, T3 param3, T4 param4) : base(param1, param2, param3)
    {
        Parameter4 = param4;
    }
}
 
Usage
 
Declaring:
public event EventHandler<FuncEventArgs<int, string, string>> ConvertRequest;
 
Raise:
// copy handler for thread safe
var tempHandler = ConvertRequest;
if (tempHandler != null)
{
	// prepare parameters
	var e = new FuncEventArgs<int,string,string>(10, "Name");

	tempHandler(this, e);

	// get result from clients
	string s = e.Result;
}
 
Register:
ConvertRequest += new EventHandler<FuncEventArgs<int, string, string>>(OnConvertRequest);
 
Handle:
void OnConvertRequest(object sender, FuncEventArgs<int, string, string> e)
{
	e.Result = e.Parameter1.ToString() + e.Parameter2;
}

OfirEnjoy,

 

Thank you for your interest!

We will contact you as soon as possible.

Send us a message

Oops, something went wrong
Please try again or contact us by email at info@tikalk.com