Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type A DLL cannot be cast to type B DLL. Type A originates.. from in the context LoadFrom

I am trying to test third pary API in visual studio using TestProject. I am getting error. I have referenced 3rd API in Test Project as well as in Business Logic project. Now I am passing test data from Test project to Business logic project getting bellow error.

[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'.

What is the the problem here? I tried making Test Project referenced DLL to Copy Local to false and vice versa still did not work.

like image 727
James123 Avatar asked Apr 30 '15 22:04

James123


2 Answers

Assuming the types are really the same (or castable) in both assemblies (the one referenced by the project and the one loaded using LoadFrom), loading the "LoadFrom" assembly into the application domain should fix the problem. This probably isn't thread safe but it's worth trying.

// 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());
like image 169
CaymanRich Avatar answered Sep 21 '22 04:09

CaymanRich


Target framework discrepancy:

My error was caused by loading an assembly with a target framework version (e.g 4.5.2) lower than another that it was referencing (e.g 4.6.1)

like image 22
Declan Taylor Avatar answered Sep 22 '22 04:09

Declan Taylor