How do I stop my class library generating an .EXE

2019-08-08 08:50发布

问题:

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".