Feedback

C# - Generische Event Args -> EventArgs<T>

Veröffentlicht von am 11/16/2010
(0 Bewertungen)
Wer kennt das Problem nicht?

Man hat 10 Entitäten und nun braucht man 10 Events und kann sich an das dumpfsinnige schreiben einfacher EventArgs machen.

Aus diesem Grund gibts hier eine Generische Variante

EventArgs<MyEntity> myArgs = new EventArgs<MyEntity>(myEntityInstace);
using System;
using System.Diagnostics.Contracts;

namespace BE.Framework.Events
{
   /// <summary>
   /// Type Save Generic Eventargs
   /// </summary>
   /// <typeparam name="T">Type of the Argument Value</typeparam>
   /// <remarks>This generic inherited version of the default eventargs can be used to avoid many dummy eventarg classes. </remarks>
    public class EventArgs<T> : System.EventArgs
    {
        [ContractInvariantMethod]
        void ObjectInvariant()
        {
            Contract.Invariant(this.Value != null); 
        }

       private T m_Value;

       /// <summary>
       /// Gets or sets the value of the EventArgs.
       /// </summary>
       /// <value>The value.</value>
       public T Value
       { 
           get
           {
               return m_Value;
           }
           private set
           {
               
               m_Value = value;
           }
       }
        /// <summary>
        /// Initializes a new instance of the <see cref="GenericEventArgs&lt;T&gt;"/> class.
        /// </summary>
        public EventArgs()
            : base()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="GenericEventArgs&lt;T&gt;"/> class.
        /// </summary>
        public EventArgs(T value)
            : this()
        {
            Contract.Requires<ArgumentNullException>(value != null);
            Contract.Ensures(this.Value != null);
            this.Value = value;
        }
    }
}

Kommentare zum Snippet

 

Logge dich ein, um hier zu kommentieren!