我的计算机上安装了一个应用程序。如何确定它是否在DEBUG模式下编译?
我试过使用.NET Reflector,但它没有显示任何具体的东西。这是我看到的:
// Assembly APPLICATION_NAME, Version 8.0.0.15072
Location: C:\APPLICATION_FOLDER\APPLICATION_NAME.exe
Name: APPLICATION_NAME, Version=8.0.0.15072, Culture=neutral, PublicKeyToken=null
Type: Windows Application
我很久以前的blogged,我不知道它是否仍然有效,但代码是......
private void testfile(string file)
{
if(isAssemblyDebugBuild(file))
{
MessageBox.Show(String.Format("{0} seems to be a debug build",file));
}
else
{
MessageBox.Show(String.Format("{0} seems to be a release build",file));
}
}
private bool isAssemblyDebugBuild(string filename)
{
return isAssemblyDebugBuild(System.Reflection.Assembly.LoadFile(filename));
}
private bool isAssemblyDebugBuild(System.Reflection.Assembly assemb)
{
bool retVal = false;
foreach(object att in assemb.GetCustomAttributes(false))
{
if(att.GetType() == System.Type.GetType("System.Diagnostics.DebuggableAttribute"))
{
retVal = ((System.Diagnostics.DebuggableAttribute)att).IsJITTrackingEnabled;
}
}
return retVal;
}
ZombieSheep的回答是不正确的。
我对这个重复问题的回答是:How to tell if a .NET application was compiled in DEBUG or RELEASE mode?
要非常小心 - 只需查看Assembly Manifest中的'assembly attributes'是否存在'Debuggable'属性并不意味着你有一个不是JIT优化的程序集。程序集可以进行JIT优化,但将Advanced Build设置下的Assembly Output设置为包含'full'或'pdb-only'信息 - 在这种情况下,将出现'Debuggable'属性。
请参阅我的帖子以获取更多信息:How to Tell if an Assembly is Debug or Release和How to identify if the DLL is Debug or Release build (in .NET)
Jeff Key的应用程序无法正常工作,因为它根据DebuggableAttribute是否存在来识别“Debug”构建。如果在Release模式下编译并选择DebugOutput为“none”以外的任何值,则存在DebuggableAttribute。
您还需要准确定义“调试”与“发布”的含义......
System.Diagnostics.Conditional()
属性有条件地编译方法。你实际上是在正确的道路上。如果您在反射器中查看反汇编程序窗口,如果它是在调试模式下构建的,您将看到以下行:
[assembly: Debuggable(...)]
使用Jeff Key的IsDebug工具怎么样?它有点过时,但是因为你有Reflector你可以反编译它并在任何版本的框架中重新编译它。我做到了。
这是ZombieSheep提出的解决方案的VB.Net版本
Public Shared Function IsDebug(Assem As [Assembly]) As Boolean
For Each attrib In Assem.GetCustomAttributes(False)
If TypeOf attrib Is System.Diagnostics.DebuggableAttribute Then
Return DirectCast(attrib, System.Diagnostics.DebuggableAttribute).IsJITTrackingEnabled
End If
Next
Return False
End Function
Public Shared Function IsThisAssemblyDebug() As Boolean
Return IsDebug([Assembly].GetCallingAssembly)
End Function
更新 这个解决方案对我有用,但正如Dave Black指出的那样,可能存在需要采用不同方法的情况。 所以也许你也可以看看Dave Black的答案: