How to define conditional compilation symbols in separate file (not .csproj or app.config)

守給你的承諾、 提交于 2019-12-21 04:50:53

问题


We need to define a conditional compilation symbol in a class library project. This should not be checked in the source control (it doesn't apply to all developers), so it should be defined in someplace other than the .csproj or the app.config file. How can this be achieved?


回答1:


I would define your various build types in the configuration manager (menu BuildConfiguration Manager) and set up each of the required constants on each of the build types.

You can then have each member of the team simply select the build type they want to do, and it will automatically use the appropriate constants. (I think that the most recently used build type is stored in the .suo file, which is "solution user options" and you wouldn't normally check in to your source control, so this would be maintained specifically for each user).

You can define pre-processor constants on the C# compiler command line using the /define switch, but you will have the problem of how to call this. Any changes you make to the project properties to use this will be saved in the .csproj file. You would have to do all of your building from the command line instead which I'm sure you won't want. You can also define them in MSBuild scripts, but you would have the same problem.




回答2:


This works:

It turns out you can define conditional symbols in the csproj.user file. I assume the same thing would work for other languages, but I haven't tried.

So just add something like the following to the csproj.user file.

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DefineConstants>MySpecialConstant, TestBlahBlah</DefineConstants>
</PropertyGroup>

I actually like @SimonPStevens solution better because it embraces TFS, instead of hiding. Sometimes, however, this is just easier...



来源:https://stackoverflow.com/questions/1221343/how-to-define-conditional-compilation-symbols-in-separate-file-not-csproj-or-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!