2010-02-02 28 views

回答

36

檢查this。這個想法是,您使用Assembly.GetCustomAttributes()獲得程序集屬性列表,然後搜索DebuggableAttribute,然後查找此屬性是否具有IsJITTrackingEnabled屬性集。

public bool IsAssemblyDebugBuild(Assembly assembly) 
    { 
     return assembly.GetCustomAttributes(false).OfType<DebuggableAttribute>().Any(da => da.IsJITTrackingEnabled); 
    } 
+13

諷刺並不是真的需要。通常情況下,搜索互聯網最困難的部分是知道要問什麼。 – doogle 2013-05-01 17:45:16

+2

僅供參考,此鏈接已中斷 – scojomodena 2015-02-03 22:16:21

+0

鏈接已更新。 – Turch 2015-03-13 18:08:14

0

不要通過Visual Studio部署到生產環境。看看Continuous Integration和腳本構建(例如NAnt,或者更像FAKE更清晰)。

The F5 Key Is Not a Build Process

爲了誰相信,這不回答這個問題的反對者,在OP寫道:

...我需要保證所有的組件,其將是 分別部署使用發佈配置構建。

保證的是,使用一個構建服務器,如TeamCity,並可能釋放管理的工具,像Octopus Deploy。鎖定您的生產系統,以便開發人員必須完成官方構建流程。

+1

這不會以任何方式回答問題。有一個構建過程是一個好主意,這不是一個答案。 – jasonmw 2016-10-21 17:51:58

25

我喜歡那個David建議,但你也可以走這條路(AssemblyInfo.cs):

#if DEBUG 
[assembly: AssemblyDescription("Your application assembly (DEBUG version)")] 
#else if RELEASE 
[assembly: AssemblyDescription("Your application assembly (RELEASE version)")] 
#endif 

這是更加人性化的友好,因爲任何人都可以用鼠標右鍵單擊該程序集,選擇Properties,去Details標籤。

+2

+1:這實際上是這個程序集指令的目的。 – 2012-03-24 23:40:47

+7

我會推薦使用AssemblyConfiguration而不是AssemblyDescription。 AssemblyConfiguration記錄爲「指定程序集的構建配置,如零售或調試。」 – 2013-01-11 16:20:12

+0

這可以工作,但它不像在任何地方都能夠檢測到代碼中的東西那麼好。 – ps2goat 2013-10-22 19:40:24

2

如果您安裝了Reflector,您還可以單擊該程序集並在Disassembler窗格中查找可調試屬性([assembly:Debuggable()])。

7

如果是你的程序集,我相信使用AssemblyConfiguration屬性是最好的方法。它記錄爲「爲程序集指定構建配置,例如零售或調試。」

根據您的生成配置,你可能有這樣的代碼:

#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的評論說,同樣,但在看到評論前我已經在其他地方找到了這些信息,所以我相信這需要一個更重要的條目,如完整回覆而不是評論)

1

假設只有Debug和Release配置,DEBUG符號默認使用Debug配置來定義,所以下面的代碼在AssemblyInfo.cs中(在Properties文件夾下)。

#if DEBUG 
[assembly: AssemblyTitle("Debug")] 
#else 
[assembly: AssemblyTitle("Release")] 
#endif 

我用AssemblyTitle過AssemblyDescription,因爲它會出現在我的Windows 7的文件瀏覽器屬性:

DLL File properties

對於那些誰像大衛和stevieg的答案,這裏是寫在LINQPad腳本C#。要使用該腳本,您需要下載LINQPad 5並確保選擇了C#程序,如下面的屏幕截圖所示。

只需將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; 
     } 
    } 
} 

Checking release or debug build using LINQPad5

LINQPAD 5可以下載here

相關問題