简体   繁体   中英

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.

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());

Target framework discrepancy:

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

I had this error, and the only thing that fixed it was installing Microsoft .NET Framework 4.5.2 (Offline Installer) https://www.microsoft.com/en-us/download/details.aspx?id=42642

I also installed SQL Server 2014 Client tools earlier, and this may also have had a positive impact by installing a newer msxml 6.0 version.

My error messsage was:

[A]xx.xxxx.ClassUserDS cannot be cast to [B]xx.xxxx.ClassUserDS. Type A originates from 'yyyyyy, Version=3.0.5617.31787, Culture=neutral, PublicKeyToken=657d68e01eb7c911' in the context 'Default' at location 'C:\\Users\\xxxxx\\Appdata\\Local\\assembly\\dl3\\QQ3VBMNC.EO1\\VYVECOB7.H5J\\2b6c89d2\\51158eea_9892d001\\yyyyyy.DLL'. Type B orginates from 'yyyyyy, Version=3.0.5617.31787' in the context 'LoadNeither' at location 'C:\\Users\\xxxxx\\Appdata\\Local\\Microsoft\\InfoPath\\FormCache4\\30083E69.D18\\cd264661b1055c58$b04f2786908cb791\\yyyyyy.dll

I tried looking in regedit HKCU\\Software\\Microsoft\\Fusion\\ that didn't help.

I tried resetting the office cache, that didn't help.

C:\Program Files (x86)\Microsoft Office\Office15\INFOPATH.EXE /cache clearall

or 64bit office 2013:
C:\Program Files\Microsoft Office\Office15\INFOPATH.EXE /cache clearall

or 64bit office 2010
C:\Program Files\Microsoft Office\Office14\INFOPATH.EXE /cache clearall

I tried resetting the fusion (GAC) cache, I had no idea how to do that (other than running ".NET Framework 1.1 Configuration" from the start menu to have a look around.)

This error happened when I renamed an aspx page in order to hide it.

In my case I've renamed a file named tutorials.aspx to tutorials-hidden.aspx. Publish was successful but when I loaded the URL there was error 500. Fixed by adding tutorials.aspx file then deleting it from the project. Possibly there was some definitions left in the csproj file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM