C#中如何对多个contional特性进行与操作

若要获得对符号进行逻辑“与”运算的效果,可以定义序列条件方法。例如,仅当 A 和 B 均已定义时,才能执行下面的第二种方法:
[Conditional("A")]
static void DoIfA()
{

DoIfAandB();

}

[Conditional("B")]
static void DoIfAandB()
{

// Code to execute when both A and B are defined...

}
经测试,只定义B,DoIfAandB是可以执行的啊。下面的代码输出B。

#define B
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;


namespace 特性与操作
{
    class Program
    {
        [Conditional("A")]
        static void DoIfA()
        {
            DoIfAandB();
        }

        [Conditional("B")]
        static void DoIfAandB()
        {
            Console.WriteLine( "B" );
            // Code to execute when both A and B are defined...
        }
        static void Main(string[] args)
        {
            DoIfAandB();
        }
    }
}
阅读 1.5k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进