38

I have one simple question.

I know that I can reference an .net executable file in my C# project.

I don't want to make unnecessary project with "Output Type: Windows Application" just to call some dlls.

I just want to know is it OK or is it a bad practice to refrence an exe file?

7
  • 1
    Even if you reference one, what do you do with that exec file? Apr 3, 2014 at 11:06
  • 1
    What do you mean by make unnecessary project with "Output Type: Windows Application"? Are you including the exe file as a part of your project, and outputting it when compiling the project?
    – Kjartan
    Apr 3, 2014 at 11:09
  • 4
    I think he means, he's referencing a exe project type to access some functionality. He could refactor this into a separate class library but is it necessary?
    – Liam
    Apr 3, 2014 at 11:10
  • 1
    Hi. I have a project which compiles into exe file, because I need it to be an application. I have some classes in it that I want to use in other projects, so that's why I want to refrence it. Apr 3, 2014 at 11:13
  • 1
    @NikhilAgrawal If types are public you don't need a refection, it can be used as if they are in class libraries, else you need reflection. Apr 3, 2014 at 11:21

5 Answers 5

39

Yes, this can be seen as a bad practice for the following reasons:

  • Bad Project Architecture
    If you need to call some logic from an .exe, then that logic is incorrectly placed there. Instead, you should put it in a separate dll and reference that same .dll from both the executable you reference currently, and the application that references the executable. As suggested in comments below, extracting the logic into a library can help you avoid some CPU architecture limitations, which I will describe in my next point, as the library can be built to target any CPU.

  • Architecture Limitations
    The referenced executable might have been built to address optimally 32 bit or 64 bit machines, or even specific CPUs (like Itanium). A library can be built without these specifications1 in order to be cross-CPU-compatible, and thus be referenced by any project later. If you reference an executable with specific architecture settings, you should use compatible settings to the referencing project. That I consider a limitation, as you would be unable to distribute the final product to certain platforms.

  • Making Unit-Testing Difficult.
    As hinted by Abel in the comments, your unit tests will go into their own DLL and they would need to reference the executable as well. It could be hard to test it if you do not expose some internal methods/fields using the InternalsVisibleTo attribute, or use reflection (which is the slow alternative) to check and assert some non-publicly visible state of your objects. The executables may not be build with the InternalsVisibleTo attribute set, and if you fallback to reflection, you could encounter .NET security issues preventing you to reflect members of the executable (because the test suite was executed within a more restrictive setup, for instance).

    You will also encounter the architecture limitations mentioned above, which will result in using the same architecture for your unit tests. It could be a problem if your test suites are executed on a remote machine, as part of an automated build (such as in TravisCI, Bamboo, TeamCity and etc). The CI agent must then comply with the CPU architecture of the executable and the test suite. If there is no suitable agents, no tests could be ran. In addition, if you are using a public CI platform for building your application and executing the tests, this could count as distribution of the executable in legal sense. You might well get into violating the executable's license -- see the next section for more details.

  • Potential Licensing Issues
    You should carefully distribute your application. If the referenced executable needs additional licenses or fees in order to be used, you will have to enforce the users to accept that executable's license alongside the one of your application (and pay for it if needed), otherwise you risk of making an illegal distribution of it with your software. This also implies that you have the right to reference the executable in the first place.

  • Unknown Consequences
    The executable will be copied within the bin folder and installed alongside your application. There is no telling what could happen if someone browses the bin folder and executes it. There are a few problems with that:

    • The executable crashes, or misbehaves because of improper input. Usually this happens if it does not have any GUI (for instance if a command-line program is double-clicked by user it will not get any input in the form of command-line arguments and thus crash, or misbehave).

    • The executable is not intended to be used by the owner of your program, as that would legally or logically contradict to what your software does.

Yet, there are some cases where referencing an executable can be justified, but those are rare enough:

  • The executable comes from a 3rd party, and no library with the same functionality exists, and there is no other way to link to that functionality. It also might be explicit requirement for your project established by your employer or client.
  • The executable is written in another language and you need to communicate with it via interop.

As long as the latter do not apply to you, and especially if you develop the executable that is referenced yourself, I would definitely recommend to extract the needed logic to a separate library.


1 In fact you can also build an executable to target any CPU, as mentioned by Dominic Kexel's comment. The opposite is also possible - to build a library for specific CPU, but it is less common, as the executable is usually the one being tailored to the hardware. So, to clarify my points, I had in mind referencing a 3rd party executable, or one that cannot be rebuilt for other reasons, and that executable is already optimized for some specific architecture. If you can rebuild and change that executables' targeted CPU, then you can definitely extract the needed logic into a dll.

9
  • 4
    The executable might be built to address optimally 32bit or 64bit architecture. A library can be built without these specs You can build an exectuable with AnyCPU, too. If you mean something else, could you elaborate on how to build an executable to address optimally 32bit or 64bit architecture?
    – sloth
    Apr 3, 2014 at 11:31
  • 1
    @DominicKexel, true. But, this is if you have control over how the referenced executable is being built. If so, you also have the power to extract the logic into a dll and avoid referencing the executable at all. I will edit my post to clarify this matter as it may seem ambiguous now. Apr 3, 2014 at 11:33
  • 1
    You should add another example to the Architecture Limitations part: Maybe your executable has to be 32- or 64-bit because that executable uses a native library, and you don't want your second executable be platform specific (you want it AnyCpu), then you have to extract the relevant code from your first exectable into a seperate library.
    – sloth
    Apr 3, 2014 at 11:49
  • 1
    @DominicKexel, yes, that is also valid point. I edited already, but added this to my first point. Apr 3, 2014 at 11:52
  • 3
    Good answer, but I think you're missing one very important use-case that almost every project encounters: unit testing. Usually, unit tests go into their own DLL. The natural way to create unit tests for your executable is to link to that executable and write tests for (public/protected/internal-with-internalsVisibleTo) methods, properties etc in the unit test DLL.
    – Abel
    Sep 24, 2017 at 1:32
9

If you are including an executable file as a resource in your project, then I suppose it's no big deal if it solves your problem and works (although in theory, it would seem more correct to extract common logic out into a separate .dll which could be used in several projects).

However: You might want to include that .exe as an embedded resource, so that it is not visible directly in the output directory when you build your project:

Right click the project node and select Add > Existing Item and find the .exe file. Now right click it in the Solution Explorer, select properties and set Build Action to Embedded Resource.

The file will be "baked into" your own .dll or .exe or whatever you are building, instead of simply be copied to your output directory.

3
  • 2
    +1. I like the idea for the embedded resource. This can really fix a lot of potential issues with the deployment and unwanted user actions (if the users runs that executable). The price will be some boilerplate code for getting to the executable's logic - instead of referencing as one would do with a dll, so some reflection stuff will be needed, as well as temporary directory space to extract the exe. Apr 3, 2014 at 11:29
  • 1
    @IvayloSlavov - do you have any working example on this?
    – FrenkyB
    Aug 30, 2017 at 18:24
  • @FrenkyB, in fact, not with executables. I have tried loading other dll (assemblies) embedded into another assembly, but that code is not available with me at the moment. If you are interested I could make a gist for you. Sep 1, 2017 at 13:09
4

.NET DLL or EXE, both are assemblies, you could use either exe or dll by referencing them. There is no problem in shipping exe with your code, until unless you don't want to get this exe executed separately.

2
  • Is it possible to call methods in this exe from another project (which references exe)?
    – FrenkyB
    Aug 30, 2017 at 18:22
  • @FrenkyB it IS possible to call methods in the exe from another project (I just tried calling a static method in the EXE from an xUnit project which produces a DLL). You must be careful however because some code may fail if it assumes the Main() method was executed (which it isn't if you reference the EXE and just call any public method you want).
    – Eric Mutta
    May 7, 2021 at 2:06
3

My answer to this question is far from an opinion.
Being able to reference a .NET executable assembly as a library is one of the features of .NET and CLI design. This feature saves you from creating a separate project and then moving and reworking your logic there.
If you are worried about the way your project looks when shipping it, then ILMerge is your friend. And also if you are referencing a strictly licensed 3rd-party library then you can tell ILMerge to skip it during the merging process.
BUT as @Ivaylo Slavov pointed out if your project is architecture-dependent (x86/x64) then you have to go the hard way.

1

It is ok, if it helps you to ship product in time.

It's not ok on long run. So may be just leave yourself a note, to revise it as soon as you will have necessary time to do it better.

But first, if you don't have enough time for that now, it's ok: make it work and ship it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.