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. 【转】JavaScript中的this关键字使用的四种调用模式

    http://blog.csdn.net/itpinpai/article/details/51004266 this关键字本意:这个.这里的意思.在JavaScript中是指每一个方法或函数都会有一 ...

  2. http相关概念在iOS中的使用介绍

    http://www.cocoachina.com/ios/20160329/15773.html

  3. ADO.NET连接到数据库(oracle)

    本文摘抄于http://www.cnblogs.com/luluping/archive/2009/10/13/1582737.html,如有侵权,请联系博主. OracleConnection 对象 ...

  4. JS懒加载

    4.如何使用js懒加载图片       a.懒加载图片是基于jquery.js的,所以: <script src="jquery.js" type="text/ja ...

  5. 转:自定义ASP.NET MVC Html辅助方法

    在ASP.NET MVC中,Html辅助方法给我们程序员带来很多方便,其重要性也就不言自明.有时候,我们不想重复地写一些HTML代码,或者MS没有提供我们想要的那个HTML标签的Html辅助方法,那么 ...

  6. Android笔记:Socket通讯常见问题

    经验证的socket通讯问题 1.如果是模拟器和本机PC直接通讯,需要使用本机IP地址 而不是 10.0.2.2  如本机的静态地址为192.168.1.2 则直接使用该地址 2.接收和连接代码不能在 ...

  7. sqlserver 四舍五入(转)

    select   cast(round(12.5,2)   as   numeric(5,2)) 解释: round()函数,是四舍五入用,第一个参数是我们要被操作的数据,第二个参数是设置我们四舍五入 ...

  8. Apple Demo

    https://developer.apple.com/library/ios/navigation/ http://developer.apple.com/library/ios/samplecod ...

  9. Linux系统产生随机数的3种方法

    Linux系统产生随机数的3种方法 方法一:生成8位随机数 [root@localhost ~]# echo "$RANDOM$(date +%N%t)" | md5sum | c ...

  10. JavaScript:复选框事件的处理

    复选框事件的处理 复选框本身也是多个组件的名字相同.所以在定义复选框的同事依然要使用document.all()取得全部的内容. 范例:操作复选框,要求是可以一个个去选择选项,也可以直接全选,全选按钮 ...