2012-06-13 104 views
3

好吧,所以這裏是我的問題:如何通過類型名稱獲取通用接口的類型?

我有一個xml文件,有一個方法和它的參數記錄。此XML文件記錄的ID值的列表,下面的.NET泛型接口類型,名稱:

System.Collections.Generic.IList`1[CoreLib.Domain.MyClass] 

我知道大多數這些參數都將成爲泛型列表或字典。當我試圖使用的GetType的字符串值我從XML閱讀,這一切都返回null,如果我嘗試將拋出異常標誌設置爲true,它拋出以下消息:

「莫非從 程序集'TestLibrary,版本= 1.0.0.0,Culture = neutral, PublicKeyToken = null'。「不加載類型'CoreLib.Domain.MyClass'

什麼是最好的策略獲得的東西,我可以拉回我可以填充的實際類型?先謝謝你!

UPDATE我試圖組裝引用添加到字符串名稱類似如下:

Type.GetType("System.Collections.Generic.List`1[CoreLib.Domain.MyClass,CoreLibrary]") 

coreLibAssembly.GetType("System.Collections.Generic.List`1[CoreLib.Domain.MyClass,CoreLibrary]") 

兩個結果在一個空的對象,如果我申請豁免被拋出,如果我沒有指定程序集名稱,我得到了相同的信息。我也嘗試使用Assembly Binding Log Viewer,但沒有應用程序出現在列表中,並且在我運行我的代碼(它在nunit正在執行的測試項目中)後,應用程序中似乎沒有出現任何內容。

有什麼想法?

+0

我也試圖在通用的T定義中包含程序集的全名,但仍然沒有運氣。 –

回答

0

我已經想通了。問題在於如何引用程序集,以及GetType方法如何確定程序集在哪裏。我最終解決這一問題與該的GetType匿名Assembly Resolver代表:

result = Type.GetType(typeName + ",mscorlib", //<-- needing to identify the mscorlib for generics! 
     //this anonymous delegate method is used by the GetType Framework to identify and return the correct assembly for the given assembly name. 
     //most of this is default, except to handle the mscorlib library 
     delegate(AssemblyName potentialAssembly) 
     { 
      if (potentialAssembly.Name == coreLibAssembly.FullName) 
       return coreLibAssembly; // this was never called, I had to check the namespace within the rest of my code 
      else if (potentialAssembly != null) 
       return Assembly.Load(potentialAssembly); 
      else 
       return null; 
     }, 
     //this anonymous delegate is used to return the type specific to the assembly. this method is called for each nested generic, 
     //so we don't have to parse the type string name by hand. 
     delegate(Assembly potentialAssembly, string inputTypeName, bool ignoreCase) 
     { 
      if (inputTypeName.StartsWith("CoreLib.Domain")) 
       return coreLibAssembly.GetType(inputTypeName, true, ignoreCase); 
      else if (potentialAssembly != null) 
       return potentialAssembly.GetType(inputTypeName, true, ignoreCase); 
      else 
       return Type.GetType(inputTypeName, true, ignoreCase); 
     }, true); 

現在,如果通用Type.GetType(字符串的typeName)不工作,我用這個代碼通過類型的字符串進行解析,並將程序集與給定的typeName字符串變量中的相應類型名稱進行匹配。

1

您一般可以將字符串傳遞到Type.GetType()中。例如:

Type t = Type.GetType("System.Collections.Generic.Dictionary`2[System.String,System.Int32]"); 

我在你的情況疑,CLR不知道如何解決的類型CoreLib.Domain.MyClass。您可能需要幫助它沿着指定的裝配,如本例taken from MSDN

Type.GetType("System.Collections.Generic.Dictionary`2[System.String,[MyType,MyAssembly]]") 

如果指定它仍然是組裝後「炸燬」 (下一次,建議你定義好如與特定錯誤,異常或堆棧跟蹤:-)嘗試運行Fusion Log Viewer以管理員(否則它以靜默方式失敗)來記錄綁定失敗。

+0

我試過這兩個,我一直遇到裝配問題:一,集合名稱空間在mscorlib中,所以我不能使用Assembly.GetType ... ,如果我嘗試Type.GetType與指定的程序集名稱,它將忽略給定的程序集名稱,並仍在我的測試庫中查找該對象。 (抱歉,所有的意見,我真的需要得到這個工作!) –

+0

這實際上沒有奏效。發生了什麼事情是,Type.GetType雖然在泛型中的程序集名稱是另一個類,並試圖將其識別爲類型。 –

+0

我認爲你可能需要一組額外的[]。你沒有做到MSDN顯示的內容。嘗試.... List [1] [[CoreLib.Domain.MyClass,CoreLibrary]] –

相關問題