How to tell if an existing assembly is Debug or Release build

When you build an assembly in Debug mode, the compiler adds the System.Diagnostics.DebbugableAttribute into the assembly’s attributes.  You can use this to determine if an assembly is a Debug build or Release build.  The following code is for a command line utility that just checks for that attribute in the specified assembly.



   Private Const UsageString As String = _
       “Checks to see if an assembly is a debug build.” & _
        “The arguement should be the path of the assembly  to check”



   Sub Main(ByVal args() As String)
      If args.Length = 1 Then
         Console.WriteLine(“File {0} is Debug Build = {1} “, _
                             
args(0), _
                              IsAssemblyDebugBuild(args(0)))
      Else
         Console.WriteLine(UsageString)
      End If
      Console.ReadLine()
   End Sub



   Private Function IsAssemblyDebugBuild(ByVal filepath As String) As Boolean
      Return IsAssemblyDebugBuild( _
              
Reflection.Assembly.LoadFile(IO.Path.GetFullPath(filepath)))
   End Function


   Private Function IsAssemblyDebugBuild(ByVal assemb As Reflection.Assembly) As Boolean
      For Each att As Object In assemb.GetCustomAttributes(False)
         If TypeOf att Is Diagnostics.DebuggableAttribute Then
          Return CType(att, Diagnostics.DebuggableAttribute).IsJITTrackingEnabled
         End If
      Next
   End Function



2 Comments so far

  1.   Mattias Sjögren on June 18th, 2004          

    v1.x of the C# compiler adds DebuggableAttribute(false, true) even for non-debug builds, unless you compile with /optimize. So just checking if DebuggableAttribute is present might not give you the expected result.

  2.   Bill McCarthy on June 18th, 2004          

    Thanks Mattias !

    Looks to me like a bug in the C# compiler 😉

    Anyway, I modified the code so as now it returns the IsJITTrackingEnabled value. hopefully that covers all C# quirky builds