19

使用泛型,是否有理由创建特定的派生 EventArg 类

现在似乎您可以通过通用实现简单地使用它们。

我是否应该仔细阅读所有示例并删除我的 eventArg 类(StringEventArgs、MyFooEventArgs 等。)

public class EventArgs<T> : EventArgs
{
    public EventArgs(T value)
    {
        m_value = value;
    }

    private T m_value;

    public T Value
    {
        get { return m_value; }
    }
}
4

4 回答 4

38

您所描述的本质上是tuples,用于特定目的的分组值。它们在函数式编程中是一个有用的构造,并且很好地支持了这种风格。

缺点是它们的值没有命名,并且需要理解上下文。EventArgs就其本质而言,它们通常在远离其相关背景的地方被消耗。因此,元组式EventArgs可能会让消费者感到非常困惑。

假设我们有一个事件表明某个除法已经完成,它带有分子、分母和结果:

public event EventHandler<EventArgs<double, double, double>> Divided;

事件处理程序有一些歧义:

private void OnDivided(object sender, EventArgs<double, double, double> e)
{
    // I have to just "know" this - it is a convention

    var numerator = e.Value1;
    var denominator = e.Value2;
    var result = e.Value3;
}

EventArgs使用代表事件会更清楚:

private void OnDivided(object sender, DividedEventArgs e)
{
    var numerator = e.Numerator;
    var denominator = e.Denominator;
    var result = e.Result;
}

通用的可重用EventArgs类以表达意图为代价简化了机制的开发。

于 2008-11-22T18:25:50.493 回答
3

查看Matthew Cochran撰写的Custom Generic EventArgs文章,在那篇文章中他描述了如何使用两个和三个成员进一步扩展它。

使用泛型 EventArgs 有其用途,当然也有其误用,因为在此过程中会丢失类型信息。

public class City {...}

public delegate void FireNuclearMissile(object sender, EventArgs<City> args);
public event FireNuclearMissile FireNuclearMissileEvent;

public delegate void QueryPopulation(object sender, EventArgs<City> args);
public event QueryPopulation QueryPopulationEvent;

在下面的示例中,它是类型安全的,但更多的是 LOC:

class City {...}

public class FireNuclearMissileEventArgs : EventArgs
{
    public FireNuclearMissileEventArgs(City city)
    {
        this.city = city;
    }

    private City city;

    public City City
    {
        get { return this.city; }
    }
}

public delegate void FireNuclearMissile(object sender, FireNuclearMissileEventArgs args);
public event FireNuclearMissile FireNuclearMissileEvent;

public class QueryPopulationEventArgs : EventArgs
{
    public QueryPopulationEventArgs(City city)
    {
        this.city = city;
    }

    private City city;

    public City City
    {
        get { return this.city; }
    }
}

public delegate void QueryPopulation(object sender, QueryPopulationEventArgs args);
public event QueryPopulation QueryPopulationEvent;
于 2008-11-22T17:00:19.183 回答
1

我认为元组样式的 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"));
于 2013-08-01T18:58:10.887 回答
0

正如 TcKs 已经说过的:EventArgs<T>如果您只需要传递一个值,则使用,否则从EventArgs(或EventArgs<T>,任何您想要的)派生。

于 2008-11-22T15:43:46.623 回答