10

I've got an C# Project in Visual Studio, which has Console Application as Output Type.

But I also need a Class Library of this project for another solution.

Right now I have to switch the output type every time, but I wonder if it's possible to generate exe and dll at the same build-event?

Is there a post-build-event for this?

Harshith Reddy
  • 124
  • 2
  • 11

7 Answers7

12

To my knowledge there is no possibility to change the output type after compilation. That being said, if would be possible to have two projects like Console and Library in your solution, which would use the same source code files but have different output types. That way you would have different outputs without any duplication of code.

Codor
  • 17,447
  • 9
  • 29
  • 56
4

it is generally possible to reference a .net exe assembly as it would be a class-library.

So you can just stick in creating an exe file and reference the exe (sounds strange, but works) in your other project.

This is the dialog for browsing for references. As you see you can select exe files. Browse for Reference Dialog

But as commented it really depends on what your usecase is. I don't recommend to ship an exe with an entry point to your customer hoping that the customer does not discover the exe. But what you could do about that is to conditionaly compile your entry point.

For example

class Program {
  // This is the entry point of the EXE
  public static void Main() {
#if DEBUG
  // Start Debug Application
  ...
#else
  // Shipped to client - Entry point disabled
  return;
#endif
  }
}

If there is a relevant reason to have a shipped exe and a shipped class library, I would refactor your solution like this:

  • (A) complete application (.sln)
    • (B) console-application (.csproj) which has a reference to (C)
    • (C) class library project (.csproj)

With that it is perfectly clear to others that there is an application that uses the library and the library itself.

Peter Ittner
  • 551
  • 2
  • 13
  • 1
    Though works but it won't be recommended since generally you won't ship the exe to your clients. Rather the package together or only the consumable dll's – Rahul Aug 17 '16 at 07:17
  • 1
    I think this is the correct answer. An exe is an assembly which happens to have an entry point. – stijn Aug 17 '16 at 07:17
  • I might be mistaken, but couldn't you simply rename the extension of the executable to dll and have the client be none the wiser? It would then just "look" like a dll but have an entry point (though the entry point just returns immediately). – KDecker Aug 17 '16 at 13:16
2

Console Application is the type of your project. You can not change it.

What you can -and must- do is, carry your logic into a Class Library project and use your class library from any type of project you want.

Doruk
  • 884
  • 9
  • 25
  • 1
    Of course you can. Just look at your Solution Setting under the Tab `Application`. There is a Combo Box for Output type. – Smartis has left SO again Aug 17 '16 at 07:11
  • @Smartis, it's odd i never noticed it. Maybe because i never needed it. Why do you exactly need such functionality ? Why would you want to decide what to get(dll or exe) during build ? Maybe i can help if i knew. BTW, though Peter I's solution will work, it's better to seperate logic and ui into different projects. – Doruk Aug 17 '16 at 07:18
2

You should compile your project to become a dll and then use this dll in a console application.

A possibility to achieve what you want is to manually run the msbuild on your post-build event of your project.

See: How do i build a solution programatically in C#?

or Building C# Solution in Release mode using MsBuild.exe

Community
  • 1
  • 1
DaKirsche
  • 352
  • 1
  • 14
2

The usual solution for this is using a Solution with two projects:

  • a Class Library with all the code (which builds into a DLL)
  • an Console Application referencing the library whose Main just calls some function(s).

For more information, check the MSDN page on Solutions.

Codor suggested manually adding the files to the Console project, but one downside is that build settings are not shared between both versions, so you might get some inconsistency there.

0

I'm not really sure why people think it's not possible but it actually is.

The easiest way would be renaming the exe to dll Sounds stupid, I know. But it works in many cases. Also, as "Peter I" said a .NET exe can be imported as assembly in other projects. So you might not actually need a dll anyways.

Another way would be using C# command line as stated here: /out (C# Compiler Options)

You can use command command line options in Pre/Post build events Pre-build Event/Post-build Event Command Line Dialog Box

uTeisT
  • 2,256
  • 14
  • 26
0

I have a similar requirement and couldn't find any definite answer in this post or anywhere. I currently have a class library and would like to create a console application project without copying any code. Ideally speaking there should be two projects, one for creating a console application and another for creating a class library. And this is what the visual studio also suggests. When I tried to run the class library, I got the below message.

enter image description here

It clearly asks us to add an executable project to the solution and add the reference to the library project.

Below are the steps to do this.

Right click solution -> Add new project -> Console App -> choose name -> ok.

Right click on the console project -> add reference -> In reference manager, click on the projects tab and select the other project(In my case this is the class library project, In case it is not listed just click on browse and select the .csproj file) -> ok.

Now to use the classes in the other project, simple do using LibraryProjectNameSpace

There we are. Bingo!!!!

Also as mentioned in the other answers it is not possible to have the same project generate both .exe and .dll. But you can have the same solution generate these two guys by having two projects. In this way there is no need to switch the output of the project every time.

FYI, I use visual studio 2017

Community
  • 1
  • 1
HariUserX
  • 1,341
  • 1
  • 9
  • 17