加载外部 dll 时出现 ReflectionTypeLoadException
ReflectionTypeLoadException when loading external dll
假设我有一些项目的解决方案。在项目 Proj1.BL
中,我有摘要 class MyAbstractClass
,并且( 强要求)我应该只在另一个解决方案中继承它。它们都将只包含一个 class(例如 DerivedClass : MyAbstractClass
),将引用第一个解决方案中的相应项目(Proj1.BL
、Proj1.DataModel
等),并且将是编译为 DLL(Class 库)。最后,这些 DLL(另一个强需求)将存储在 $(OutDir of first solution)\Externals
.
我的问题是:我应该如何在第一个解决方案中正确加载这些程序集?我试过这样的方法:
public static Type LoadType(string fileName)
{
if (fileName == null) throw new ArgumentNullException("fileName");
fileName = Path.Combine("Externals", fileName);
if (!File.Exists(fileName)) throw new ApplicationException("File does not exist.");
return
Assembly.LoadFrom(fileName)
.GetTypes()
.SingleOrDefault(_ => _.IsSubclassOf(typeof (MyAbstractClass)));
}
但我会继续得到
ReflectionTypeLoadException. Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
LoaderException
是关于 CLR 无法从 Proj1.BL, Version=*.*.*.*, Culture=neutral, PublicKeyToken=null
.
加载类型 Proj1.BL.MyAbstractClass
但是我 运行 的代码在 Proj1.BL
。 怎么了?
感谢您的任何建议。
咦,答案比想象中简单。当 Proj1
个程序集的版本值在每次重建后自动递增时存在版本不匹配,但 DLL 期望找到 Proj1
个在编译时指定的程序集的版本。
假设我有一些项目的解决方案。在项目 Proj1.BL
中,我有摘要 class MyAbstractClass
,并且( 强要求)我应该只在另一个解决方案中继承它。它们都将只包含一个 class(例如 DerivedClass : MyAbstractClass
),将引用第一个解决方案中的相应项目(Proj1.BL
、Proj1.DataModel
等),并且将是编译为 DLL(Class 库)。最后,这些 DLL(另一个强需求)将存储在 $(OutDir of first solution)\Externals
.
我的问题是:我应该如何在第一个解决方案中正确加载这些程序集?我试过这样的方法:
public static Type LoadType(string fileName)
{
if (fileName == null) throw new ArgumentNullException("fileName");
fileName = Path.Combine("Externals", fileName);
if (!File.Exists(fileName)) throw new ApplicationException("File does not exist.");
return
Assembly.LoadFrom(fileName)
.GetTypes()
.SingleOrDefault(_ => _.IsSubclassOf(typeof (MyAbstractClass)));
}
但我会继续得到
ReflectionTypeLoadException. Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
LoaderException
是关于 CLR 无法从 Proj1.BL, Version=*.*.*.*, Culture=neutral, PublicKeyToken=null
.
Proj1.BL.MyAbstractClass
但是我 运行 的代码在 Proj1.BL
。 怎么了?
感谢您的任何建议。
咦,答案比想象中简单。当 Proj1
个程序集的版本值在每次重建后自动递增时存在版本不匹配,但 DLL 期望找到 Proj1
个在编译时指定的程序集的版本。