Click here to Skip to main content
15,882,152 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

(C#) Determining whether the current build mode is Debug or Release

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
3 Feb 2012CPOL 13.9K   2   1
While I think alternative 3 could be quite useful I often have access to the assembly name and not the actual file name.For example the assembly name might be:Company.ProductWhereas the file name might be:Company.Product.dllThere are times when I'll fully qualify a class such...
While I think alternative 3 could be quite useful I often have access to the assembly name and not the actual file name.
For example the assembly name might be:
Company.Product
Whereas the file name might be:
Company.Product.dll

There are times when I'll fully qualify a class such as:
"Company.Product.Namespace.MyClass, Company.Product"
and therefore I have the name of the assembly, but not the name of the file.

The fully qualified class name is useful to pass to Type.GetType(string) when passing only the full class name doesn't seem to work.

While adding ".dll" on the end is not difficult I do think there are times when the following alternative might be useful.

Instead of passing the file name you simply pass the assembly name. Which does tend to be the file name without the .dll.

Instead of using the Assembly.LoadFile function it uses Assembly.Load.

C#
public static bool IsInDebugMode(string assemblyName)
{
    var assembly = System.Reflection.Assembly.Load(assemblyName);
    var attributes = assembly.GetCustomAttributes(typeof(System.Diagnostics.DebuggableAttribute), false);
    if (attributes.Length > 0)
    {
        var debuggable = attributes[0] as System.Diagnostics.DebuggableAttribute;
        if (debuggable != null)
            return (debuggable.DebuggingFlags & System.Diagnostics.DebuggableAttribute.DebuggingModes.Default) == System.Diagnostics.DebuggableAttribute.DebuggingModes.Default;
        else
            return false;
    }
    else
        return false;
}


However I'm sure there are plenty of times when people have the file name and therefore the previous alternative would be more suited.

So the function used depends on whether the assembly info you have access to is the assembly name or the file name.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
SoftwareMonkeys
Australia Australia
Founder of www.softwaremonkeys.net

Comments and Discussions

 
GeneralGood job, I updated my tip at http://www.codeproject.com/Tip... Pin
Huisheng Chen3-Feb-12 22:56
Huisheng Chen3-Feb-12 22:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.