How do I stop my class library generating an .EXE file and causing a TypeLoadException Exception?

你离开我真会死。 提交于 2019-12-14 04:00:32

问题


I've got a really strange error occurring in my solution one of my projects which is a class library is causing a project it is referenced in to throw a TypeLoadException. I've looked through various answers on SO and the closest to my issue is;

TypeLoadException was unhandled in C# [closed]

This answer led me to question if this was my issue, doing some digging in the debug folders I found that my project that was referencing my class library is generating a DLL and EXE with the same name, could this be my problem?

If so how do I fix this?, I have other class libraries in my solution that are also referenced in this project they are not generating both DLL and EXE files.


回答1:


I don't believe it but I've actually found the cause via a colleague, hopefully this will help others who encounter this (nice one Adam).

The reason was my project referencing my class library had the same assembly name, I had been doing some re-jigging of my namespaces and hadn't noticed that my main project had the same name, this meant that when my code was built the compiler created a EXE for my main project and a DLL for my class library this in turn forced the TypeLoadException because the compiler had already loaded the EXE with the same name.

My code is now working. Thanks to all who took the time to post.




回答2:


my class library is generating a DLL and EXE with the same name

Yes, that causes this problem. You can easily see it by running Fuslogvw.exe and logging all bindings. It is a somewhat odd quirk of Fusion, but when it looks for an assembly then it only looks at the filename and ignores the extension. It will accept both a .dll and a .exe as an acceptable match for the assembly it is looking for. Which is otherwise somewhat logical, there is no true difference between DLLs and EXEs when they contain managed code. Referencing an EXE is for example entirely supported.

There's no way to force the loader to do this differently. The workaround should otherwise be simple, just be sure to generate a distinct assembly names.




回答3:


Under Project -> (YourProjectName) Properties -> Application, make sure that "Output Type" is set to "Class Library".



来源:https://stackoverflow.com/questions/13236646/how-do-i-stop-my-class-library-generating-an-exe-file-and-causing-a-typeloadexc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!