问题
I just figured out by accident (when something compiled that I didn't think would compile) that EventHandler is not constrained to the type System.EventArgs.
Here's the inline docs:
#region Assembly mscorlib.dll, v4.0.0.0
// C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\mscorlib.dll
#endregion
namespace System
{
// Summary:
// Represents the method that will handle an event.
//
// Parameters:
// sender:
// The source of the event.
//
// e:
// An System.EventArgs that contains the event data.
//
// Type parameters:
// TEventArgs:
// The type of the event data generated by the event.
[Serializable]
public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e);
}
Is this a mismatch between docs and implementation?
I'm asking because I am curious. It's not a complaint at all.
回答1:
The type constraint was removed in .net 4.5.
Here is the .net 4.5 signature. http://msdn.microsoft.com/en-us/library/db0etb8x%28v=vs.110%29.aspx
[SerializableAttribute]
public delegate void EventHandler<TEventArgs>(
Object sender,
TEventArgs e
)
Here is the .net 4.0 signature. http://msdn.microsoft.com/en-us/library/db0etb8x%28v=vs.100%29.aspx
[SerializableAttribute]
public delegate void EventHandler<TEventArgs>(
Object sender,
TEventArgs e
)
where TEventArgs : EventArgs
来源:https://stackoverflow.com/questions/15736970/why-no-constraint-on-eventhandlerteventargs