初学Attribute(一个简单实例)
2004-04-07 17:02
459 查看
// A simple attribute example
using System;
using System.Reflection;
[AttributeUsage( AttributeTargets.All )]
//指定可以对它们应用特性的应用程序元素。
//可以通过按位“或”运算组合 AttributeTargets 枚举值来获得首选组合。
public class RemarkAttribute : Attribute
//所有特性都从Attribute类继承
{
string pri_remark ; // underlies remark property
public RemarkAttribute( string comment )
{
pri_remark = comment ;
}
public string remark
{
get
{
return pri_remark;
}
}
}
[RemarkAttribute( "This class uses an attribute." )]
//应用自定义特性
class UseAttrib
{
//...
}
class AttribDemo
{
public static void Main()
{
Type t = typeof( UseAttrib );
Console.Write( "Attributes in " + t.Name + ": " );
object[] attribs = t.GetCustomAttributes(false);
//获取所有UseAttrib类的特性
foreach( object o in attribs )
{
Console.WriteLine(o);
}
Console.Write( "Remark: " );
//Retrieve the RemarkAttribute.
Type remarkAttribute = typeof( RemarkAttribute );
RemarkAttribute ra=( RemarkAttribute ) Attribute.GetCustomAttribute( t , remarkAttribute );
//获取RemarkAttribute属性
Console.WriteLine( ra.remark );
Console.Read()
}
}
获取Attributes:
object[] GetCustomAttributes( bool searchBases )
static Attribute GetCustomAttribute( MemberInfo mi,Type attribtype )
最初开始关注特性,是写自定义控件的时候,对那些属性应用的特性,例如
CategoryAttribute
DescriptionAttribute
DefaultValueAttribute
ReadOnlyAttribute
但是特性到底有什么用,这个问题一直困扰着我!除了给一些类以特别的信息之外还有什么用?
using System;
using System.Reflection;
[AttributeUsage( AttributeTargets.All )]
//指定可以对它们应用特性的应用程序元素。
//可以通过按位“或”运算组合 AttributeTargets 枚举值来获得首选组合。
public class RemarkAttribute : Attribute
//所有特性都从Attribute类继承
{
string pri_remark ; // underlies remark property
public RemarkAttribute( string comment )
{
pri_remark = comment ;
}
public string remark
{
get
{
return pri_remark;
}
}
}
[RemarkAttribute( "This class uses an attribute." )]
//应用自定义特性
class UseAttrib
{
//...
}
class AttribDemo
{
public static void Main()
{
Type t = typeof( UseAttrib );
Console.Write( "Attributes in " + t.Name + ": " );
object[] attribs = t.GetCustomAttributes(false);
//获取所有UseAttrib类的特性
foreach( object o in attribs )
{
Console.WriteLine(o);
}
Console.Write( "Remark: " );
//Retrieve the RemarkAttribute.
Type remarkAttribute = typeof( RemarkAttribute );
RemarkAttribute ra=( RemarkAttribute ) Attribute.GetCustomAttribute( t , remarkAttribute );
//获取RemarkAttribute属性
Console.WriteLine( ra.remark );
Console.Read()
}
}
获取Attributes:
object[] GetCustomAttributes( bool searchBases )
static Attribute GetCustomAttribute( MemberInfo mi,Type attribtype )
最初开始关注特性,是写自定义控件的时候,对那些属性应用的特性,例如
CategoryAttribute
DescriptionAttribute
DefaultValueAttribute
ReadOnlyAttribute
但是特性到底有什么用,这个问题一直困扰着我!除了给一些类以特别的信息之外还有什么用?
相关文章推荐
- sql plus创建一个简单的存储过程实例
- 一个简单的socket监听的服务器实例
- C语言概述-C语言的一个简单实例
- 简单的存储过程实例2--对比三个表中的数据,将三个表中金额挑选一个最优值
- JS调用OC简单实例-传递一个或者多个参数给oc方法
- 《Applications=Code+Markup》读书札记(2)——创建一个简单的 WPF 程序的代码结构及关于 Window 实例位置设置问题
- 【Android 网络数据解析实现一个简单的新闻实例(一)】
- Spring+SpringMVC+Hiberate+MySql+Bootstrap实现一个简单验证登录实例
- Hibernate4最简单一个项目实例
- 一个简单的DWR框架实例
- JMeter入门(2):一个简单实例
- C#使用TcpListener及TcpClient开发一个简单的Chat工具实例
- java swing最简单实例(1) 一个空的JFrame
- 一个简单的读写注册表实例
- 一个简单的JMS实例(使用ActiveMQ)
- 一个Ajax简单实例分析
- C++中time时间函数的一个简单实例
- 一个简单的jsp+servlet实例,实现简单的登录
- 一个简单的jQuery插件制作 学习过程及实例
- android一个简单的线程实例