【发布时间】:2018-02-09 14:11:04
【问题描述】:
public interface IRequestProcessor<out T>
{
T Translate(string caseId);
}
public class xyzReqProcessor : IRequestProcessor<xyzType>
{
public xyzType Process(string xyzMsg)
{
return new xyz();
}
}
public class VHDReqProcessor : IRequestProcessor<VHDType>
{
public VHDType Process(string xyzMsg)
{
return new VHD();
}
}
直到这里看起来不错。 现在我想用工厂初始化类,但它不能返回 IRequestProcessor 类型的对象。
public static IRequestProcessor Get(FormType translatorType)
{
IRequestProcessor retValue = null;
switch (translatorType)
{
case EFormType.VHD:
retValue = new VHDProcessor();
break;
case EFormType.XYZ:
retValue = new XYZProcessor();
break;
}
if (retValue == null)
throw new Exception("No Request processor found");
return retValue;
}
在调用 Factory.Get(FormType translateType) 方法时,我不想指定任何固定对象类型,如下所示
Factory.Get
(FormType translateType)
【问题讨论】:
-
你可以声明它返回
IRequestProcessor<object>。但是调用者必须转换调用Translate(或Process;问题不一致)的结果。 -
如果在编译时不知道类型,就不能使用强类型。
-
@JonSkeet :不,如果我想使用 Object,那么我可以简单地使用它而无需泛型。
-
那么,您如何期望从仅在 执行 时才知道的值返回 编译时安全 值?如果我打电话给
FormType type = GetFormTypeFromSomewhere(); var processor = Factory.Get(type);,你希望processor的compile-time 类型是什么? -
但是帖子中没有显示 IRequestProcessor 类型......并且帖子中类型之间唯一共同的基本类型是'object'
标签: c# .net generics interface factory-pattern