Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I Pass Compilation Constants to a Project Reference?

Tags:

c#

msbuild

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.

like image 209
JasonBock Avatar asked Aug 10 '17 20:08

JasonBock


People also ask

What are Project References?

A project reference is a link from the current Studio project to another project. The reference makes certain resources in the referenced project become available for use in the current project.


1 Answers

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).

like image 146
Martin Ullrich Avatar answered Sep 29 '22 22:09

Martin Ullrich