9

So I have about 10 short css files that I use with mvc app. There are like error.css login.css etc... Just some really short css files that make updating and editing easy (At least for me). What I want is something that will optimize the if else branch and not incorporate it within the final bits. I want to do something like this

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" /> 
}

I'll have a msbuild task that will combine all the css files, minimize them and all that good stuff. I just need to know if there is a way to remove the if else branch in the final bits.

FlySwat
  • 172,459
  • 74
  • 246
  • 311
jdelator
  • 4,101
  • 6
  • 39
  • 53
  • similars questions in Stackoverflow, one question, and many, many different answers: http://stackoverflow.com/questions/654450/programatically-detecting-release-debug-mode-net http://stackoverflow.com/questions/798971/how-to-idenfiy-if-the-dll-is-debug-or-release-build-in-net http://stackoverflow.com/questions/194616/how-to-tell-if-net-app-was-compiled-in-debug-or-release-mode http://stackoverflow.com/questions/50900/best-way-to-detect-a-release-build-from-a-debug-build-net http://stackoverflow.com/questions/890459/asp-net-release-build-vs-debug-build – Kiquenet Feb 03 '11 at 19:51
  • Please refer to my posts: [How to Tell if an Assembly is Debug or Release](http://dave-black.blogspot.com/2011/12/how-to-tell-if-assembly-is-debug-or.html) and [http://stackoverflow.com/questions/798971/how-to-idenfiy-if-the-dll-is-debug-or-release-build-in-net/5316565#5316565](http://stackoverflow.com/questions/798971/how-to-idenfiy-if-the-dll-is-debug-or-release-build-in-net/5316565#5316565) – Dave Black Jun 21 '12 at 13:39

5 Answers5

27

Specifically, like this in C#:

#if (DEBUG)
   Debug Stuff
#endif

C# has the following preprocessor directives:

#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 
FlySwat
  • 172,459
  • 74
  • 246
  • 311
7
  if (System.Diagnostics.Debugger.IsAttached)
  {
           // Do this
  }
  else
  {
            // Do that
  }
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • 5
    This tells you (at runtime) if a debugger is attached, but not if the assembly is a DEBUG (vs RELEASE) build. – AlfredBr Oct 24 '12 at 17:17
6

I should had used google.

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

Make sure that the option "Configuration settings" -> "Build" "Define DEBUG constant" in the project properties is checked.

SteveC
  • 15,808
  • 23
  • 102
  • 173
jdelator
  • 4,101
  • 6
  • 39
  • 53
4

You can try to use

HttpContext.Current.IsDebuggingEnabled

it is controlled by a node in configuration. In my opinion this is nicer solution than conditional compilation.

However if you want to be able to control based on a compilation I think you can used a ConditionalAttribute.

Regards,

Assassin
  • 1,296
  • 2
  • 14
  • 17
1

Compiler constants. I don't remember the C# syntax, but this is how I do it in VB:

#If CONFIG = "Debug" Then
  'do somtehing
#Else
  'do something else
#EndIf
Jonathan Allen
  • 68,373
  • 70
  • 259
  • 447