我要开始部署我的Web应用程序,并且需要确保所有将要部署的组件都是使用发布配置构建的。我们的系统是使用C#/.Net 3.5开发的。
有没有办法实现这个要求?
我要开始部署我的Web应用程序,并且需要确保所有将要部署的组件都是使用发布配置构建的。我们的系统是使用C#/.Net 3.5开发的。
有没有办法实现这个要求?
看看这个链接。其思路是使用Assembly.GetCustomAttributes()
获取程序集属性列表,查找DebuggableAttribute
,然后找到是否设置了IsJITTrackingEnabled
属性。
public bool IsAssemblyDebugBuild(Assembly assembly)
{
return assembly.GetCustomAttributes(false).OfType<DebuggableAttribute>().Any(da => da.IsJITTrackingEnabled);
}
我喜欢那个David的建议,但你也可以用以下方式 (AssemblyInfo.cs
):
#if DEBUG
[assembly: AssemblyDescription("Your application assembly (DEBUG version)")]
#else if RELEASE
[assembly: AssemblyDescription("Your application assembly (RELEASE version)")]
#endif
这样更加人性化,因为任何人都可以右键单击该程序集,选择 属性
并进入 详细信息
选项卡。
#if DEBUG
[assembly: AssemblyConfiguration("Debug")]
#else
[assembly: AssemblyConfiguration("Release")]
#endif
然后检查装配属性:
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;
}
我知道R.Schreurs在Rubens Farias的评论中也有相同的说法,但在看到该评论之前,我已经在其他地方找到了这些信息,因此我认为这需要一个更重要的条目,如全面回答,而不是只是一个评论。
#if DEBUG
[assembly: AssemblyTitle("Debug")]
#else
[assembly: AssemblyTitle("Release")]
#endif
// 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可以在这里下载。