C# 特性

Conditional:起条件编译的作用,只有满足条件,才允许编译器对它的代码进行编译。一般在程序调试的时候使用。
Obsolete:这个属性用来标记当前的方法已经被废弃,不再使用了。
DebuggerStepThrough:调试时候跳过这个标记的部分

 static void Main(string[] args)
        {
            Person person=new Person();
            person.Add();
            person.Sum();
            person.Test();
            ReadKey();
        }

class Person
    {
        [Obsolete("这个是过期的")]
        public void Sum()
        {
            WriteLine("Sum");
        }

        [DebuggerStepThrough]
        public void Add()
        {
            Console.WriteLine("Add");
        }

   [Conditional("Two")]
        [Conditional("Common")]
        public void Test()
        {
#if Common
            WriteLine("test");
#elif Two
            WriteLine("Twotest");
#endif

        }

    }

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
02.
#define条件编译
C#中条件编译指令用于按条件包含或排除源文件中的某些部分。在Visual Studio中,会看到被排除的代码显示为灰色。
语法:#define 名称

注意:这里名称取Debug,你也可以取其他名称如Dragon

1 #define Debug
说明:

1、Debug可以看做是声明的一个变量,但此变量没有真正的值,存在时#if Debug结果为true,否则为false;

2、#define单独用没什么意义,一般是和#if或者Conditional特性结合使用;

3、#define必须定义在所有using命名空间前面;

4、Debug与DEBUG是不同的,C#区分大小写。
方式一、使用#if

#define Debug
using System;
using static System.Console;
namespace Attar4
{
    class Program
    {
        static void Main(string[] args)
        {
#if Debug
            WriteLine("Debug");
#else 
          WriteLine("NoDebug");
#endif
            ReadKey();
        }
    }

}

在这里插入图片描述
如果注释掉 //#define Debug ,输出结果为:

//#define Debug
using System;
using static System.Console;
namespace Attar4
{
    class Program
    {
        static void Main(string[] args)
        {
#if Debug
            WriteLine("Debug");
#else 
          WriteLine("NoDebug");
#endif
            ReadKey();
        }
    }

}

在这里插入图片描述
方式二、使用Conditional特性
们可以将一些函数隔离出来,使得它们只有在定义了某些环境变量或者设置了某个值之后才能发挥作用
使用Conditional特性的隔离策略要比#if/#endif不容易出错。

#define Debug
#define Trace
#if (Debug&&Trace)
#define DebugAndTrace
#endif
using System;
using System.Diagnostics;
using static System.Console;
namespace Attar4
{
    class Program
    {
        static void Main(string[] args)
        {

            Person person=new Person();
            person.Print0();
            person.Print1();
            person.Print2();
            person.Print3();
            ReadKey();
        }
    }

    class Person
    {
        [Conditional("DEBUG")]
        public void Print0()
        {
            WriteLine("DEBUG is defined");
        }

        [Conditional("Debug")]
        public void Print1()
        {
            WriteLine("Debug is defined");
        }

        //定义了Debug或者Trace后才会执行此方法
        [Conditional("Debug"),Conditional("Trace")]
        public void Print2()
        {
            WriteLine("Debug or Trace is defined");
        }

        //只有定义了Debug和Trace后才会执行此方法
        [Conditional("DebugAndTrace")]
        public void Print3()
        {
            WriteLine("Debug and Trace is defined");
        }

    }

}

在这里插入图片描述
03.
自定义Attribute
自定义的Attribute必须直接或者间接地从Attribute这个类派生

using System;
using static System.Console;
namespace Attar5
{
    class Program
    {
        static void Main(string[] args)
        {
            Type type=typeof(People);
            object[] objects=type.GetCustomAttributes(typeof(HelpAttribute),false);
            WriteLine(((HelpAttribute)objects[0]).Des);
            ReadKey();
        }
    }



    //ValidOn: 读取或者设置这个属性,指明Attribute 可以被施加的元素的类型。
    //AllowMultiple: 读取或者设置这个属性,表示是否可以对一个程序元素施加多个Attribute 。
    //Inherited:读取或者设置这个属性,表示是否施加的Attribute 可以被派生类继承或者重载。
    [AttributeUsage(AttributeTargets.Class|AttributeTargets.Struct,AllowMultiple = false,Inherited = false)]
    class HelpAttribute:Attribute
    {

        private string _des;

        public string Des { get => _des; set => _des = value; }

        public HelpAttribute(string Des)
        {
            this.Des = Des;
        }
    }

    [Help("这是一个描述")]
    class People
    {

        public void Print()
        {
            
        }
    }

    class Student:People
    {
        
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值