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. addevent兼容函数 && 阻止默认行为 && 阻止传播

    function addEvent(a, b, c, d) { a.addEventListener ? a.addEventListener(b, c, d) : a.attachEvent(&qu ...

  2. myeclipse 8.5 常用快捷键【转】

    eclipse 里查找行号的方法 今天开发的时候为了方便查找报错行的位置,特意在网上找了一下快捷键是什么,现做记录. 在Eclipse里的show the line number 后,使用" ...

  3. nsenter into docker. selinux(semanage,restorecon)

    Docker容器运行后,如何进入容器进行操作呢?起初我是用SSH.如果只启动一个容器,用SSH还能应付,只需要将容器的22端口映射到本机的一个端口即可.当我启动了五个容器后,每个容器默认是没有配置SS ...

  4. 读书笔记——《图解TCP/IP》(2/4)

    经典摘抄 第三章 数据链路 1.实际的通信媒介之间处理的却是电压的高低.光的闪灭以及电波的强弱等信号. 2.数据链路层的相关技术:MAC寻址.介质共享.非公有网络.分组交换.环路检测.VLAN等 3. ...

  5. ArcGIS API for Silverlight动态标绘的实现

    原文:ArcGIS API for Silverlight动态标绘的实现 1.下载2个dll文件,分别是: ArcGISPlotSilverlightAPI.dll 和 Matrix.dll 其下载地 ...

  6. js数组到后台转 list数组

    前台的数组格式是: [{"credit_record_certificate_id":"452","credit_record_type": ...

  7. setContentScaleFactor 设置图片的分辨率

    float scale = [[UIScreenmainScreen] scale];//得到设备的分辨率 [imageView setContentScaleFactor:[[UIScreen ma ...

  8. JS-003-innerText 与 innerHTML 区别

    此文主要讲述在使用 innerText 和 innerHTML 获取元素中间值时的差别,我个人将二者的区别简单的理解为: webelement.innerText : 获取的是页面元素显示的文本 we ...

  9. JVM底层又是如何实现synchronized的

    目前在Java中存在两种锁机制:synchronized和Lock,Lock接口及其实现类是JDK5增加的内容,其作者是大名鼎鼎的并发专家Doug Lea.本文并不比较synchronized与Loc ...

  10. VMware Workstation linux 问题

    1.can't find  /mnt/cdrom in etc/fstab or /etc/mtab mkdir /mnt/cdrom 2.80端口被占 一.查看哪些端口被打开 netstat -an ...