Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to add a reference in a C# project depending on the value of a conditional compilation symbol (preprocessor constant)?

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?

like image 405
tunnuz Avatar asked Dec 29 '22 06:12

tunnuz


2 Answers

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.

like image 81
Preet Sangha Avatar answered Jan 24 '23 21:01

Preet Sangha


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'))">
like image 36
jsmars Avatar answered Jan 24 '23 20:01

jsmars