ref: http://www.uml.org.cn/net/200810135.asp

ref: http://blog.csdn.net/okvee/article/details/2610349

注意这么几个问题:

1. Attribute和Property的区别

2. Attribute在编译中就有了,与面向对象中的多态不一样

3. 常用的Attribute: AttributeUsage, Flags, DllImport, Serializable, Conditional, 自定义特性

4. 可以通过反射来获取信息

5. Attribute本质上是一个类

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using MySql.Data;
 using MySql.Data.Entity;
 using MySql.Data.MySqlClient;
 using System.IO;
 using System.Data;
 using System.Diagnostics;
 using System.Runtime.InteropServices;
 using System.Reflection;

 namespace test4
 {
     [Flags]
     public enum Animal
     {
         Dog = 0x0001,
         Cat = 0x0002,
         Duck = 0x0004,
         Chicken = 0x0008
     }
     [AttributeUsage(AttributeTargets.Class)]
     public class VersionAttribute : Attribute
     {
         public string Name { get; set; }
         public string Date { get; set; }
         public string Description { get; set; }
     }
     [Version(Name = "yingzhongwen", Date = "2015-06-25", Description = "yingzhongwen's class")]
     public class MyClass
     {
         public void SayHello()
         {
             Console.WriteLine("Hello, my .NET world.");
         }
     }
     class Program
     {
         [DllImport("User32.dll")]
         public static extern int MessageBox(int hParent, string msg, string caption, int type);
         static void Main(string[] args)
         {
             var info = typeof(MyClass);
             var classAttribute = (VersionAttribute)Attribute.GetCustomAttribute(info, typeof(VersionAttribute));
             Console.WriteLine(classAttribute.Name);
             Console.WriteLine(classAttribute.Date);
             Console.WriteLine(classAttribute.Description);
             object obj = Activator.CreateInstance(typeof(MyClass));
             MethodInfo mi = (typeof(MyClass)).GetMethod("SayHello");
             mi.Invoke(obj, null);

             Animal animals = Animal.Cat | Animal.Dog;
             Console.WriteLine(animals.ToString());
             Console.WriteLine((MessageBox(, )));
         }
     }
 }

.NET: C#: Attribute的更多相关文章

  1. [C#] 剖析 AssemblyInfo.cs - 了解常用的特性 Attribute

    剖析 AssemblyInfo.cs - 了解常用的特性 Attribute [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5944391.html 序 ...

  2. JavaScript特性(attribute)、属性(property)和样式(style)

    最近在研读一本巨著<JavaScript忍者秘籍>,里面有一篇文章提到了这3个概念. 书中的源码可以在此下载.我将源码放到了线上,如果不想下载,可以直接访问在线网址,修改页面名就能访问到相 ...

  3. [C#] C# 知识回顾 - 特性 Attribute

    C# 知识回顾 - 特性 Attribute [博主]反骨仔 [原文地址]http://www.cnblogs.com/liqingwen/p/5911289.html 目录 特性简介 使用特性 特性 ...

  4. js attribute 和 jquery attr 方法

    attribute 是原生js dom 对象上的一个属性,这个属性有很多子属性,比如 isId(判断属性是否是Id) , name (获取属性名称) , value (获取属性值),attribute ...

  5. 【.net 深呼吸】自定义特性(Attribute)的实现与检索方法

    在.net的各个语言中,尤其是VB.NET和C#,都有特性这一东东,具体的概念,大家可以网上查,这里老周说一个非标准的概念——特性者,就是对象的附加数据.对象自然可以是类型.类型成员,以及程序集. 说 ...

  6. angular2系列教程(四)Attribute directives

    今天我们要讲的是ng2的Attribute directives.顾名思义,就是操作dom属性的指令.这算是指令的第二课了,因为上节课的components实质也是指令. 例子

  7. 学会给你的类(及成员)来定制一套自己的Attribute吧

    在通过Visual Studio创建的C#程序集中,都包含了一个AssemblyInfo.cs的文件,在这个文件中,我们常常会看到这样的代码 [assembly: AssemblyTitle(&quo ...

  8. Attribute操作的性能优化方式

    Attribute是.NET平台上提供的一种元编程能力,可以通过标记的方式来修饰各种成员.无论是组件设计,语言之间互通,还是最普通的框架使 用,现在已经都离不开Attribute了.迫于Attribu ...

  9. SharePoint 2016 配置向导报错 - The 'ListInternal' attribute is not allowed

    前言 配置SharePoint 2016的配置向导中,第三步创建配置数据库报错,然后百度.谷歌了一下,都没有解决,自己看日志搞定,也许会有人遇到类似问题,分享一下. 1.配置向导的错误截图,如下图: ...

  10. C# 知识特性 Attribute

    C#知识--获取特性 Attribute 特性提供功能强大的方法,用以将元数据或声明信息与代码(程序集.类型.方法.属性等)相关联.特性与程序实体关联后,可在运行时使用"反射"查询 ...

随机推荐

  1. Bootstrap 路径分页标签和徽章组件

    一.路径组件 路径组件也叫做面包屑导航. //面包屑导航 <ol class="breadcrumb"> <li><a href="#&qu ...

  2. Bootstrap 表格和按钮

    一.表格 1.条纹状表格 行产生一行隔一行加单色背景效果 注:表格效果需要基于基本格式.table <table class="table table-striped"> ...

  3. linux shell send and receive emails

    http://www.netcan666.com/2016/02/20/%E5%88%A9%E7%94%A8telnet%E5%9C%A8%E5%91%BD%E4%BB%A4%E8%A1%8C%E5% ...

  4. php--validate表单验证

    validate表单验证扩展规则 添加自定义检验(验证class) 获取html加入 class <input id="D_NUMBER" name="D_NUMB ...

  5. mysql基本sql语句大全(提升用语篇)

    1.说明:复制表(只复制结构,源表名:a 新表名:b) (Access可用) 法一:select * into b from a where 1<>1(仅用于SQlServer) 法二:s ...

  6. 1057 N的阶乘

    1057 N的阶乘 基准时间限制:1 秒 空间限制:131072 KB 输入N求N的阶乘的准确值. Input 输入N(1 <= N <= 10000) Output 输出N的阶乘 Inp ...

  7. [LeetCode]题解(python):101 Symmetric tree

    题目来源 https://leetcode.com/problems/symmetric-tree/ Given a binary tree, check whether it is a mirror ...

  8. Candies---hdu3159(spfa+差分约束)

    题目链接:http://poj.org/problem?id=3159 题意:有n个小孩,m个关系格式是A B C 表示小孩 B 的糖果数最多比小孩A多C个,相当于B-A<=C; 有m个这样的关 ...

  9. JQuery:JQuery设置HTML

    JQuery:设置HTML1.Query - 设置内容和属性设置内容 - text().html() 以及 val()我们将使用前一章中的三个相同的方法来设置内容: text() - 设置或返回所选元 ...

  10. Linux命令行–初识Linux shell

    shell及脚本简介  GNU/Linux shell 是个交互工具,它为用户提供了启动程序.管理文件系统上的文件以及管理运行在Linux系统上的进程的途径 . shell的核心是命令行提示符 它是s ...