Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# using statement inside #if DEBUG but not ship assembly

Tags:

c#

I have a feature that I only want to happen on Debug, but do not want to ship the dll's that this feature requires. Is that possible to do?

I have:

#if DEBUG
using MyAssembly;
#endif

Of course MyAssembly is being referenced by the project. I would like MyAssembly.dll to not be shipped on a release mode. Can that be achieved? Will using Conditional("DEBUG") help in this regard?

like image 634
Bati Avatar asked Jun 14 '17 22:06

Bati


2 Answers

References that aren't required are usually removed automatically by the compiler, however: you can be more explicit by changing the csproj to include a Condition on the PropertyGroup. Something like:

<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
  <Reference Include="YourReference" />
</PropertyGroup>

(it could also be a <PackageReference> etc)

like image 67
Marc Gravell Avatar answered Nov 13 '22 15:11

Marc Gravell


It's perfectly fine to put a using directive in an #if DEBUG section, and it will remove that directive when compiled for debug.

However, that's only part of the story; it won't accomplish your objective by itself. The Solution Explorer in Visual Studio also has a References section. You would need to remove the reference for the assembly, as well, or it will still be included when you build.

I don't recall anything in the Visual Studio user interface that will let you do this, but I expect it should be possible somehow if you manually edit the Project file (it's just an MSBuild file). Personally, I try very hard to avoid doing things that require manual edits to the project files. Visual Studio wants to be able to own these files, and you can end up creating conflicts, where you and Visual Studio overwrite each other's changes.

like image 23
Joel Coehoorn Avatar answered Nov 13 '22 14:11

Joel Coehoorn