Dot-Net

忽略傳遞給 Type.GetType() 的程序集限定名稱中的版本

  • January 19, 2022

當傳入的程序集限定名稱指定與實際載入的 DLL 版本不同時,是否可以Type通過?如果是這樣, 的行為是什麼?Type.GetType()``GetType()``Version``GetType()

Type無論程序集是什麼版本,我都想從程序集中獲取一個。我有一個函式,它獲取程序集限定名稱作為參數:

Type someType = Type.GetType(someName);

someName值對應於Type我想要獲取的值,但它可能與Version我的應用程序中載入的值不同。

在測試中,我發現即使目前載入的程序集的版本與程序集限定名稱欄位GetType()中的值不匹配,它也會返回正確的類型。Version

我已經成功使用了這個:

Type type = Type.GetType(typeName, AssemblyResolver, null);

private static System.Reflection.Assembly AssemblyResolver(System.Reflection.AssemblyName assemblyName)
{
   assemblyName.Version = null;
   return System.Reflection.Assembly.Load(assemblyName);
}

引用自:https://stackoverflow.com/questions/1159354