2016-10-22 42 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

相關問題