您所在的位置:开发 > .NET > 你必须知道的.NET之特性和属性(3)

你必须知道的.NET之特性和属性(3)

2007-09-18 13:29 Anytao 博客园 我要评论(0) 字号:T | T
一键收藏,随时查看,分享好友!

本文的主题以特性的介绍为主,属性的论述重点突出在二者的比较上,希望给你的理解带来收获。

AD:

1、定义特性

[AttributeUsage(AttributeTargets.Class |
AttributeTargets.Method,
Inherited = true)]
public class TestAttribute : System.Attribute
{
public TestAttribute(string message)
{
throw new Exception("error:" + message);
}
public void RunTest()
{
Console.WriteLine("TestAttribute here.");
}
}

2、应用目标元素

[Test("Error Here.")]
public void CannotRun()
{
//
}

3、获取元素附加信息

如果没有什么机制来在运行期来获取Attribute的附加信息,那么attribute就没有什么存在的意义。因此,.NET中以反射机制来实现在运行期获取attribute信息,实现方法如下: 

public static void Main(string[] args)
{
Tester t = new Tester();
t.CannotRun();

Type tp = typeof(Tester);
TestAttribute myAtt = (TestAttribute)Attribute.GetCustomAttribute((MemberInfo)tp,

typeof(TestAttribute));
}

5、经典示例

5.1、小菜一碟

啥也不说了,看注释吧!

using System;
using System.Reflection;  //应用反射技术获得特性信息

namespace Anytao.net
{
//定制特性也可以应用在其他定制特性上,
//应用AttributeUsage,来控制如何应用新定义的特性
[AttributeUsageAttribute(AttributeTargets.All,       //可应用任何元素
AllowMultiple = true,                            //允许应用多次
Inherited = false)]                              //不继承到派生类
//特性也是一个类,
//必须继承自System.Attribute类,
//命名规范为:"类名"+Attribute。       
public class MyselfAttribute : System.Attribute
{
//定义字段
private string _name;
private int _age;
private string _memo;

//必须定义其构造函数,如果不定义有编译器提供无参默认构造函数
public MyselfAttribute()
{
}
public MyselfAttribute(string name, int age)
{
_name = name;
_age = age;
}

//定义属性
//显然特性和属性不是一回事儿
public string Name
{
get { return _name == null ? string.Empty : _name; }
}

public int Age
{
get { return _age; }
}

public string Memo
{
get { return _memo; }
set { _memo = value; }
}

//定义方法
public void ShowName()
{
Console.WriteLine("Hello, {0}", _name == null ? "world." : _name);
}
}

//应用自定义特性
//可以以Myself或者MyselfAttribute作为特性名
//可以给属性Memo赋值
[Myself("Emma", 25, Memo = "Emma is my good girl.")]
public class Mytest
{
public void SayHello()
{
Console.WriteLine("Hello, my.net world.");
}
}

public class Myrun
{
public static void Main(string[] args)
{
//如何以反射确定特性信息
Type tp = typeof(Mytest);
MemberInfo info = tp;
MyselfAttribute myAttribute =
(MyselfAttribute)Attribute.GetCustomAttribute(info,

typeof(MyselfAttribute));
if (myAttribute != null)
{
//嘿嘿,在运行时查看注释内容,是不是很爽
Console.WriteLine("Name: {0}", myAttribute.Name);
Console.WriteLine("Age: {0}", myAttribute.Age);
Console.WriteLine("Memo of {0} is {1}", myAttribute.Name,

myAttribute.Memo);
myAttribute.ShowName();
}

//多点反射
object obj = Activator.CreateInstance(typeof(Mytest));

MethodInfo mi = tp.GetMethod("SayHello");
mi.Invoke(obj, null);
Console.ReadLine();
}
}
}

啥也别想了,自己做一下试试。

5.2、他山之石

MSDN认为,特性 (Attribute) 描述如何将数据序列化,指定用于强制安全性的特性,并限制实时 (JIT) 编译器的优化,从而使代码易于调试。属性 (Attribute) 还可以记录文件名或代码作者,或在窗体开发阶段控制控件和成员的可见性。

6、结论

Attribute是.NET引入的一大特色技术,但讨论的不是很多,所以拿出自己的体会来分享,希望就这一技术要点进行一番登堂入室的引导。更深层次的应用,例如序列化、程序安全性、设计模式多方面都可以挖掘出闪耀的金子,这就是.NET在技术领域带来的百变魅力吧!希望大家畅所欲言,来完善和补充作者在这方面的不全面和认知上的不深入,那将是作者最大的鼓励和动力。

【相关文章】

【责任编辑:火凤凰 TEL:(010)68476606】

内容导航

分享到:

网友评论TOP5

查看所有评论(

提交评论

  1. 专题:JVM编程语言,你的选择?
  2. 宅男程序员给老婆课程之11:域模型

文章排行

本月本周24小时

热点专题

更多>>

读书

Eclipse插件开发方法与实战
本书分为4个部分共24章,以插件开发为中心,围绕插件开发主要介绍SWT/JFace的应用、插件扩展点的实现,以及GEF、EMF和RCP的相关

51CTO旗下网站

领先的IT技术网站 51CTO 领先的中文存储媒体 WatchStor 中国首个CIO网站 CIOage 中国首家数字医疗网站 HC3i 移动互联网生活门户 灵客风LinkPhone