简体   繁体   中英

Single Visual Studio C# project, multiple DLLs versions?

We have one master project that creates a single DLL with FEATURE_1, FEATURE_2 and FEATURE_3 as three conditional compilation symbols that enable those respective features.

MyLib.dll => has FEATURE_1, FEATURE_2 and FEATURE_3 compiled in

We now wish to have the same master project spit out 3 different DLLs as follows:

MyLib.1.dll => has only FEATURE_1 compiled in
MyLib.2.dll => has only FEATURE_2 compiled in
MyLib.3.dll => has only FEATURE_3 compiled in

At present we build within VS2013 and those compile constants are defined inside the .csproj file (within the <DefineConstants> </DefineConstants> tags), which hard-codes them.

Is it possible to pass them via a command line so we can still maintain one master csproj but build the 3 different flavors in the RELEASE configuration just by changing the command line (eg: gcc's -D<buildFlag> style) ? The solution has other projects and they're designed to work with the RELEASE configuration. I'm also open to any other technique that is easy to use and maintain.

We're really trying to avoid creating pseudo-projects or affecting other projects in the solution (21 projects in the solution) - seems like an overkill/hackish for something very simple.

I haven't done anything with the command line, but to solve similar problems, I created separate projects (that define the Framework Target and any conditional compilation symbols), and then add all the project files as LINKED files. In this way, I only have to modify a single set of source files, but each project compiles into its own DLL.

To add a file to a project as a linked

  1. Right click the project and click Add Existing Item...
  2. Select the file (or files) you want to Add.
  3. Instead Of clicking the Add button, click the arrow next to the Add button, and click Add as Link .

I'm not sure if this will work for your case, but it sure has saved me lots of time when developing libraries for different .NET framework versions.

Is it possible that you have a complete turnaround?

Instead of defining all features in a single library and then disable some of them, you can create individual projects for each features, and then at packaging phase merge the smaller assemblies into a single one.

Microsoft has ILMerge, while ILRepack is an open source alternative,

https://www.nuget.org/packages/ilmerge

https://www.nuget.org/packages/ILRepack/

Then you are free of conditional compilation, which is too difficult to manage, and the complexity is moved to your packaging scripts, which can be easily managed and checked into source code management.

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