Conditional Compilation in Referenced Assemblies
Asked Answered
R

3

7

I'm writing an assembly with some conditionally compiled members in it, e.g.:

[Conditional("DEBUG")]
public static void Log(string message) { /*...*/ }

And using it like so:

public void DoStuff() {
    Log("This will only appear on debug builds");
    /* ... Do stuff ... */
}

But when I give this assembly to someone to use in their project, I want them to be able to define whether or not DEBUG-conditional members are compiled.

If that's not possible (e.g. the methods are just completely removed at compile-time), then is there any way to package multiple 'configurations' of an assembly (e.g. maybe with [AssemblyConfiguration]) and select them according to the configuration of the referencing assembly?

Also: I'm not looking for suggestions to manually set the references in the .csproj file of the referencing assembly; I know I can do that, but it's tedious, and has to be done for every reference.

Rainie answered 29/10, 2014 at 15:44 Comment(3)
uhm...if you're using DI, maybe interceptors is what you need. =/Maurer
Just prepare for them 2 dll's: debug and release one and use conditional referencing, or you can make that Log method to check something and either do job or ignore call.Aphanite
@Aphanite you don't need the two dlls, it's dependent on the client assembly compilation defines.Hiedihiemal
H
8

[Conditional("DEBUG")] is exactly what you are looking for. MSDN explanation of that attribute says:

Indicates to compilers that a method call or attribute should be ignored unless a specified conditional compilation symbol is defined.

However, what that explanation (as well as many others) fails to mention is that compilation symbol in question needs to be defined in the referencing assembly. In other words, if Assembly A contains method

[Conditional("DEBUG")]
public static void SomeMethod()
{ /* ... */ }

and you compile that assembly as Release, then SomeMethod will get called from Assembly B as long as that assembly is compiled as Debug.

Heterocercal answered 12/11, 2014 at 9:14 Comment(0)
H
5

The methods will be compiled into assembly independently of the defined values, so you can use the methods, and method usage will depend on the compiler defines when compiling the client assembly.

In other words, System.Diagnostics.ConditionalAttribute instructs the compiler that the METHOD CALL should be ignored, not how the method is compiled.

Hiedihiemal answered 29/10, 2014 at 16:2 Comment(0)
B
1

Method calls will be removed at compile time when some other code tries to use the method.

So depending on your goals you can either:

  • If you want others to conditionally use your "Log" method, but your own code in that assembly should not use the method - give release assembly (one compiled without "DEBUG").
  • If you want others to conditionally use your "Log" and your own code to log too - give debug assembly
  • If you want to be able to turn on/off logging for your own code at run time - consider using actual logging framework that allows it (even one that is part of .Net framework let you do so).

For more info check ConditionalAttribute

Brackett answered 29/10, 2014 at 16:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.