繁体   English   中英

在C#中使用反射时,如何将接口指定为通用类型参数?

[英]How to specify an interface as a generic type argument when using reflection in C#?

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

这是正常加载程序集时的代码:

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,我认为这是因为我没有将Interface指定为合同,所以它失败了。

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

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

谢谢!

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

假定在Assembly1中定义了Epicor.ServiceModel.Channels.ImplBase,并在Assembly2中定义了Erp.Contracts.JobEntrySvcContract。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM