原文发布时间为: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:\n{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}:\n{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}:\n{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; }
        //    }
        //}

    }

}

C#的特性学习草稿的更多相关文章

  1. Gradle学习草稿

    参考博客:http://www.cnblogs.com/davenkin/p/gradle-learning-1.html Android Plugin DSL Reference http://go ...

  2. Java8 新特性学习 Lambda表达式 和 Stream 用法案例

    Java8 新特性学习 Lambda表达式 和 Stream 用法案例 学习参考文章: https://www.cnblogs.com/coprince/p/8692972.html 1.使用lamb ...

  3. java8 新特性学习笔记

    Java8新特性 学习笔记 1主要内容 Lambda 表达式 函数式接口 方法引用与构造器引用 Stream API 接口中的默认方法与静态方法 新时间日期 API 其他新特性 2 简洁 速度更快 修 ...

  4. C#特性学习笔记二

    实例探讨: 自定义了一个特性类: [AttributeUsage(AttributeTargets.Class|AttributeTargets.Method)] class WahAttribute ...

  5. ES7/8新特性学习随笔

    随着每年EcmaScript都会为js带来一些新特性,带来更多美化的编程体验,今天就走进一下es2016/2017所带来的新特性 ES7新特性 includes() 指数操作符 ES8新特性 asyn ...

  6. java8新特性学习1

    java8增加了不少新特性,下面就一些常见的新特性进行学习... 1.接口中的方法 2.函数式接口 3.Lambda表达式 4.java8内置的四大核心函数式接口 5.方法引用和构造器引用 6.Str ...

  7. python切片、迭代、生成器、列表生成式等高级特性学习

    python高级特性 1行代码能实现的功能,决不写5行代码.请始终牢记,代码越少,开发效率越高. 切片 当我们要取一个list中的前n各元素时,如果前n个少的话,我们还可以一个一个的取,但是若前n个元 ...

  8. C#的特性学习

    转自:https://www.cnblogs.com/rohelm/archive/2012/04/19/2456088.html   特性提供功能强大的方法,用以将元数据或声明信息与代码(程序集.类 ...

  9. 【Java语言特性学习之一】设计模式

    设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了可重用代码.让代码更容易被他人理解.保证代码可靠性. 毫无疑问,设计模式于 ...

随机推荐

  1. BST AVL RBT B- B+ 的一些理解

    BST(二叉查找树,排序二叉树),如果数据有序的话,组成的二叉树会形成单列的形式,导致查询效率低AVL(平衡二叉树) 使树的左右高度差的绝对值不超过2,保证了查询效率.但是插入和删除会带来多次旋转,导 ...

  2. 初用emmet

    下载emmet的pspad插件emmet.js.复制到pspad目录下的 script\JScript 文件夹. 输入 ul#nav>li.item$*4>{Item $} 但是没反应. ...

  3. Selenium私房菜系列--总章

    前言 在这段期间,我一直在找关于服务器的端测试方案,自动化工具等等,无意间我发现了Selenium这个工具.在试用一段时间后,觉得Selenium确实是一个很不错的Web测试工具.在和强大的QTP比较 ...

  4. 十个 JDBC 的最佳实践

    JDBC是Java为多种关系型数据库提供的统一的访问接口,以下是我长期使用JDBC总结的十个最佳实践. 1. 使用PrearedStatement 任何一个使用过JDBC的Java程序员几乎都知道这个 ...

  5. RPC(Remote Procedure Call Protocol)远程过程调用协议

    RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.RPC协议假定某些传输协议的存在 ...

  6. 使用正则进行HTML页面属性的替换

    使用正则表达式拼接富文本框 package com.goboosoft.common.utils; import org.apache.commons.lang3.StringUtils; impor ...

  7. vscode 打开新文件不替换旧文件

    设置 "workbench.editor.enablePreview": false

  8. 使用SpringBoot-JPA进行自定义的保存及批量保存

    更多精彩博文,欢迎访问我的个人博客 说明 SpringBoot版本:2.1.4.RELEASE java版本:1.8 文中所说JPA皆指spring-boot-starter-data-jpa 使用J ...

  9. 1.入手树莓派之linux环境搭建

    最近刚刚买了一款 树莓派3代B型 raspberrypi 板载蓝牙和WIFI 英国版本,没玩过,觉得很好奇,生怕记性不好哈,把自己玩的过程记录一下,以备不时之需: 需要材料: 1) 树莓派: 2)sd ...

  10. 【转】C#中的==、Equal、ReferenceEqual

    [转]C#中的==.Equal.ReferenceEqual 转载自: http://www.cnblogs.com/zagelover/articles/2741409.html 1. Refere ...