簡體   English   中英

從調試版本中檢測發行版本的最佳方法? 。凈

[英]Best way to detect a release build from a debug build? .net

所以我有大約10個短的css文件,可以與mvc應用程序一起使用。 就像error.css login.css等...只是一些非常短的CSS文件,這些文件使更新和編輯變得容易(至少對我來說)。 我想要的是可以優化if else分支,而不將其合並到最終位中的東西。 我想做這樣的事情

if(Debug.Mode){

<link rel="stylesheet" type="text/css" href="error.css" /> 
<link rel="stylesheet" type="text/css" href="login.css" /> 
<link rel="stylesheet" type="text/css" href="menu.css" /> 
<link rel="stylesheet" type="text/css" href="page.css" /> 
} else {
<link rel="stylesheet" type="text/css" href="site.css" /> 
}

我將執行一個msbuild任務,該任務將合並所有css文件,將它們和所有這些好東西最小化。 我只需要知道是否有一種方法可以刪除最后幾位中的if else分支。

具體來說,在C#中像這樣:

#if (DEBUG)
   Debug Stuff
#endif

C#具有以下預處理程序指令:

#if 
#else 
#elif // Else If
#endif
#define
#undef // Undefine
#warning // Causes the preprocessor to fire warning
#error // Causes the preprocessor to fire a fatal error
#line // Lets the preprocessor know where this source line came from
#region // Codefolding
#endregion 
  if (System.Diagnostics.Debugger.IsAttached)
  {
           // Do this
  }
  else
  {
            // Do that
  }

我應該使用谷歌。

#if DEBUG
    Console.WriteLine("Debug mode.") 
#else 
    Console.WriteLine("Release mode.") 
#endif 

確保已檢查項目屬性中的選項“配置設置”->“構建”“定義調試常量”。

您可以嘗試使用

HttpContext.Current.IsDebuggingEnabled

它由配置中的節點控制。 在我看來,這是比條件編譯更好的解決方案。

但是,如果您希望能夠基於編譯進行控制,我認為您可以使用ConditionalAttribute

問候,

編譯器常量。 我不記得C#語法,但這是我在VB中的處理方式:

#If CONFIG = "Debug" Then
  'do somtehing
#Else
  'do something else
#EndIf

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM