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();
}
}
Run Code Online (Sandbox Code Playgroud)
到这里看起来不错。现在我想用工厂初始化类,但它无法返回 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 …Run Code Online (Sandbox Code Playgroud)