C# 如何检查程序集是使用调试还是发布配置构建的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2186613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if an assembly was built using Debug or Release configuration?
提问by born to hula
I'm starting deployment of my web application and I need to guarantee that all the assemblies that are going to be deployed were built using Release configuration. Our system was developed using C#/.Net 3.5.
我正在开始部署我的 Web 应用程序,我需要保证将要部署的所有程序集都是使用发布配置构建的。我们的系统是使用 C#/.Net 3.5 开发的。
Is there any way to achieve this?
有没有办法实现这一目标?
采纳答案by David
Check this. The idea is that you get the list of assembly attributes using Assembly.GetCustomAttributes()
and search for DebuggableAttribute
and then find if such attribute has IsJITTrackingEnabled
property set.
检查这个。这个想法是您使用Assembly.GetCustomAttributes()
并搜索DebuggableAttribute
并查找该属性是否具有IsJITTrackingEnabled
属性集来获取程序集属性列表。
public bool IsAssemblyDebugBuild(Assembly assembly)
{
return assembly.GetCustomAttributes(false).OfType<DebuggableAttribute>().Any(da => da.IsJITTrackingEnabled);
}
回答by TrueWill
Don't deploy to production via Visual Studio. Look into Continuous Integrationand scripted builds (such as with NAnt, or perhaps something more legible like FAKE).
不要通过 Visual Studio 部署到生产环境。查看持续集成和脚本构建(例如使用NAnt,或者可能更清晰的东西,如FAKE)。
The F5 Key Is Not a Build Process
To detractors who believe that this does not answer the question, the OP wrote:
对于认为这不能回答问题的批评者,OP 写道:
...I need to guarantee that all the assemblies that are going to be deployed were built using Release configuration.
...我需要保证将要部署的所有程序集都是使用发布配置构建的。
To guaranteethat, use a build server such as TeamCityand possibly a release management tool like Octopus Deploy. Lock down your production systems so that developers must go through the official build process.
为了保证这一点,请使用构建服务器(例如TeamCity)和可能的发布管理工具(例如Octopus Deploy )。锁定您的生产系统,以便开发人员必须完成正式的构建过程。
回答by Rubens Farias
I loved that Davidsuggestion, but you could also go this way (AssemblyInfo.cs
):
我喜欢大卫的建议,但你也可以这样做(AssemblyInfo.cs
):
#if DEBUG
[assembly: AssemblyDescription("Your application assembly (DEBUG version)")]
#else if RELEASE
[assembly: AssemblyDescription("Your application assembly (RELEASE version)")]
#endif
This is more human friendly, as anyone can right-click that assembly, to select Properties
and go to Details
tab.
这更加人性化,因为任何人都可以右键单击该程序集,选择Properties
并转到Details
选项卡。
回答by xr280xr
If you have Reflector installed you can also click on the assembly and look for the debuggable attribute ([assembly: Debuggable()]) in the Disassembler pane.
如果您安装了 Reflector,您还可以单击程序集并在 Disassembler 窗格中查找可调试属性 ([assembly: Debuggable()])。
回答by Guillermo Ruffino
If it is your assembly I believe using the AssemblyConfigurationattribute is the best approach. It is documented as "Specifies the build configuration, such as retail or debug, for an assembly."
如果是您的程序集,我相信使用AssemblyConfiguration属性是最好的方法。它记录为“为程序集指定构建配置,例如零售或调试”。
Depending on your build configurations you might have code like this:
根据您的构建配置,您可能有这样的代码:
#if DEBUG
[assembly: AssemblyConfiguration("Debug")]
#else
[assembly: AssemblyConfiguration("Release")]
#endif
Then check the assembly attribute:
然后检查程序集属性:
public static bool IsAssemblyConfiguration(Assembly assembly, string configuration)
{
var attributes = assembly.GetCustomAttributes(typeof(AssemblyConfigurationAttribute), false);
if (attributes.Length == 1)
{
var assemblyConfiguration = attributes[0] as AssemblyConfigurationAttribute;
if (assemblyConfiguration != null)
{
return assemblyConfiguration.Configuration.Equals(configuration, StringComparison.InvariantCultureIgnoreCase);
}
}
return true;
}
(I know R. Schreurs comment at Rubens Farias says the same, but I've find this information somewhere else before seeing the comment so I believe this requires a more important entry like a full response instead of a comment)
(我知道 R. Schreurs 在 Rubens Farias 的评论也是如此,但我在看到评论之前在其他地方找到了这个信息,所以我相信这需要一个更重要的条目,比如完整的回复而不是评论)
回答by Chris Voon
Assuming only Debug and Release configuration, DEBUG symbol is by default defined with Debug configuration, so the code below in AssemblyInfo.cs (under Properties folder).
假设只有Debug和Release配置,DEBUG符号默认是用Debug配置定义的,所以下面的代码在AssemblyInfo.cs(在Properties文件夹下)。
#if DEBUG
[assembly: AssemblyTitle("Debug")]
#else
[assembly: AssemblyTitle("Release")]
#endif
I use AssemblyTitle over AssemblyDescription as it will show up on my Windows 7 file explorer properties:
我在 AssemblyDescription 上使用 AssemblyTitle,因为它将显示在我的 Windows 7 文件资源管理器属性中:
For those who like David and stevieg's answer, here is a LINQPad script written in C#. To use the script, you need to download LINQPad 5and make sure C# Program is selected as shown in screenshot below.
对于喜欢 David 和 stevieg 答案的人,这里有一个用 C# 编写的 LINQPad 脚本。要使用该脚本,您需要下载LINQPad 5并确保选择了 C# 程序,如下面的屏幕截图所示。
Simply replace DLL_FOLDER_PATH to point to folder containing the DLLs to be inspected.
只需替换 DLL_FOLDER_PATH 以指向包含要检查的 DLL 的文件夹。
// TODO - Specify your folder containing DLLs to inspect
static string DLL_FOLDER_PATH = @"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0";
void Main()
{
(from dllPath in Directory.GetFiles(DLL_FOLDER_PATH, "*.dll")
let assembly = dllPath.SafeLoad()
let build = assembly == null ? "Error" : (dllPath.SafeLoad().IsAssemblyDebugBuild() ? "Debug" : "Release")
select new {
Assembly_Path = dllPath,
Build = build,
}).Dump();
}
static class Extensions {
public static bool IsAssemblyDebugBuild(this Assembly assembly)
{
return assembly.GetCustomAttributes(false).OfType<DebuggableAttribute>().Select(da => da.IsJITTrackingEnabled).FirstOrDefault();
}
public static Assembly SafeLoad(this string path){
try{
return Assembly.LoadFrom(path);
}
catch {
return null;
}
}
}
LINQPAD 5 can be downloaded here.
LINQPAD 5 可以在这里下载。