c# - Visual Studio conditional project reference based on a constant -
for user authorization, want include specific module each user. configured conditional compilation this
<defineconstants>trace;debug;sampleconstant1</defineconstants> and edited project file this:
<projectreference include="..\solution1.modules.module1\solution1.modules.module1.csproj" condition="$(defineconstants.contains('sampleconstant1'))"> <project>{4e378bd0-4ff8-4160-9331-1ecbfd2b6f30}</project> <name>solution1.modules.module1</name> </projectreference> for case want add reference project module1 if defineconstants contains sampleconstant1; no matter put in defineconstants, solution loads module1 project. did wrong here?
update: code correct. please see j0e3gan's answer. visual studio ui not reflect conditional references within references folder of project. therefore references visible in given configuration or platform selection. compiler , intellisense on other hand aware of conditional references, honoring correct settings both visual feedback , error notification during builds.
i suspect problem conditioning project reference module1, not whether include module1 in solution.
including project in solution (and hence loading solution) , project referencing project in solution 2 different things of course.
update:
if want condition project reference, joe wrobel wrote a related blog post should help. key takeaway wrap itemgroup contains projectreference condition in choose element - example:
<choose> <when condition="$(defineconstants.contains('sampleconstant1'))"> <itemgroup> <projectreference include="..\solution1.modules.module1\solution1.modules.module1.csproj"> <project>{4e378bd0-4ff8-4160-9331-1ecbfd2b6f30}</project> <name>solution1.modules.module1</name> </projectreference> <!-- other projectreference elements --> </itemgroup> </when> <otherwise> <itemgroup> <!-- other projectreference elements --> </itemgroup> </otherwise> </choose> from tests evening, works great condition project reference(s) on whether constant sampleconstant1 defined. however, note conditioned project references do not show in solution explorer under (would-be) referencing project's references folder - regardless whether conditioning constant defined.
to see conditioning worked, had build: with sampleconstant1 defined, referencing project built successfully while using class defined in module1 - expected; , without sampleconstant1 defined, referencing project failed build because class defined in module1 not resolved - expected.
Comments
Post a Comment