c# - A 类 DLL 不能转换为 B 类 DLL。 Type A originates.. from in the context LoadFrom

标签 c# .net visual-studio visual-studio-2012

我正在尝试使用 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 Localfalse,反之亦然。

最佳答案

假设两个程序集(项目引用的一个和使用 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/

相关文章:

c++ - 为什么 auto_ptr 似乎违反了 Visual C++ 上的私有(private)继承?

c# - Facebook C# SDK : An active access token must be used to query information about the current user

.net - .NET 中如何实现值类型

c# - 异步方法和竞争条件

c++ - 我什么时候需要 MSVCRT 可再发行组件?

c# - C# 应用程序的图像监视

javascript - 传递参数日期时间?在 mvc 和 jquery 中

c# - 尝试从另一个线程访问复杂对象时出现 InvalidOperationException

c# - 为什么抽象类可以有构造函数?

c# - 如何使用 Compact Framework 加粗文本