在我的項目中,我有以下幫助程序方法,該方法遍歷所有程序集並獲取BaseCamaFrom類型的子類的所有類型。在第二次嘗試反射時得到ReflectionTypeLoadException在.net中4
public static List<Type> GetAllTestActionFormTypes()
{
List<Type> types = new List<Type>();
// add all the types that are subclasses of BaseCamaForm to the _camaFormType list
foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
foreach (Type t in asm.GetTypes())
if (t.IsSubclassOf(typeof(BaseCamaForm)))
types.Add(t);
return types;
}
此方法在第一次調用時正常工作。但是,在調用此方法的第二次以下異常發生時,asm.GetTypes()
叫:
ReflectionTypeLoadException was unhandled by user code: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
在看LoaderException屬性我發現了一個System.IO.FileLoadException
以下消息:
Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.
爲什麼這段代碼在第一次被調用時工作,但第二次總是異常?
編輯:更多的調查之後,我引用的唯一組件具有的2.0.40607一個運行時版本是Microsoft.TeamFoundation.Client和Microsoft.TeamFoundation.VersionControl.Client。我無法弄清楚爲什麼這些引用會導致反射問題,也不知道爲什麼它只出現在第二次嘗試中。在嘗試對某些類使用
Activator.CreateInstance(types[x])
調用時,這似乎也偶爾會發生。
您能否澄清何時發生「第一次」與「第二次」呼叫?如果你連續兩次明確地調用它,那就表示出現了一個問題,而在兩次之間發生的一系列其他事情將會指示其他事情...... – jvenema 2010-08-25 15:29:44
當用戶單擊按鈕以從類型中檢索數據時,會調用它。第一個按鈕按下工作,但是當用戶第二次運行時,它失敗。由於它收到的信息,緩存不是一個選項 – KallDrexx 2010-08-25 15:36:25
另外,作爲一個附註,這是一個項目,它已被升級。 Net 3.5到4,但升級前不存在此功能 – KallDrexx 2010-08-25 15:39:12