According to the "rules", an EventHandler type parameter must inherit from EventArgs:
Event handler methods accept two parameters. The first type of System.Object is called 'sender'. This is the object that raised the event. The second parameter is of type System.EventArgs and is equal to named 'e'. This is event related data. For example, if an event occurs when a file is opened, the event data usually contains the file name.
In your case, it might be something like this:
public class StringEventArgs : EventArgs { public string Message {get;private set;} public StringEventArgs (string message) { this.Message = message; } }
and your event handler:
public event EventHandler<StringEventArgs> OutputRaised;
When you raise an event, you must disconnect an instance of the StringEventArgs class:
protected virtual void OnWriteText( string message ) { var handle = this.OutputRaised; if (handle != null) { var message = string.Format("({0}) : {1}", this.Port, e); handle(this, new StringEventArgs(message)); } }
I would also like to add that theoretically there is nothing wrong with the code. The compiler does not complain, and your code will work. The EventHandler<T> delegate does not indicate that the type parameter should inherit from EventArgs . This FxCop means that you are violating the "design rules" for declaring an event.
Frederik gheysels
source share