Programmatically determine if an assembly is compiled in debug or release mode?

T

Taco

Dear all,

I'm writing a WinApp that makes use of plugins. In the aboutbox I want
to show a list of loaded plugins (Name, Version and if the plugin is
compiled in Debug or Release mode). The Name and Version are easy to
retrieve by using reflection, but I don't know how to determine in
which mode (debug/release) a plugin is compiled. All plugins are
derived
from a PluginBase class containing the logic to retrieve the three
above
mentioned properties. The plugins are in separate assemblies.

Of course I can make the DebugOrRelease property abstract in the base
class and override it in the concrete plugin classes. The code in the
concrete classes will then be the same as in my source code of the
base class below, but I've to repeat this for every concrete class and
I believe that's not really clean (that's why we have base classes,
isn't it).

Does someone know how to retrieve the mode (debug/release) of the
plugin assembly?

Thank you in advance.

With kind regards,
Taco.


=== Source: Common assembly ===

public interface IPlugin {
string Name { get; }
string Version { get; }
string DebugOrRelease { string get; }
}

public abstract class PluginBase : IPlugin {

public string Name {
get { return this.GetType().Name; }
}

public string Version {
get { return this.GetType().Assembly.GetName().Version.ToString();
}
}

public virtual string DebugOrRelease {
get {

// The following is not working, as this is the base class and the
// real plugins are in other assemblies, maybe compiled with other
// conditional compiler constants....

#if DEBUG
return "Debug";
#else
return "Release";
#endif

}
}
}

=== Source: Concrete plugin assembly ===

public class MyFirstPlugin : PluginBase {
public override string DebugOrRelease {
get {
// The following is working but has to be repeated in every
// concrete plugin.

#if DEBUG
return "Debug";
#else
return "Release";
#endif
}
}
}

public class MySecondPlugin : PluginBase {
public override string DebugOrRelease {
get {
// The following is working but has to be repeated in every
// concrete plugin.

#if DEBUG
return "Debug";
#else
return "Release";
#endif
}
}
}
 
M

mdb

I'm writing a WinApp that makes use of plugins. In the aboutbox I want
to show a list of loaded plugins (Name, Version and if the plugin is
compiled in Debug or Release mode). The Name and Version are easy to
retrieve by using reflection, but I don't know how to determine in
which mode (debug/release) a plugin is compiled. All plugins are
derived
from a PluginBase class containing the logic to retrieve the three
above
mentioned properties. The plugins are in separate assemblies.


How about:

[Conditional("Debug")]
public bool IsDebugMode()
{
return true;
}

bool IsDebug = false;
IsDebug = IsDebugMode();

-mdb
 
T

Taco

Sorry, after a search on Google, one of the first hits gave me the answer:
http://groups.google.com/groups?selm=eYYJ#[email protected]

Thank you anyway.
Taco

=== Source code ===
public abstract class PluginBase : IPlugin {
public string DebugOrRelease {
get {
Assembly ass = this.GetType().Assembly;
object[] attrs = ass.GetCustomAttributes(
typeof(DebuggableAttribute), true);
if (attrs != null && attrs.Length > 0) {
return "Debug";
} else {
return "Release";
}
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top