Detect if assembly is compiled as debug

  • Thread starter Thread starter cymantic
  • Start date Start date
C

cymantic

Is it possible in c# to detect if an assembly is compiled with the
DEBUG option?

I want to add attributes to some classes, that will do different
things depending on whether the class is build as debug or not.

Thanks in advance.
 
You can use #ifdef as follows

#ifdef DEBUG
[MyAttribute("foo")]
#endif
class Foo
{
}

In this case the MyAttribute will only apply to the class Foo if the assembly is compiled with the DEBUG symbol defined.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Is it possible in c# to detect if an assembly is compiled with the
DEBUG option?

I want to add attributes to some classes, that will do different
things depending on whether the class is build as debug or not.

Thanks in advance.
 
Richard said:
You can use #ifdef as follows

#ifdef DEBUG
[MyAttribute("foo")]
#endif
class Foo
{
}


Not quite....

I want to do different things with the Attribute if the class which
contains the attribute is compiled with debug info...

e.g.

System.Type type = obj.GetType();

MemberInfo[] members = type.GetMembers();

for ( int i = 0; i < members.Length; i++ )
{
object[] attributes =
members.GetCustomAttributes(false);

for ( int j = 0; j < attributes.Length; j++ )
{
if ( attribute[j] is MyAttribute &&
obj.IsCompiledWithDebug )
{
// do something
}
else
{
// do something else
}
}


where obj is of type Foo, which is defined as in your example:


class Foo
{
[MyAttribute("foo")]
public void Bar()
{
....
}
}
 
You can't directly determine wheather a class was compiled with Debug or
not.
But you can, for example use System.Diagnostics.Debugger.IsAttached to
determine if the running program is currently debugged.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Richard said:
You can use #ifdef as follows

#ifdef DEBUG
[MyAttribute("foo")]
#endif
class Foo
{
}


Not quite....

I want to do different things with the Attribute if the class which
contains the attribute is compiled with debug info...

e.g.

System.Type type = obj.GetType();

MemberInfo[] members = type.GetMembers();

for ( int i = 0; i < members.Length; i++ )
{
object[] attributes =
members.GetCustomAttributes(false);

for ( int j = 0; j < attributes.Length; j++ )
{
if ( attribute[j] is MyAttribute &&
obj.IsCompiledWithDebug )
{
// do something
}
else
{
// do something else
}
}


where obj is of type Foo, which is defined as in your example:


class Foo
{
[MyAttribute("foo")]
public void Bar()
{
....
}
}
 
Back
Top