C#的特性学习草稿

原文发布时间为:2008-11-22 —— 来源于本人的百度文章 [由搬家工具导入]

举个简单的例子:
先定义个特性
从Attribute继承,并标明用法

[AttributeUsage(AttributeTargets.Property|AttributeTargets.Class)]
public class MyAttribute:Attribute
{

}

//应用此特性
[My]
public class Entity
{
private int m_MyProperty ;
[My]
public virtual int MyProperty
{
get { return m_MyProperty; }
set { m_MyProperty = value; }
}

}

//检索此特性(在类上标的特性)
class program
{
static void Main()
{
Attribute attr = Attribute.GetCustomAttribute(typeof(Entity), typeof(MyAttribute),false);

}
}


using System;
using System.Collections.Generic;
using System.Text;

using System.Reflection;

namespace baidu
{

    class Class5
    {

        //[Obsolete("no use forever",true)]
        //static void Old() { }

        //static void New() { }
        //public static void Main()
        //{
        //    Old();
        //}
        //static void Main()
        //{
        //     Console.WriteLine(typeof(HumanBase).IsSerializable);
        //     Console.ReadLine();
        //}

        //static void Main()
        //{

        //    BoardingCheckAttribute boardingCheck = new BoardingCheckAttribute("1", "dsf", 1, 1, "df", "df");
        //   Console.WriteLine(boardingCheck.Name
        //                   + "'s ID is "
        //                + boardingCheck.ID
        //                  + ", he/she wants to "
        //                   + boardingCheck.Destination
        //                   + " from "
        //                    + boardingCheck.Departure
        //                    + " by the plane "
        //                   + boardingCheck.FlightNumber
        //                    + ", his/her position is "
        //                 + boardingCheck.PostionNumber
        //                     + ".");
        //    Console.ReadLine();
        //}
        //static void Main()
        //{
        //    Attribute attr = Attribute.GetCustomAttribute(typeof(Base), typeof(HelpAtrribute),true);
        //    if (attr is HelpAtrribute)
        //    {
        //        HelpAtrribute help = (HelpAtrribute)attr;
        //        Console.WriteLine(help.Description);
        //    }
        //    Console.ReadLine();

        //}

        static void Main()
        {
            Type type = typeof(Class1);
            HelpAtrribute helpAttr;
            foreach (Attribute attr in type.GetCustomAttributes(true))
            {
                helpAttr = attr as HelpAtrribute;
                if (helpAttr != null)
                {
                    Console.WriteLine("Description of Class1: {0}", helpAttr.Description);
                }
            }

            foreach (MethodInfo method in type.GetMethods())
            {
                foreach (Attribute attr in method.GetCustomAttributes(true))
                {
                    helpAttr = attr as HelpAtrribute;
                    if (helpAttr != null)
                    {
                        Console.WriteLine("Description of {0}: {1}", method.Name, helpAttr.Description);
                    }
                }
            }

            foreach (FieldInfo field in type.GetFields())
            {
                foreach (Attribute attr in field.GetCustomAttributes(true))
                {
                    helpAttr = attr as HelpAtrribute;
                    if (helpAttr != null)
                    {
                        Console.WriteLine("Description of {0}: {1}", field.Name, helpAttr.Description);
                    }
                }
            }

           
            Console.ReadLine();
        }

        [AttributeUsage(AttributeTargets.All, AllowMultiple = false, Inherited = false)]
        public class HelpAtrribute : Attribute
        {
            protected string description;
            public HelpAtrribute(string description)
            {
                this.description = description;
            }
            public string Description
            {
                get { return this.description; }
            }
            protected string version;
            public string Version
            {
                get { return this.version; }
                set { this.version = value; }
            }

        }
        [HelpAtrribute("this is a donothing class")]
        //[HelpAtrribute("this is a doNothing class")]
        public class AnyClass
        {
            [HelpAtrribute("this is a doNothing class")]
            public void AnyMethod()
            {
            }
        }

        [HelpAtrribute("BaseClass")]
        public class Base
        {
        }
        [HelpAtrribute("DeriveClass")]
        public class Derive : Base
        {
        }

        [HelpAtrribute("this is class1")]
        public class Class1
        {
            [HelpAtrribute("this is doNothing Method")]
            public void Anymethod()
            {
            }
            [HelpAtrribute("this is an integer")]
            public int AnyInt=0;
        }
        [HelpAtrribute("this is Class2", Version = "2.0")]
        public class Class2
        {
        }

        //[AttributeUsage(AttributeTargets.Property | AttributeTargets.Class)]
        //public class BoardingCheckAttribute : Attribute
        //{
        //    private string id,name,dep,des;
        //    private int flightnum, positionum;
        //    public BoardingCheckAttribute(string id, string name, int flightNumber, int PostionNumber, string departure, string destination)
        //    {
        //        this.id = id;
        //        this.name = name;
        //        this.flightnum = flightNumber;
        //        this.positionum = PostionNumber;
        //        this.dep = departure;
        //        this.des = destination;
        //    }

        //    public string ID
        //    {
        //        get { return id; }
        //        set { id = value; }
        //    }
        //    public string Name
        //    {
        //        get { return name; }
        //        set { name = value; }
        //    }

        //    public int FlightNumber
        //    {
        //        get { return flightnum; }
        //        set { flightnum = value; }
        //    }
        //    public int PostionNumber
        //    {
        //        get { return positionum; }
        //        set { positionum = value; }
        //    }
        //    public string Departure
        //    {
        //        get { return dep; }
        //        set { dep = value; }
        //    }
        //    public string Destination
        //    {
        //        get { return des; }
        //        set { des = value; }
        //    }
        //}

    }

}

原文地址:https://www.cnblogs.com/handboy/p/7148481.html