现在需要EventArg类,我们有仿制药 [英] Are EventArg classes needed now that we have generics

查看:444
本文介绍了现在需要EventArg类,我们有仿制药的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用仿制药,是有过一个理由来创建特定的派生EventArg类



好像现在你可以在飞行中与通用实现只使用它们。



我应该去thorugh我所有的例子和删除我的eventArg类(StringEventArgs,MyFooEventArgs等。)

 公共类的EventArgs< T> :EventArgs的
{
公众的EventArgs(T值)
{
m_value =价值;
}

私人牛逼m_value;

公众吨价
{
{返回m_value; }
}
}


解决方案

你所描述基本上是元组,分组用于特定目的的值。他们在函数式编程并支持这种风格很好的一个有用的结构。



的缺点是,它们的值没有被命名,他们需要来理解。 EventArgs的其本质是经常食用远离他们的相关背景。因此,元组式的 EventArgs的可为消费者非常混乱。



让我们说我们有指示一些事件师已经完成,它所携带的分子,分母和结果:

 公共事件的EventHandler< EventArgs的<双,双,双>>分为; 



事件处理程序有一定的模糊性:

 私人无效OnDivided(对象发件人,EventArgs<双,双,双> E)
{
//我只是知道这 - 这是一个会议

无功分子= e.Value1;
VAR分母= e.Value2;
VAR的结果= e.Value3;
}

这是与更清晰的 EventArgs的表示事件:

 私人无效OnDivided(对象发件人,DividedEventArgs E)
{
无功分子= e.Numerator;
VAR分母= e.Denominator;
VAR的结果= e.Result;
}



通用可重复使用的<​​code> EventArgs的班表达意图的代价缓解机制的发展。


With generics, is there ever a reason to create specific derived EventArg classes

It seems like now you can simply use them on the fly with a generic implementation.

Should i go thorugh all of my examples and remove my eventArg classes (StringEventArgs, MyFooEventArgs, etc . .)

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

    private T m_value;

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

解决方案

What you are describing are essentially tuples, grouped values used for a particular purpose. They are a useful construct in functional programming and support that style very well.

The downside is that their values are not named, and they require context to be understood. EventArgs by their very nature are often consumed far away from their relevant context. Therefore, tuple-esque EventArgs can be very confusing for the consumer.

Let's say we have an event indicating some division has been completed, and it carries the numerator, denominator, and result:

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

The event handler has some ambiguity:

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;
}

This would be much clearer with an EventArgs representing the event:

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

Generic reusable EventArgs classes ease development of the mechanism at the expense of expressing intent.

这篇关于现在需要EventArg类,我们有仿制药的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆