简体   繁体   English

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

[英]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.

相关问题 如何使用反射C#获取构造函数泛型参数类型 - How to get Constructor Generic parameter type using reflection c# C# 使用反射从方法返回类型的泛型参数中检测可为空的引用类型 - C# detect nullable reference type from generic argument of method's return type using reflection 如何使用反射来查找开放泛型类型上的哪个开放泛型参数实现泛型接口? - How can I use reflection to find which open generic argument on an open generic type implements a generic interface? C#-从通用接口指定结构字段类型 - C# - Specify a struct field type from a generic interface 当必需的参数为null时,如何在通用方法中不指定类型参数? - How to not specify the type argument in a generic method when the required argument is null? 如何使用反射确定已实现接口的通用类型? - How do I determine the generic type of an implemented interface using reflection? 具有泛型类型参数的C#构造函数与接口结合 - C# Constructor with argument on generic type combine with interface C#:如何强制将通用类型作为接口? - C#: How to force a generic type to be an interface? 使用 c# 反射时无法创建类型为 System.Fabric.FabricServiceNotFoundException 的泛型类 - Cannot make a Generic Class of Type System.Fabric.FabricServiceNotFoundException when using c# reflection C#:使用反射从通用类型创建元组 - C# : Create a Tuple from a generic type using reflection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM