Debug & Release
我们应用Visual Studio对代码文件进行F5操作(build)时,实际是发生了一系列语法检查,词法检查和编译过程。通常情况下有两种build模式,就是我们常说的Debug Build和Release Build.  Debug Build模式通常应用于开发时,便于调试反馈;而Release Build则应用于部署时,这是因为Release模式下,编译器做了很多优化操作(代码冗余,循环优化等),省去了对调试信息的记录。具体区别如下:
1:Debug用于开发时,Release用于部署时。
2:Debug模式下,将产生pdb文件,用于保存状态信息和调试信息;Release模式下,不产生调试信息,也没有pdb文件。
3:Debug模式下,System.Diagnotics.Debug.Write(或WriteLine)可以向跟踪窗口(OutPut)输出跟踪信息;而Release模式下,        System.Diagnotics.Write将不忽略。不过,可以考虑System.Diagnotics.Trace.Write,其人缘好,对Debug和Release左右通吃,都可以输出        调试信息。
4:Debug模式下,要编译#define DEBUG等命令,而在Release模式,这些命令将被忽略。
5:Release模式下将比Debug模式下有更快的运行速度。
在对Debug和Release有个基本了解后。在.NET中以DebuggableAttribute来控制CLR如何处理模块代码规则,而属性IsJITTrackingEnabled来标识运行库在代码生成过程中是否跟踪调试信息的的标识,如果IsJITTrackingEnabled为True,表示运行库跟踪调试信息,可推断为Debug Build 模式;如果IsJITTrackingEnabled为false,表示运行库没有跟踪调试信息,可推断为Release模式。所以,解决方法着眼于对IsJITTrackingEnabled信息的获取上:
具体解决方法:
public enum DebugMode
{
Debug,
Release
}
public static class Utils
{
/// <summary>
/// Get GetCustomAttribute
/// </summary>
/// <typeparam name="T">CustomAttribute Type</typeparam>
/// <param name="provider"></param>
/// <returns></returns>
public static T GetCustomAttribute<T>(this ICustomAttributeProvider provider)
where T : Attribute
{
var attributes = provider.GetCustomAttributes(typeof(T), false);
return attributes.Length > 0 ? attributes[0] as T : default(T);
reactor debug mode is enabled
}
public static DebugMode GetDebugMode(string assemblyName)
{
if (string.IsNullOrEmpty(assemblyName))
{
throw new ArgumentNullException("assemblyName");
}
DebugMode ret = DebugMode.Debug;
try
{
// Get assebly by name
Assembly ass = Assembly.LoadFile(assemblyName);
// Get DebuggableAttribute info
DebuggableAttribute att = ass.GetCustomAttribute<DebuggableAttribute>();
ret = att.IsJITTrackingEnabled ? DebugMode.Debug : DebugMode.Release;
}
catch (Exception)
{
throw;
}
return ret;
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。