Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug-only classes and resources in visual studio - is it possible?

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

like image 305
izb Avatar asked Jan 19 '10 11:01

izb


2 Answers

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.

like image 153
Simon P Stevens Avatar answered Sep 30 '22 23:09

Simon P Stevens


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

like image 44
Jon Skeet Avatar answered Oct 01 '22 00:10

Jon Skeet