Alternatives to Conditional Compilation in C#

前端 未结 7 2447
感情败类
感情败类 2021-02-07 21:17

What is the alternative to having code with conditional compilation in C#?

I have a class that has lots of code that is based on # ifdef .. After sometime my code is unr

7条回答
  •  死守一世寂寞
    2021-02-07 21:31

    An alternative is to use the ConditionalAttribute. The conditional attribute works in a similar way.

    #define TRACE_ON
    using System;
    using System.Diagnostics;
    
    public class Trace 
    {
        [Conditional("TRACE_ON")]
        public static void Msg(string msg)
        {
            Console.WriteLine(msg);
        }
    }  
    
    public class ProgramClass
    {
        static void Main()
        {
            Trace.Msg("Now in Main...");
            Console.WriteLine("Done.");
        }
    }
    

提交回复
热议问题