我认为元组样式的 EventArgs 很有用。就像 Tuple 的一样,它们可能会被误用,但似乎我的懒惰比我的谨慎感强。我实现了以下内容:
public static class TupleEventArgs
{
static public TupleEventArgs<T1> Create<T1>(T1 item1)
{
return new TupleEventArgs<T1>(item1);
}
static public TupleEventArgs<T1, T2> Create<T1, T2>(T1 item1, T2 item2)
{
return new TupleEventArgs<T1, T2>(item1, item2);
}
static public TupleEventArgs<T1, T2, T3> Create<T1, T2, T3>(T1 item1, T2 item2, T3 item3)
{
return new TupleEventArgs<T1, T2, T3>(item1, item2, item3);
}
}
public class TupleEventArgs<T1> : EventArgs
{
public T1 Item1;
public TupleEventArgs(T1 item1)
{
Item1 = item1;
}
}
public class TupleEventArgs<T1, T2> : EventArgs
{
public T1 Item1;
public T2 Item2;
public TupleEventArgs(T1 item1, T2 item2)
{
Item1 = item1;
Item2 = item2;
}
}
public class TupleEventArgs<T1, T2, T3> : EventArgs
{
public T1 Item1;
public T2 Item2;
public T3 Item3;
public TupleEventArgs(T1 item1, T2 item2, T3 item3)
{
Item1 = item1;
Item2 = item2;
Item3 = item3;
}
}
可以按如下方式使用(与事件引发器扩展一起使用时)
public event EventHandler<TupleEventArgs<string,string,string>> NewEvent;
NewEvent.Raise(this, TupleEventArgs.Create("1", "2", "3"));