Sunday, May 27, 2018

Can I Pass Compilation Constants to a Project Reference?

Leave a Comment

If I have a <ProjectReference> reference, is there any way to pass a conditional compilation value to that project? Something like this (I know <DefineConstants> doesn't exist like this, it's just to illustrate the need):

<ProjectReference Include="..\DataProducer\DataProducer.csproj">   <DefineConstants>WAS_SET</DefineConstants> <ProjectReference> 

Therefore, if there's a class in that project like this:

    public sealed class ProduceValue     {         public string Produce()         { #if WAS_SET             return "WAS SET"; #else             return "NOTHING WAS SET"; #endif         }     } 

Then by passing that value during compilation or not, I could get different output.

1 Answers

Answers 1

The ProjectReference item allows adding the metadata fields Properties and UndefineProperties to allow maninpulating the set of "global" properties that are used to build project references. You could make use of this by passing in a global property to the referenced project like this:

<ProjectReference Include="..\DataProducer\DataProducer.csproj">   <Properties>DefineConstants=WAS_SET</Properties> <ProjectReference> 

The effect of this now being a global property to the referenced project is that it will override any definition/update of DefineConstants defined statically in the project - this may also include any added configuration constant (DEBUG).

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment