/*
*
* Obsolete
*
*
*
* **/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.Diagnostics;
namespace A15_Attribute
{
class Demo1
{
//[Obsolete("该方法已过时,请使用NewMethod()替换",true)] //表示必须替换
[Obsolete("该方法已过时,请使用NewMethod()替换")]
public void OldMethod()
{
Console.WriteLine("这是旧方法");
}
public void NewMethod()
{
Console.WriteLine("这是新方法");
}
static void Main1(string[] args)
{
Demo1 obj = new Demo1();
obj.OldMethod();
obj.NewMethod();
}
}
}
/*
*
* 实现条件编译
* 1.使用Condition特性,针对方法
*
* 2.使用预编译指令,非常灵活,直接影响所包含的所有行代码
*
* **/
//#define OnlyTest //允许测试方法运行
//#define debug
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.Diagnostics;
namespace A15_Attribute
{
class Demo2
{
[Conditional("OnlyTest")]//条件执行,默认不执行
public void TestMethod()
{
Console.WriteLine("这是测试方法");
}
public void Method1()
{
Console.WriteLine("这是普通方法1");
}
public void Method2()
{
Console.WriteLine("这是普通方法2");
}
public void Test()
{
TestMethod();
Method1();
TestMethod();
Method2();
}
//条件编译
public void Test2()
{
Console.WriteLine("aaa");
Console.WriteLine("bbb");
Console.WriteLine("ccc");
#if debug //条件编译 如果定义了宏debug 则执行
Console.WriteLine("ddd");
#else
Console.WriteLine("eee");
#endif
}
static void Main(string[] args)
{
Demo2 obj = new Demo2();
obj.Test();
obj.Test2();
}
}
}