Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
.net EventHandler 仅限于从 EventArgs 继承的模板。这是怎么做的?实现(在 vs 中引用)显示了以下代码:
[Serializable] public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e);
但我认为 TEventArgs 只是一个名字。如何编写一个类型化的委托,限制从 MyClass 继承的任何内容?
TEventArgs是一个泛型类型参数- 但它有一个约束。实际签名是:
TEventArgs
[Serializable] public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e) where TEventArgs : EventArgs
“ where TEventArgs : EventArgs” 位是类型约束,这意味着您只能提供is或派生类的类型参数。TEventArgsEventArgs
where TEventArgs : EventArgs
EventArgs
基本上它只是“普通”的 C# 泛型,只是应用于委托声明。