若要获得对符号进行逻辑“与”运算的效果,可以定义序列条件方法。例如,仅当 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();
}
}
}