Is it possible to add a class to a project in Visual Studio and have that class only built for the Debug configuration of the project? That is, it will not appear in the Release build at all.
If it i开发者_JAVA百科s possible, is it also possible to do the same for resources?
I'm thinking specifically about test classes that are only run in the Debug configuration but are removed from the assembly for release.
You can use #DEBUG (see Jon's answer) for classes.
For resources you can edit the MSBuild script file to include parts of the project conditionally based on the build mode selected.
The .csproj file is a XML MSBuild script, if you open it up in a text editor, you should find inside all of the parts of your project. If you can locate the parts you want to exclude from certain builds you can mark them with the Condition property. For example, to make a ItemGroup only be built for the Debug configuration you would do this:
<ItemGroup Condition=" '$(Configuration)' == 'Debug' " ...
You should be able to take a look though this and find the resources you want to exclude and add a similar Condition property to them, or to their parent group.
However, I would recommend that you use a separate assembly for test stuff and don't let it get mixed up with you main assemblies.
Classes are easy:
#if DEBUG
// Put your class here
#endif
Not sure about resources though... I suspect it's feasible by editing the project file by hand, but not in Visual Studio.
I wouldn't do this for test purposes though - I'd encourage you to use a separate assembly for tests. Aside from anything else, that means you can test what you ship, by testing against the release build. If you need access to internal types/members, you can always use [InternalsVisibleTo]
to grant internal access from your production assembly to your test assembly. Indeed, I suspect that's the most common use of the attribute :)
As for the resources, you indeed need to modify *.csproj by hand, changing
<EmbeddedResource Include="Components\AdmittanceChart.resx">
<DependentUpon>AdmittanceChart.cs</DependentUpon>
<SubType>Designer</SubType>
</EmbeddedResource>
to
<EmbeddedResource Condition=" '$(Configuration)|$(Platform)' == 'Debug' " Include="Components\AdmittanceChart.resx">
<DependentUpon>AdmittanceChart.cs</DependentUpon>
<SubType>Designer</SubType>
</EmbeddedResource>
but, as Jon suggested it's not necessarily good practice.
You can use Conditional attribute as well. This article describes Conditional Compilation
精彩评论