C# 泛型对象的创建模式

C# 泛型对象的创建模式,c#,asp.net-core,design-patterns,asp.net-core-2.1,C#,Asp.net Core,Design Patterns,Asp.net Core 2.1,在以下场景中,请有人提供返回具体实现的最佳方法。假设我有: public interface IThing<TInput> where TInput : RequestBase { string Process(T input); } 公共接口,其中TInput:RequestBase { 字符串处理(T输入); } 然后是多个实现: public class Thing1<T> : IThing<T> where T : ReqThing1 pu

在以下场景中,请有人提供返回具体实现的最佳方法。假设我有:

public interface IThing<TInput> where TInput : RequestBase
{
    string Process(T input);
}
公共接口,其中TInput:RequestBase
{
字符串处理(T输入);
}
然后是多个实现:

public class Thing1<T> : IThing<T> where T : ReqThing1
public class Thing2<T> : IThing<T> where T : ReqThing2
public class Thing1:i其中T:ReqThing1
公共类内容2:I其中T:ReqThing2

在我的调用类中,包装这些类的构造并以一种干净、可测试的方式返回我想要的信息的最佳方式是什么?谢谢

我不太明白你想要什么,但我有个主意:

public abstract class RequestBase
{
}

public class ReqThing1 : RequestBase
{
}

public class ReqThing2 : RequestBase
{
}

public interface IThing<T> where T : RequestBase
{
    string Process(T input);
}

public class Thing1 : IThing<ReqThing1>
{
    public string Process(ReqThing1 input)
    {
        throw new System.NotImplementedException();
    }
}

public class Thing2 : IThing<ReqThing2>
{
    public string Process(ReqThing2 input)
    {
        throw new System.NotImplementedException();
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        var thing1 = new Thing1();
        var thing2 = new Thing2();
    }
}
公共抽象类RequestBase
{
}
公共类ReqThing1:RequestBase
{
}
公共类ReqThing2:RequestBase
{
}
公共接口i,其中T:RequestBase
{
字符串处理(T输入);
}
公共类物品1:我的物品
{
公共字符串处理(请求1输入)
{
抛出新系统。NotImplementedException();
}
}
公共类的东西2:我的
{
公共字符串处理(请求2输入)
{
抛出新系统。NotImplementedException();
}
}
公共课程
{
公共静态void Main(字符串[]args)
{
var thing1=新thing1();
var thing2=新thing2();
}
}