using System;
public abstract class MyBaseClass<T>
where T : class, new()
{
protected T RealObject = new T();
public void setRealObject(dynamic realObject) { this.RealObject = realObject; }
public static U CreateFromBaseObject<U>(T baseObject) where U : MyBaseClass<T>, new()
{
var t = new U(); t.setRealObject(baseObject); return t;
}
}
public class RealDerivedClass {}
public class MyDerivedClass : MyBaseClass<RealDerivedClass>{}
public class Test
{
public static void Main()
{
RealDerivedClass realDerivedClass = new RealDerivedClass();
//Приходится так:
MyDerivedClass myRealDerivedClass = MyDerivedClass.CreateFromBaseObject<MyDerivedClass>(realDerivedClass);
//А хочется так:
//MyDerivedClass myRealDerivedClass = MyDerivedClass.CreateFromBaseObject(realDerivedClass);
}
}