icesar
icesar

Reputation: 496

Determine debug/release mode in published DLL? Without #DEBUG

I have a component which can be referenced in some projects (for example, Component.dll). I publish it, of course, in release mode.

In another project (for example, Project.exe) I reference Component.dll.
If I build Project.exe in Debug mode, is there a way to find out about that in my Component.dll library?

To clarify more: if I have a class and a method named Test within Component.dll. Can I do something like:

public void Test(){
    if(Debug.IsInDebugMode)
        ...
}

Keep in mind that Component.dll is built in release mode.

Upvotes: 6

Views: 5270

Answers (1)

Hans Passant
Hans Passant

Reputation: 941465

Whether your code is built in Release or Debug mode doesn't matter a great deal. The generated IL is very nearly the same. The Debug version will have an attribute that the jitter uses to set compilation defaults, that attribute is missing in yours. The next thing that matters is exactly how you debug or run your application. The setting that's important is Tools + Options, Debugging, General, "Suppress JIT optimization on module load". It is ticked by default.

Which now makes it matter whether your app gets started by a debugger or not. That's easy to find out, use the System.Diagnostics.Debugger.IsAttached property. When false, the machine code generated from your IL is going to be optimized by the jitter. A degenerate case is attaching a debugger after the code got started. Kinda important that this doesn't make any difference to you btw.

Upvotes: 5

Related Questions