简体   繁体   中英

Build same Project as Console and DLL

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?

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.

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. 浏览参考对话框

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.

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.

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

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.

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

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.

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM