我正在使用ChannelFactory编写WCF程序,该工具需要指定一个类型才能调用CreateChannel方法。例如:
在我的情况下,我正在进行路由,因此我不知道我的通道工厂将使用哪种类型。我可以解析消息头以确定类型,但我遇到了一个难题,即使我有Type的实例,我也无法将其传递给ChannelFactory期望的泛型类型。
换句话说,我试图做的事情是类似于这样:
IProxy proxy = ChannelFactory<IProxy>.CreateChannel(...);
在我的情况下,我正在进行路由,因此我不知道我的通道工厂将使用哪种类型。我可以解析消息头以确定类型,但我遇到了一个难题,即使我有Type的实例,我也无法将其传递给ChannelFactory期望的泛型类型。
换句话说,我试图做的事情是类似于这样:
string listtype = Console.ReadLine(); // say "System.Int32"
Type t = Type.GetType( listtype);
List<t> myIntegers = new List<>(); // does not compile, expects a "type"
List<typeof(t)> myIntegers = new List<typeof(t)>(); // interesting - type must resolve at compile time?
有没有在C#中可以利用的方法来解决这个问题?