4

I am developing a library which can be compiled for two different technologies. Basically, the users of the library should be able to compile the solution either for the Unity3D game engine or the NeoAxis game engine. The problem is: while the library is ready for the conditional compilation (#if UNITY using ... #endif, etc.), I can't find a way to enable a set of references or the other depending on the conditional compilation symbols.

Is it possible to do it? If so, how?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
tunnuz
  • 23,338
  • 31
  • 90
  • 128

3 Answers3

7

Yes but you have to do this in the msbuild .csproj file. This file is essentially just list of data, such as references.

What you do is add a Condition statement to both References.

<Reference ..a.. Condition="'$LibToUse' =='NeoAxis'" />


<Reference ..b.. Condition="'$LibToUse' =='Unitv3D'" />

Then just define command line var called LibToUse with the desired value.

Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
2

How to add a reference by conditional compilation symbol only like you do in code. i.e. having or not having set for example UNITY in the project settings is answered in this post, and is simply done by editing your .csproj file, adding a Condition to the reference, and calling the DefineConstants.Contains() method like so:

<Reference Include="yourdll" Condition="$(DefineConstants.Contains('UNITY'))">
Community
  • 1
  • 1
jsmars
  • 1,640
  • 5
  • 21
  • 33
1

Go ahead and add all the necessary references. They will automatically be removed by the compiler if never used in your code which will be the case thanks to the #if/#endif. Another possibility is to provide two different .sln files referencing two different .csproj files pointing to the same source code but different references. There are many projects doing this: SomeProject_VS2008.sln and SomeProject_VS2010.sln.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Using multiple csproj files violates the DRY principle, maintenance headaches galore https://en.wikipedia.org/wiki/Don%27t_repeat_yourself – marknuzz Apr 04 '16 at 01:19