Need .NET code to execute only when in debug configuration

前端 未结 8 926
闹比i
闹比i 2021-01-30 10:38

I have some code that access an API out on the web. One of the API\'s parameters allows me to let them know that I am testing.

I would like to only set this parameter in

相关标签:
8条回答
  • 2021-01-30 11:26

    In addition to #if #endif directives, you can also use conditional attributes. If you mark a method with the attribute

    [Conditional("Debug")]
    

    It will only be compiled and run when your application is built in debug mode. As was noted in the comment below, these only work when the method has a void return type.

    0 讨论(0)
  • 2021-01-30 11:27

    Here is another post with a similar result : http://www.bigresource.com/Tracker/Track-vb-lwDKSoETwZ/

    A better explanation can be seen at : http://msdn.microsoft.com/en-us/library/4y6tbswk.aspx

    // preprocessor_if.cs
    #define DEBUG
    #define MYTEST
    using System;
    public class MyClass 
    {
        static void Main() 
        {
    #if (DEBUG && !MYTEST)
            Console.WriteLine("DEBUG is defined");
    #elif (!DEBUG && MYTEST)
            Console.WriteLine("MYTEST is defined");
    #elif (DEBUG && MYTEST)
            Console.WriteLine("DEBUG and MYTEST are defined");
    #else
            Console.WriteLine("DEBUG and MYTEST are not defined");
    #endif
        }
    }
    
    0 讨论(0)
提交回复
热议问题