.NET: C#: Attribute
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的更多相关文章
- [C#] 剖析 AssemblyInfo.cs - 了解常用的特性 Attribute
剖析 AssemblyInfo.cs - 了解常用的特性 Attribute [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5944391.html 序 ...
- JavaScript特性(attribute)、属性(property)和样式(style)
最近在研读一本巨著<JavaScript忍者秘籍>,里面有一篇文章提到了这3个概念. 书中的源码可以在此下载.我将源码放到了线上,如果不想下载,可以直接访问在线网址,修改页面名就能访问到相 ...
- [C#] C# 知识回顾 - 特性 Attribute
C# 知识回顾 - 特性 Attribute [博主]反骨仔 [原文地址]http://www.cnblogs.com/liqingwen/p/5911289.html 目录 特性简介 使用特性 特性 ...
- js attribute 和 jquery attr 方法
attribute 是原生js dom 对象上的一个属性,这个属性有很多子属性,比如 isId(判断属性是否是Id) , name (获取属性名称) , value (获取属性值),attribute ...
- 【.net 深呼吸】自定义特性(Attribute)的实现与检索方法
在.net的各个语言中,尤其是VB.NET和C#,都有特性这一东东,具体的概念,大家可以网上查,这里老周说一个非标准的概念——特性者,就是对象的附加数据.对象自然可以是类型.类型成员,以及程序集. 说 ...
- angular2系列教程(四)Attribute directives
今天我们要讲的是ng2的Attribute directives.顾名思义,就是操作dom属性的指令.这算是指令的第二课了,因为上节课的components实质也是指令. 例子
- 学会给你的类(及成员)来定制一套自己的Attribute吧
在通过Visual Studio创建的C#程序集中,都包含了一个AssemblyInfo.cs的文件,在这个文件中,我们常常会看到这样的代码 [assembly: AssemblyTitle(&quo ...
- Attribute操作的性能优化方式
Attribute是.NET平台上提供的一种元编程能力,可以通过标记的方式来修饰各种成员.无论是组件设计,语言之间互通,还是最普通的框架使 用,现在已经都离不开Attribute了.迫于Attribu ...
- SharePoint 2016 配置向导报错 - The 'ListInternal' attribute is not allowed
前言 配置SharePoint 2016的配置向导中,第三步创建配置数据库报错,然后百度.谷歌了一下,都没有解决,自己看日志搞定,也许会有人遇到类似问题,分享一下. 1.配置向导的错误截图,如下图: ...
- C# 知识特性 Attribute
C#知识--获取特性 Attribute 特性提供功能强大的方法,用以将元数据或声明信息与代码(程序集.类型.方法.属性等)相关联.特性与程序实体关联后,可在运行时使用"反射"查询 ...
随机推荐
- Address localhost:1099 is already in use 的错误
http://blog.csdn.net/huazhongkejidaxuezpp/article/details/41813683
- go access database demo
package main import ( "database/sql" "fmt" _ "github.com/lib/pq" " ...
- C/C++ 中长度为0的数组
参考文献:http://blog.csdn.net/zhaqiwen/article/details/7904515 近日在看项目中的框架代码时,发现了了一个奇特的语法:长度为0的数组例如 uint8 ...
- javascript值和引用
JavaScript引用指向的是值. 简单值(即标量基本类型值,基本类型值,js中6类,null.undefined.boolean.number.string和symbol)总是通过值复制的方式来赋 ...
- Array原型链添加“遍历”方法
<script> //1.在我们之前的项目里向原型链中集成方法时大多代码分析不严密,有时间我在这里会做详细分析; Array.prototype.each = function(fn) { ...
- 20145211 《Java程序设计》实验报告五————Java网络编程及安全实验报告
实验内容 1.掌握Socket程序的编写: 掌握密码技术的使用: 设计安全传输系统. 实验步骤 这一部分是与我的partner合作的,详见他的博客- [20145326 <Java程序设计> ...
- JavaScript实现在文本框中输入空格时自动填写某个值
<script language="javascript" type="text/javascript"> var txtText4 = " ...
- linux bq20z75 驱动
新的项目中使用到了电池.电池的guage使用TI的bq20z75.kernel的驱动中已经有bq20z75的驱动,只要稍加修改就可以使用. 参考链接 http://www.ti.com/lit/er/ ...
- http://blinkfox.com/shi-yong-spring-aoplai-tong-ji-fang-fa-de-zhi-xing-shi-jian/
http://blinkfox.com/shi-yong-spring-aoplai-tong-ji-fang-fa-de-zhi-xing-shi-jian/ spring-aop.xml @Com ...
- Android如何使用so文件和Android studio中导入so
Android中使用so文件: 做一个PDF阅读的功能,找到一个开源的库,mupdf.下载的是网上编译好的so库,导入到自己项目中的时候一直报错Java.lang.UnsatisfiedLinkErr ...