.NET EventHandlers - Generic or no?

后端 未结 9 1805
遇见更好的自我
遇见更好的自我 2020-12-03 13:53

Every time I start in deep in a C# project, I end up with lots of events that really just need to pass a single item. I stick with the EventHandler/Event

相关标签:
9条回答
  • 2020-12-03 14:26

    I do believe that the recent versions of .NET have just such an event handler defined in them. That's a big thumbs up as far as I'm concerned.

    /EDIT

    Didn't get the distinction there originally. As long as you are passing back a class that inherits from EventArgs, which you are, I don't see a problem. I would be concerned if you weren't wrapping the resultfor maintainability reasons. I still say it looks good to me.

    0 讨论(0)
  • 2020-12-03 14:27

    To make generic event declaration easier, I created a couple of code snippets for it. To use them:

    • Copy the whole snippet.
    • Paste it in a text file (e.g. in Notepad).
    • Save the file with a .snippet extension.
    • Put the .snippet file in your appropriate snippet directory, such as:

    Visual Studio 2008\Code Snippets\Visual C#\My Code Snippets

    Here's one that uses a custom EventArgs class with one property:

    <?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 one type/argument.</Title>
          <Shortcut>ev1Generic</Shortcut>
          <Description>Code snippet for event handler and On method</Description>
          <Author>Ryan Lundy</Author>
          <SnippetTypes>
            <SnippetType>Expansion</SnippetType>
          </SnippetTypes>
        </Header>
        <Snippet>
          <Declarations>
            <Literal>
              <ID>type</ID>
              <ToolTip>Type of the property in the EventArgs subclass.</ToolTip>
              <Default>propertyType</Default>
            </Literal>
            <Literal>
              <ID>argName</ID>
              <ToolTip>Name of the argument in the EventArgs subclass constructor.</ToolTip>
              <Default>propertyName</Default>
            </Literal>
            <Literal>
              <ID>propertyName</ID>
              <ToolTip>Name of the property in the EventArgs subclass.</ToolTip>
              <Default>PropertyName</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($type$ $argName$)
            {
              this.$propertyName$ = $argName$;
            }
    
            public $type$ $propertyName$ { 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>
          <Imports>
            <Import>
              <Namespace>System</Namespace>
            </Import>
          </Imports>
        </Snippet>
      </CodeSnippet>
    </CodeSnippets>
    

    And here's one that has two properties:

    <?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>Ryan Lundy</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>propertyType1</Default>
            </Literal>
            <Literal>
              <ID>arg2Name</ID>
              <ToolTip>Name of the second argument in the EventArgs subclass constructor.</ToolTip>
              <Default>property1Name</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>
          <Imports>
            <Import>
              <Namespace>System</Namespace>
            </Import>
          </Imports>
        </Snippet>
      </CodeSnippet>
    </CodeSnippets>
    

    You can follow the pattern to create them with as many properties as you like.

    0 讨论(0)
  • 2020-12-03 14:28

    Since .NET 2.0

    EventHandler<T>

    has been implemented.

    0 讨论(0)
提交回复
热议问题