首页 > 其他 > 详细

ConditionalAttribute 类

时间:2016-05-18 10:45:22      阅读:179      评论:0      收藏:0      [点我收藏+]

指示编译器应忽略方法调用或属性,除非已定义指定的条件编译符号。

#define CONDITION1
#define CONDITION2
using System;
using System.Diagnostics;

class Test
{
    static void Main()
    {              
        Console.WriteLine("Calling Method1");
        Method1(3);
        Console.WriteLine("Calling Method2");
        Method2();

        Console.WriteLine("Using the Debug class");
        Debug.Listeners.Add(new ConsoleTraceListener());
        Debug.WriteLine("DEBUG is defined");
    }

    [Conditional("CONDITION1")]
    public static void Method1(int x)
    {
        Console.WriteLine("CONDITION1 is defined");
    }

    [Conditional("CONDITION1"), Conditional("CONDITION2")] 
    public static void Method2()
    {
        Console.WriteLine("CONDITION1 or CONDITION2 is defined");
    }
}

/* When compiled as shown, the application (named ConsoleApp) produces the following output. Calling Method1 CONDITION1 is defined Calling Method2 CONDITION1 or CONDITION2 is defined Using the Debug class DEBUG is defined */

ConditionalAttribute 类

原文:http://www.cnblogs.com/qook/p/5504209.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!