簡體   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