我正在尝试通过以下方案。但是遇到了这个编译时错误。
The type 'DerivedDefinition' cannot be used as type parameter 'T' in the generic type or method 'BaseInstance<T>'. There is no boxing conversion or type parameter conversion from 'DerivedDefinition' to 'BaseDefinition'.
原因是什么?如何解决?
这与上一个问题https://stackoverflow.com/questions/21718509/supporting-generic-and-non-generic-implementation
有关更新:这是基于下面提供的答案的正确工作代码
public abstract class BaseDefinition
{
public virtual void Print() { Console.WriteLine("base definition"); }
}
public class DerivedDefinition : BaseDefinition
{
public override void Print() { Console.WriteLine("derived definition"); }
}
public abstract class BaseInstance
{
protected BaseDefinition definition;
public BaseDefinition Definition { get { return definition; } }
}
public abstract class BaseInstance<T> : BaseInstance where T : BaseDefinition
{
//public T DefinitionExact { get { return (T)definition; } }
}
public class DerivedInstance : BaseInstance<DerivedDefinition>
{
public DerivedInstance()
{
definition = new DerivedDefinition();
}
}
class TestClass
{
static void Main(string[] args)
{
BaseInstance baseInstance = new DerivedInstance();
baseInstance.Definition.Print();
//baseInstance.DefinitionExact.Print();
DerivedInstance derivedInstance = new DerivedInstance();
derivedInstance.Definition.Print();
}
}
以下代码非常错误......
public abstract class BaseDefinition
{
public virtual string Print() { Console.WriteLine("base definition"); }
}
public class DerivedDefinition : BaseDefinition
{
public override string Print() { Console.WriteLine("derived definition"); }
}
public abstract class BaseInstance
{
private BaseDefinition definition;
protected BaseDefinition Definition { get { return definition; } }
}
public abstract class BaseInstance<T> : BaseInstance where T : BaseDefinition
{
protected T DefinitionExact { get { return (T)definition; } }
}
public class DerivedInstance<DerivedDefinition> : BaseInstance<DerivedDefinition>
{
public DerivedInstance()
{
definition = new DerivedDefinition();
}
}
class TestClass
{
static void Main(string[] args)
{
BaseInstance baseInstance = new DerivedInstance<DerivedDefinition>();
baseInstance.Definition.Print();
baseInstance.DefinitionExact.Print();
}
}
答案 0 :(得分:1)
您的通用设置存在问题:
public class DerivedInstance<DerivedDefinition> : BaseInstance<DerivedDefinition>
{
public DerivedInstance()
{
definition = new DerivedDefinition();
}
}
当您声明一个通用类时,您将名称放在&lt;&gt;中这将是您的类内部处理的类型。您想要替换,因此您的代码如下所示:
public class DerivedInstance<T> : BaseInstance<T>
{
public DerivedInstance()
{
definition = new DerivedDefinition();
}
}
答案 1 :(得分:1)
要解决编译器错误,您需要约束派生类中的type参数,以使其满足基类中的约束:
public class DerivedInstance<DerivedDefinition> : BaseInstance<DerivedDefinition>
where DerivedDefinition : BaseDefinition
{
public DerivedInstance()
{
definition = new DerivedDefinition();
}
}
但是,由于你的类型参数的名称与你的一个类名相同,我怀疑你实际上是想这样做:
public class DerivedInstance : BaseInstance<DerivedDefinition>
{
public DerivedInstance()
{
definition = new DerivedDefinition();
}
}
也就是说,DerivedInstance类可能根本不是通用的,而是你打算提供DerivedDefinition作为基类类型参数的参数。