Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I test for compiler directives with an MSBuild Condition in a .csproj file?

I am totally new to the functions and conditions in .csproj files so any and all help is appreciated.

What I want to do is check for a specific compiler directive in the current configuration. An example would be something like the following:

<Choose>
    <When Condition= [current configuration has CONST-1 compiler constant defined] >
        ...
    </When>
    <When Condition= [current configuration has CONST-2 compiler constant defined] >
        ...
    </When>
</Choose>

I don't know if this is even possible or not. If there is a better way to do what I am asking let me know that as well. Either way, I want to test for a condition independent of the configuration.

EDIT

What I really want is a value that I can edit easily, preferrably within Visual Studio, that I can also check regargless of the configuraiton. I thought about compiler constants because you can easily change them in the Project Properties in VS.

like image 737
Mike Webb Avatar asked Oct 18 '12 14:10

Mike Webb


People also ask

Which of the following tag is used to specify the path of project in Msbuild?

For example, bin\Debug. Specifies the path to the output directory, relative to the project directory, for example, bin\Debug.

Are Csproj files XML?

CSPROJ files are are meant to be opened and edited in Microsoft Visual Studio (Windows, Mac) as part of Visual Studio projects. However, because CSPROJ files are XML files, you can open and edit them in any text or source code editor.

How do I edit a project in Visual Studio?

Then, right-click on the project and choose Edit <projectname>. You don't have to use solutions or projects in Visual Studio to edit, build, and debug code. You can simply open the folder that contains your source files in Visual Studio and start editing.


1 Answers

Compiler constants are set into a property "DefineConstants" so you should just be able to evaluate that property. Your Choose statement needs to go after the PropertyGroups that define the constants or inside a target.

<Choose>
    <When Condition="$(DefineConstants.Contains(CONST-1))">
        ...
    </When>
    <When Condition="$(DefineConstants.Contains(CONST-2))">
        ...
    </When>
</Choose>
like image 124
Ted Elliott Avatar answered Nov 16 '22 00:11

Ted Elliott