[英]How to specify an interface as a generic type argument when using reflection in C#?
I am trying to instantiate a type using reflection that takes a contract as a generic type parameter. 我正在尝试使用将合同作为通用类型参数的反射实例化一个类型。 If it was a generic type method, I could use the .MakeGenricMethod method, and specify the reflected type. 如果它是通用类型方法,则可以使用.MakeGenricMethod方法,并指定反射类型。 However, if the type itself is not generic, how would I go about specifying the interface as the contract? 但是,如果类型本身不是通用的,我该如何将接口指定为协定?
This is what the code would look like with the assemblies loaded normally: 这是正常加载程序集时的代码:
Ice.Lib.Framework.WCFServiceSupport.CreateImpl<Erp.Proxy.BO.JobEntryImpl>(EpicorSession, Epicor.ServiceModel.Channels.ImplBase<Erp.Contracts.JobEntrySvcContract>.UriPath);
Where I am stuck is at the "Epicor.ServiceModel.Channels.ImplBase" part. 我遇到的问题是在“ Epicor.ServiceModel.Channels.ImplBase”部分。
I need to specify that Erp.Contracts.JobEntrySvcContract interface when reflecting or the class won't instantiate properly. 我需要在反射时指定Erp.Contracts.JobEntrySvcContract接口,否则该类将无法正确实例化。 I then need to grab that .UriPath property and plug it into my CreateImpl method. 然后,我需要获取该.UriPath属性并将其插入到我的CreateImpl方法中。
Here's what I've got for that part: 这是我为那部分准备的:
Type _ImplBase = asmEpicorServiceModel.GetType("Epicor.ServiceModel.Channels.ImplBase");
FieldInfo UriPath = _ImplBase.GetField("UriPath", BindingFlags.GetField | BindingFlags.Public | BindingFlags.Static);
The problem is, _ImplBase is returning null, and I think this is because I didn't specify that Interface as the contract so it failed. 问题是_ImplBase返回null,我认为这是因为我没有将Interface指定为合同,所以它失败了。
public class ImplBase<TContract> : ImplBase where TContract : class
{
public static readonly string UriPath;
Eventually, I will need to grab that UriPath static property. 最终,我将需要获取该UriPath静态属性。
Thank you! 谢谢!
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.