• 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; }
            //    }
            //}

        }

    }

  • 相关阅读:
    详解java定时任务
    Java之Exception
    Java设计模式
    Java中的static关键字解析
    浅析Java中的final关键字
    深入理解Java的接口和抽象类
    一个故事讲清楚NIO
    Java并发编程:线程池的使用
    Java垃圾回收机制
    ubuntu sublime text 2 破解版
  • 原文地址:https://www.cnblogs.com/handboy/p/7148481.html
Copyright © 2020-2023  润新知