我正在尝试使用 TestProject 在 Visual Studio 中测试第三方 API。我收到错误。我在测试项目和业务逻辑项目中引用了第三个 API。现在我将测试数据从测试项目传递到业务逻辑项目,出现以下错误。
[A]TIMSS.API.User.UserDefinedInfo.UserDefinedCustomerTechnicalDisciplinees cannot be cast to [B]TIMSS.API.User.UserDefinedInfo.UserDefinedCustomerTechnicalDisciplinees. Type A originates from 'TIMSS.API.User, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadFrom' at location 'C:\Svad\Trunk\Source\EBusiness\EBusiness.Test\bin\bin\TIMSS.API.User.dll'. Type B originates from 'TIMSS.API.User, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'C:\Svadlakonda\Srikanth\Trunk\Source\EBusiness\EBusiness.Test\bin\TIMSS.API.User.dll'.
这里的问题是什么?我尝试将测试项目引用的 DLL 设置为 Copy Local
为 false
,反之亦然。
最佳答案
假设两个程序集(项目引用的一个和使用 LoadFrom 加载的一个)中的类型确实相同(或可转换),将“LoadFrom”程序集加载到应用程序域中应该可以解决问题。这可能不是线程安全的,但值得一试。
// This can cause "A cannot be cast to B" errors even using two exact copies of the same DLL.
var uncastableAssembly = Assembly.LoadFrom(filename);
// This shoud solve the casting issue but there still could be other issues.
var castableAssembly = AppDomain.CurrentDomain.Load(Assembly.LoadFrom(filename).GetName());
关于c# - A 类 DLL 不能转换为 B 类 DLL。 Type A originates.. from in the context LoadFrom,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29978888/