C#特性(Attribute)

機能: クラスまたはメソッドにラベル情報を追加します。たとえば、コード プログラムをデバッグするときは、デバッグ イベント、デバッガ、デバッグ時間、およびクラスとメソッドのその他の情報を示します。
1. さまざまな定義方法に従って、機能はシステム提供の機能とユーザー定義の機能に分類されます。
システムが提供する機能:
(1) 条件付き: コンパイル時に、メソッドが指定された条件に従って実行されるかどうかを決定します。デバッグ時にデバッグ情報を出力し、リリース時に出力をキャンセルします。(デバッグ、開発およびテストのモード、リリース、プログラムのコンパイルの公開モード)

​
#define Hello//预处理指令,加上后,才表示要执行hello方法
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AtrributeTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            fun1("fun1---");
            Console.ReadKey();
        }
        static void fun1(string info)
        {
            Console.WriteLine(info);
            MyAtrribute.function1("debug---");
            MyAtrribute.hello("hello----");
        }
    }
    public class MyAtrribute 
    {
        [Conditional("DEBUG")]
        public static void function1(string info)//Debug模式下才执行
        {
            Console.WriteLine(info);
        }
        [Conditional("Hello")]
        public static void hello(string info)//加了预定义指令才执行
        {
            Console.WriteLine(info);
        }
    }
}

​

(2)Obsolete:プログラムが変更された場合、特定のメソッドを破棄する必要があり、識別子としてObsoleteが使用されます。使用すべきではないプログラム エンティティをマークします。これにより、特定のターゲット要素を破棄するようにコンパイラーに指示できます。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AtrributeTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            fun1("fun1---");
            fun2("fun2---");
            Console.ReadKey();
        }
        static void fun1(string info)
        {
            Console.WriteLine(info);
        }
        [Obsolete("Don't use Old Method",true)] //加了true表示在使用该方法时,编译器会直接报错
        static void fun2(string info)
        {
            Console.WriteLine(info);
        }
    }
}

カスタム機能: AttributeUsage を通じて定義され、通常は Attribute から継承するクラスを作成します。もちろん、ラベル機能も追加する必要があります。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AtrributeTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            
        }
    }
    //自定义Attribute标签
    //AllowMultiple在这里表示同一个类上是否只能放一个Attribute
    //Inherited表示Class被继承的时候,是否继承Attribute
    [AttributeUsage(AttributeTargets.Class,AllowMultiple =false,Inherited =false)]
    public class HelpAttribute : System.Attribute
    {
        protected string description;

        public HelpAttribute(string descriptionInfo)
        {
            this.description = descriptionInfo;
        }
        public string Description
        {
            get { return this.description; }
        }
        private string name;
        public string Name
        {
            set { this.name = value; }
            get { return this.name; }
        }
        private int age;
        public int Age
        {
            set { this.age = value; }
            get { return this.age; }
        }
    }
    //给MyClass加上了自定义的Attribute标签信息
    [HelpAttribute("this is a do-nothing class",Name ="Peter",Age =18)]
    public class MyClass
    {
    }


}

注: カスタム属性が適用できる状況。All はすべての状況に適用できることを意味します。Assembly はアセンブリに適用できることを意味します。Class はクラスに適用できることを意味します。その他は同様です。

 

結論: 抱きしめる木は髪の毛の先に生まれ、9 階建てのプラットフォームは土の山から始まり、千里の道も一歩から始まります。

おすすめ

転載: blog.csdn.net/falsedewuxin/article/details/129966256