原文发布时间为: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. Oracle Storage in Action : 删除物理数据文件

    ALTER TABLESPACE XH_DM DROP DATAFILE 'F:\DEV\ORACLE-DATA\ORCL\XH_DM_1.DBF'; SQL> ALTER TABLESPACE ...

  2. 在SQLServer使用触发器实现数据完整性

    1.实现数据完整性的手段 在sqlserver中,在服务器端实现数据完整性主要有两种手段:一种是在创建表时定义数据完整性,主要分为:实体完整性.域完整性.和级联参照完整性:实现的手段是创建主键约束.唯 ...

  3. Oracle Recycle Bin

    开启回收站RECYCLEBIN=ON,默认开启 ALTER SYSTEM SET RECYCLEBIN=OFF SCOPE=SPFILE; 一.从回收站还原表 还原删除的表和从属对象. 如果多个回收站 ...

  4. 为什么ABAP整型的1转成string之后,后面会多个空格

    有同事问这个问题:lv_s是从int4转过来的,长度为2,和硬编码的lv_s2(长度为1)相比,后面多了个空格. 为什么?查SAP ABAP的编程帮助F1 help. 帮助文档说的很清楚了:如果赋值操 ...

  5. 分类IP地址(A、B、C类)的指派范围、一般不使用的特殊IP地址

    分类IP地址(A.B.C类)的指派范围.一般不使用的特殊IP地址 A类地址:0开头,8位网络号 B类地址:10开头,16位网络号 C类地址:110开头,24位网络号 D类地址:1110开头,多播地址 ...

  6. 超不清视频播放器-用Python将视频转成字符

    前言 今天分享的这段代码,看起来没啥实际用处,而且有些反潮流,因为现如今大家看视频都追求更高分辨率的超清画质,而我们这个,是一个“超不清”的视频播放器:在控制台里播放视频,用字符来表示画面 不过我觉得 ...

  7. windows安装tensorflow的一个教训

    今天没什么课,然后就准备安装tensorflow. 看了一下教程,就去做了. 然后就犯了错误.网上的教程还是有一些差异的,而我又比较大意,没有很注意到CUDA,cudnn的版本要求,也过于高估自己cp ...

  8. 利用MSF实现三层网络的一次内网渗透

    目标IP192.168.31.207 很明显这是一个文件上传的靶场 白名单限制 各种尝试之后发现这是一个检测文件类型的限制 上传php大马文件后抓包修改其类型为  image/jpeg 上传大马之后发 ...

  9. css实现水平/垂直居中效果

    一.如果是已知宽高的元素做水平/垂直居中效果的话,可以直接用具体的数值指定定位布局或偏移布局,这个就不过多讨论.这里主要介绍在不知宽高或需要弹性布局下的几种实现方式. 二.1.table表格法思路:显 ...

  10. 初见Vue

    一.What 官方定义:是一套用于构建用户界面的渐进式框架.这,what?不明觉厉,我反正现在还是不知道,在这之前,就只知道Vue.js是用来渲染数据的,其实它的核心库只关注视图层.不多说,用多了就知 ...