C# 7.3增加了对将泛型类型参数限制为委托类型的支持。support
public class UsingDelegate<T> where T : System.Delegate { }
public class Multicaster<T> where T : System.MulticastDelegate { }
这可能很琐碎,但是在where T : System.Delegate
和where T : System.MulticastDelegate
之间是否有任何实际区别?这在Microsoft Docs上没有记录。
System.Delegate
和System.MulticastDelegate
,因为没有从前者而不是后者派生的类型。 - fghzxmwhere T : System.Delegate
就足够了。在MulticastDelegate
中所有公共方法都是对虚拟Delegate
方法的重写(或者像等式操作符一样,转给一个方法去处理)。任何你需要使用T
的地方都可以得到MulticastDelegate
的实现。甚至静态方法Delegate.Combine
和Delegate.Remove
都会调用被MulticastDelegate
重写的虚拟方法。 - madreflection