题
所以我有大约 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
它由配置中的节点控制。在我看来,这是比条件编译更好的解决方案。
但是,如果您希望能够基于编译进行控制,我认为您可以使用 条件属性.
问候,
编译器常量。我不记得 C# 语法,但我在 VB 中是这样做的:
#If CONFIG = "Debug" Then
'do somtehing
#Else
'do something else
#EndIf
不隶属于 StackOverflow