Pop Quiz: Can you reference an exe assembly

0 comments suggest edit

With Visual Studio.NET 2005 and on, you can now reference an exe assembly just fine.

Can you reference an exe assembly? If you answer yes, you are correct. If you answer no, you may also be correct. It depends on which tool you are using. It turns out that VS.NET will not let you reference an exe assembly. However, you can reference an exe via the C# compiler using the /r switch.

This is quite problematic for me as I᾿m a firm believer in the benefits of test-driven development with unit tests (my tool of choice is NUnit). I like to have my unit tests in a separate class library from the code I᾿m testing, and have my UnitTest assembly reference the code I᾿m testing.

However, if I᾿m working on an exe, VS.NET won᾿t let me reference the exe. Thus, I either have to add my unit test fixtures to the exe and have the exe project reference the NUnit class libraries (which I am loathe to do), or move as much of the logic of the exe into an extraneous class library just so I can unit test it. Of course there is a third option which is to use Visual Notepad and the csc command line, but I᾿d lose a lot of productivity that way. Hopefully this is fixed in Whidbey.

For more info on test-driven development in C#, check out this MSDN article.

Found a typo or error? Suggest an edit! If accepted, your contribution is listed automatically here.



4 responses

  1. Avatar for Ian Griffiths
    Ian Griffiths April 28th, 2004

    It is possible to fool VS.NET in to referencing an EXE by editing the .csproj file by hand. However, it's not a technique likely to inspire confidence... I don't tend to use it.

    Instead I usually structure my projects specifically to allow for testing. I build all code into DLLs so that they can be referenced from test projects.

    You use VS.NET's ability to 'link' files into a project to make a file appear in more than one project, so you can build both EXEs and DLLs from the same source if you want to.

  2. Avatar for nospamplease75@yahoo.com (Haac
    nospamplease75@yahoo.com (Haac April 28th, 2004

    I tried to do just that, but it wouldn't compile. I must not have made the correct edit.

  3. Avatar for John Lanpheer
    John Lanpheer April 6th, 2005

    I realize this is an old thread, but maybe someone will see it.

    One alternative for some special cases: If your classes implement an interface defined in a separate DLL, you can create the class via remoting, and call the methods through the interface. I haven't tried it, but it should work...

    Ian, if you see this, my best to Glyn.

  4. Avatar for Amiya Mohanty
    Amiya Mohanty May 21st, 2008

    I have modified the .Net Assembly FileName.EXE to FileName.DLL and it can be refernced in the VS .NET solution.
    I am happy. :)