2016-10-22 80 views
0

我正在尝试使用将合同作为泛型类型参数的反射来实例化一个类型。如果它是泛型类型的方法,我可以使用.MakeGenricMethod方法,并指定反射类型。但是,如果类型本身不是通用的,那么我将如何指定接口作为合同?如何在C#中使用反射时将接口指定为泛型类型参数?

这是代码将是什么样子与组件正常加载:

Ice.Lib.Framework.WCFServiceSupport.CreateImpl<Erp.Proxy.BO.JobEntryImpl>(EpicorSession, Epicor.ServiceModel.Channels.ImplBase<Erp.Contracts.JobEntrySvcContract>.UriPath); 

我在哪里卡住处于“Epicor.ServiceModel.Channels.ImplBase”的一部分。

我需要在反射时指定Erp.Contracts.JobEntrySvcContract接口,否则该类将不会正确实例化。然后我需要抓住.UriPath属性并将其插入到我的CreateImpl方法中。

这就是我已经得到了该部分:

Type _ImplBase = asmEpicorServiceModel.GetType("Epicor.ServiceModel.Channels.ImplBase");  
      FieldInfo UriPath = _ImplBase.GetField("UriPath", BindingFlags.GetField | BindingFlags.Public | BindingFlags.Static); 

的问题是,_ImplBase是返回null,我想这是因为我没有指定接口的合同,因此失败。

public class ImplBase<TContract> : ImplBase where TContract : class 
{ 
    public static readonly string UriPath; 

最终,我将需要获取该UriPath静态属性。

谢谢!

回答

1
Type _ImplBase = Type.GetType("Epicor.ServiceModel.Channels.ImplBase`1[[Erp.Contracts.JobEntrySvcContract, Assembly2]], Assembly1"); 

假设Epicor.ServiceModel.Channels.ImplBase在Assembly1和Erp.Contracts.JobEntrySvcContract定义在Assembly2

+0

此语法对我来说很愚蠢......对于Assembly1和Assembly2,您是否希望程序集名称作为字符串或Assembly对象实例? – user3386553

+0

@ user3386553程序集名称。我认为这很明显,因为它们是传递给Type.GetType方法的字符串的一部分。 – airafr

+0

我尝试使用您的代码并连接程序集名称FullName属性,但它仍然返回空引用。 – user3386553