做到这一点的唯一最佳方法是检查已编译的程序集本身。Rotem Bloom 发现了一个非常有用的工具“ .NET 组装信息”给你。安装此程序后,它将自己与。Dll 文件自己打开。安装完成后,您只需双击组装即可打开,它将提供组装详细信息,如下面的屏幕截图所示。在那里你可以识别它是否是调试 不管有没有编译。

希望这个能帮上忙。

恕我直言,上面的应用程序确实有误导性; 它只查找 IsJITTrackingEnable,它完全独立于是否为优化和 JIT 优化编译代码。

如果在发布模式下编译并选择 DebugOutput 为“ none”以外的任何内容,则会出现 DebuggableAttribute。

您还需要定义 没错什么是“调试”与“发布”..。

你的意思是应用程序配置了代码优化? 您的意思是您可以将 VS/JIT 调试器附加到它吗? 您的意思是它生成 DebugOutput? 您的意思是它定义了 DEBUG 常量?请记住,可以使用系统有条件地编译方法。诊断。条件()属性。

恕我直言,当有人问一个程序集是“调试”还是“发布”时,他们真正的意思是代码是否被优化了..。

Sooo, do you want to do this manually or programmatically?

手动 : You need to view the value of the DebuggableAttribute bitmask for the assembly's metadata. Here's how to do it:

  1. 在 ILDASM 中打开程序集
  2. 打开清单
  3. Look at the DebuggableAttribute bitmask. If the DebuggableAttribute is not present, it is definitely an Optimized assembly.
  4. 如果它存在,看第4个字节-如果它是’0’,它是 JIT 优化-其他的,它不是:

//元数据版本: v4.0.30319... .//. 自定义实例 void [ mscolib ] System. Diagnostics. DebuggableAttribute: : . ctor (valuetype [ mscolib ] System. Diagnostics. DebuggableAttribute/DebuggingModes) = ( 010002 00000000)

编程 : 假设您希望通过编程知道代码是否为 JITOptimized,下面是正确的实现(在一个简单的控制台应用程序中) :

void Main()
{
var HasDebuggableAttribute = false;
var IsJITOptimized = false;
var IsJITTrackingEnabled = false;
var BuildType = "";
var DebugOutput = "";
    

var ReflectedAssembly = Assembly.LoadFile(@"path to the dll you are testing");
object[] attribs = ReflectedAssembly.GetCustomAttributes(typeof(DebuggableAttribute), false);


// If the 'DebuggableAttribute' is not found then it is definitely an OPTIMIZED build
if (attribs.Length > 0)
{
// Just because the 'DebuggableAttribute' is found doesn't necessarily mean
// it's a DEBUG build; we have to check the JIT Optimization flag
// i.e. it could have the "generate PDB" checked but have JIT Optimization enabled
DebuggableAttribute debuggableAttribute = attribs[0] as DebuggableAttribute;
if (debuggableAttribute != null)
{
HasDebuggableAttribute = true;
IsJITOptimized = !debuggableAttribute.IsJITOptimizerDisabled;
            

// IsJITTrackingEnabled - Gets a value that indicates whether the runtime will track information during code generation for the debugger.
IsJITTrackingEnabled = debuggableAttribute.IsJITTrackingEnabled;
BuildType = debuggableAttribute.IsJITOptimizerDisabled ? "Debug" : "Release";


// check for Debug Output "full" or "pdb-only"
DebugOutput = (debuggableAttribute.DebuggingFlags &
DebuggableAttribute.DebuggingModes.Default) !=
DebuggableAttribute.DebuggingModes.None
? "Full" : "pdb-only";
}
}
else
{
IsJITOptimized = true;
BuildType = "Release";
}


Console.WriteLine($"{nameof(HasDebuggableAttribute)}: {HasDebuggableAttribute}");
Console.WriteLine($"{nameof(IsJITOptimized)}: {IsJITOptimized}");
Console.WriteLine($"{nameof(IsJITTrackingEnabled)}: {IsJITTrackingEnabled}");
Console.WriteLine($"{nameof(BuildType)}: {BuildType}");
Console.WriteLine($"{nameof(DebugOutput)}: {DebugOutput}");
}

我在我的博客上提供了这个实现:

How to Tell if an Assembly is Debug or Release