您是否认为创建一个抽象泛型类是一种可接受或不好的做法,该类将类型参数作为一个派生自己的类?
这允许抽象泛型类操作派生类的实例,特别是在必要时创建派生类的new()实例的能力,并且可以帮助避免从派生类派生的具体类中重复代码.
如果"糟糕"你更喜欢处理这种情况的替代方案,你将如何构建下面的代码?
例如:-
// We pass both the wrapped class and the wrapping class as type parameters
// to the generic class allowing it to create instances of either as necessary.
public abstract class CoolClass<T, U>
where U : CoolClass<T, U>, new()
{
public T Value { get; private set; }
protected CoolClass() { }
public CoolClass(T value) { Value = value; }
public static implicit operator CoolClass<T, U>(T val)
{
// since we know the derived type and …Run Code Online (Sandbox Code Playgroud)