如何错的是它与超出标准(的OBJ发件人,EventArgs参数)签名创建一个事件处理程序委托?

StackOverflow https://stackoverflow.com/questions/1157072

  •  18-09-2019
  •  | 
  •  

我明白使用标准的MS事件处理程序委托签名,因为它可以让你轻松通过事件与出打破了基于旧的委托签名的任何旧关系传递的信息扩大的好处。

什么我不知道是如何在实践中往往人们遵循这个规则?说我有这样一个简单的事件

public event NameChangedHandler NameChanged;
public delegate void NameChangedHandler(Object sender, string oldName, string newName);

这是一个简单的事件,我几乎积极的,我曾经一往无前的唯一参数需要知道从NameChanged事件是其名称更改的对象,旧名称,新名称。因此,它是值得的,创建一个单独的NameChangedEventArgs类,或者简单的这样的活动是可以接受的,只是通过委托参数直接返回的参数?

有帮助吗?

解决方案

如果你是谁拥有对付它的唯一一个你可以做任何事情,走错了路。但它不是一个坏主意,学习标准,并坚持给他们,让你继续当你工作的代码与他人的良好习惯。

所以,我会让你成为一个交易。如果你答应做正确的方式,我给你的代码片段将使它更痛苦少。只要把这个在一个是.snippet文件,并把该文件中:

我的文档\ Visual Studio 2008的\代码段\的Visual C#\我的代码段\结果 (或Visual Studio 2005如果适用)

和这里的代码段;通过键入ev2Generic,打标签使用它在VS:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Generic event with two types/arguments.</Title>
      <Shortcut>ev2Generic</Shortcut>
      <Description>Code snippet for event handler and On method</Description>
      <Author>Kyralessa</Author>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>type1</ID>
          <ToolTip>Type of the first property in the EventArgs subclass.</ToolTip>
          <Default>propertyType1</Default>
        </Literal>
        <Literal>
          <ID>arg1Name</ID>
          <ToolTip>Name of the first argument in the EventArgs subclass constructor.</ToolTip>
          <Default>property1Name</Default>
        </Literal>
        <Literal>
          <ID>property1Name</ID>
          <ToolTip>Name of the first property in the EventArgs subclass.</ToolTip>
          <Default>Property1Name</Default>
        </Literal>
        <Literal>
          <ID>type2</ID>
          <ToolTip>Type of the second property in the EventArgs subclass.</ToolTip>
          <Default>propertyType2</Default>
        </Literal>
        <Literal>
          <ID>arg2Name</ID>
          <ToolTip>Name of the second argument in the EventArgs subclass constructor.</ToolTip>
          <Default>property2Name</Default>
        </Literal>
        <Literal>
          <ID>property2Name</ID>
          <ToolTip>Name of the second property in the EventArgs subclass.</ToolTip>
          <Default>Property2Name</Default>
        </Literal>
        <Literal>
          <ID>eventName</ID>
          <ToolTip>Name of the event</ToolTip>
          <Default>NameOfEvent</Default>
        </Literal>
      </Declarations>
      <Code Language="CSharp">
        <![CDATA[public class $eventName$EventArgs : System.EventArgs
      {
        public $eventName$EventArgs($type1$ $arg1Name$, $type2$ $arg2Name$)
        {
          this.$property1Name$ = $arg1Name$;
          this.$property2Name$ = $arg2Name$;
        }

        public $type1$ $property1Name$ { get; private set; }
        public $type2$ $property2Name$ { get; private set; }
      }

      public event EventHandler<$eventName$EventArgs> $eventName$;
            protected virtual void On$eventName$($eventName$EventArgs e)
            {
                var handler = $eventName$;
                if (handler != null)
                    handler(this, e);
            }]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

其他提示

使用EventHandler<T>泛型委托为您的活动,并创建EventArgs衍生握住你的事件数据的类型。因此,换句话说,始终。这件事情,你总是知道,当你遇到它,因为它从来没有做过,否则它究竟是如何工作的。

编辑:

代码分析 CA1003:使用通用事件处理程序实例结果 代码分析 CA1009:声明的事件处理程序正确

  

在实践中是如何经常做的人[不   使用EventArgs的派生类。

当EventArgs的派生类没有被用来我从未遇到过的时间。至于你说的你自己,它会增加你日后更改代码的能力。我也认为,可读性得到改善,因为它很容易看出这是一个事件处理程序。

  

是否值得创建一个单独的   NameChangedEventArgs类,或   简单的事件,如这是它   接受只返回   直接参数通过   代表参数?

您好像说你会使用EventArgs的事件处理程序的更多PARAMS和不使用它像这样的情况。老实说,这根本就不是一个选项,在C#编程时。一致性是必须的,尤其是在当今世界论坛上的这个样子,开源项目等在那里很容易失去它的。如果确定你是一个岩石下编程这一点,你可以做任何你喜欢的,但更大的C#社区会感谢你对你的代码中使用的一致性以下标准和特别。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top