Detect if assembly is compiled as debug

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.
 
R

Richard Blewett [DevelopMentor]

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.
 
T

tgmcmillen

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()
{
....
}
}
 
C

cody

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()
{
....
}
}
 

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