我正在使用VS2010/2012,想知道是否有一种方法(可能使用反射)来查看程序集是如何构建的。
在调试时,我使用#if DEBUG
将调试信息写入控制台。
然而,当你拥有一堆程序集时,有没有一种方法可以查看它们是如何构建的?获取版本号很容易,但我一直没有找到如何检查构建类型的方法。
我正在使用VS2010/2012,想知道是否有一种方法(可能使用反射)来查看程序集是如何构建的。
在调试时,我使用#if DEBUG
将调试信息写入控制台。
然而,当你拥有一堆程序集时,有没有一种方法可以查看它们是如何构建的?获取版本号很容易,但我一直没有找到如何检查构建类型的方法。
AssemblyConfigurationAttribute
或 .NET 4.5 的 AssemblyMetadataAttribute
。#if DEBUG
[assembly: AssemblyConfiguration("Debug")]
#else
[assembly: AssemblyConfiguration("Release")]
#endif
或者
#if DEBUG
[assembly: AssemblyMetadata("DefinedVariable", "DEBUG")]
#endif
private bool IsAssemblyDebugBuild(string filepath)
{
return IsAssemblyDebugBuild(Assembly.LoadFile(Path.GetFullPath(filepath)));
}
private bool IsAssemblyDebugBuild(Assembly assembly)
{
foreach (var attribute in assembly.GetCustomAttributes(false))
{
var debuggableAttribute = attribute as DebuggableAttribute;
if(debuggableAttribute != null)
{
return debuggableAttribute.IsJITTrackingEnabled;
}
}
return false;
}
或者使用程序集元数据:
#if DEBUG
[assembly: AssemblyConfiguration("Debug")]
#else
[assembly: AssemblyConfiguration("Release")]
#endif
或者在代码中使用带有 #if DEBUG
的常量
#if DEBUG
public const bool IsDebug = true;
#else
public const bool IsDebug = false;
#endif
IsAssemblyDebugBuild
版本,如下所示:
return assembly.GetCustomAttributes(false).Any(x => (x as DebuggableAttribute) != null ? (x as DebuggableAttribute).IsJITTrackingEnabled : false);
- Verakso