我有一个具有多个派生类的基类:
class Base { }
class Derived1 : Base { }
class Derived2 : Base { }然后我有一个worker类,它将这个类作为泛型类型:
class WorkerClass<T> where T : Base, new()
{
WorkerClass() { }
void DoStuff()
{
T container = new T();
// do stuff
}
}问题是当我实例化WorkerClass时,我只将类型作为变量。我想这样做:
void DoStuff(Type type)
{
WorkerClass<Base> worker_class = null;
if(type == typeof(Derived1))
{
worker_class = new WorkerClass<Derived1>(); // compiler error
}
else if(type == typeof(Derived2))
{
worker_class = new WorkerClass<Derived2>(); // compiler error
}
// lots of common code with worker_class
worker_class.DoStuff();
}但是编译器抱怨将WorkerClass<Derived>隐式转换为WorkerClass<Base>。显式转换也会导致错误。编译器建议定义public static implicit operator WorkerClass<T>(WorkerClass<Derived>),但我不确定这段代码会是什么样子。我显然可以把所有的逻辑都放入if-否则,但这似乎是不必要的重复。
发布于 2015-11-19 16:17:14
我可能误解了您的意图,但我认为您可以使用带有约束的泛型来做这样的事情:
void DoStuff<T>() where T : Base, new()
{
WorkerClass<T> worker_class = new WorkerClass<T>();
// lots of common code with worker_class
worker_class.DoStuff();
}在那里,您可以简单地调用,例如:
DoStuff<Derived1>();发布于 2015-11-19 16:14:12
我对您的代码做了几处修改,以使其正常工作。您的WorkerClass<T>也需要从Base类继承,如果要执行的代码而不是Base类中的DoStuff方法,则可能需要重写超类中的DoStuff调用。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication6
{
public class Base
{
public virtual void DoStuff(){}
}
public class Derived1 : Base
{
public virtual void DoStuff(){}
}
public class Derived2 : Base
{
public virtual void DoStuff(){}
}
public class WorkerClass<T> : Base where T : Base,new()
{
public WorkerClass() { }
public override void DoStuff()
{
T container = new T();
// do stuff
}
}发布于 2015-11-19 16:18:29
您可以使用协方差,但只有接口和委托才支持它。尝试引入接口IWorkerClass<out T>。out关键字将模板类型T标记为协变量。
interface IWorkerClass<out T>
{
void DoStuff();
}
class WorkerClass<T> : IWorkerClass<T> where T : Base, new()
{
public void DoStuff() { }
}然后在代码示例中使用接口而不是类:
void DoStuff(Type type)
{
IWorkerClass<Base> worker_class = null;
if(type == typeof(Derived1))
{
worker_class = new WorkerClass<Derived1>(); // NO compiler error
}
else if(type == typeof(Derived2))
{
worker_class = new WorkerClass<Derived2>(); // NO compiler error
}
// lots of common code with worker_class
worker_class.DoStuff();
}https://stackoverflow.com/questions/33808931
复制相似问题