ConditionalAttribute

作者:追风剑情 发布于:2019-5-6 18:13 分类:C#

示例:除非定义了相应的宏,否则忽略方法调用

#define TOOL_B

using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Tools.FunA();
            Tools.FunB();

            Console.Read();
        }
    }

    public class Tools
    {
        [Conditional("TOOL_A")]
        public static void FunA()
        {
            Console.WriteLine("FunA");
        }

        [Conditional("TOOL_B")]
        //[Conditional("CONDITION1"), Conditional("CONDITION2")]多个条件
        public static void FunB()
        {
            Console.WriteLine("FunB");
        }
    }
}


运行测试
11111.png

原代码

namespace System.Diagnostics
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]
    public sealed class ConditionalAttribute : Attribute
    {
        public ConditionalAttribute(string conditionString);

        public string ConditionString { get; }
    }
}

标签: C#

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号