自引用泛型模式分析
摘要:曾经有人问我这样一个问题:如何迫使子类提供无参构造函数。当时给出的答案是让子类实现这样一个接口。 public interface IMustHaveParameterLessConstructor where T : IMustHaveParameterLessConstructor, new() { }这种在泛型参数中引用自身的技法,还有个名字,叫做“Self-Referencing Generics”模式。这个技法在C++中已经被使用了20多年,只不过叫做Curiously Recurring Template。这个技法可以用来实现不少有用的功能。比如为所...
阅读全文
posted @ 2014-04-10 23:14