C#属性编程
2004-11-22 14:06
495 查看
Attribute首先是一个类,在C#中, Attribute是作为编译器指令来处理的
在.NET中,属性的作用非常重要,无论是写WEB控件或是WEB服务,属性的作用几乎不可或缺,而序列化.程序安装特征等更离不开属性,看上去很神秘,其实写一个属于自己的属性也不难,在CodeProject和C#Corner上都有类似的示范代码.这下面只是普通属性,如果是AOP,则需要从ContextAttribute中继承,关于AOP以及ContextAttribute,以后的文章将会专门讲述,Code Project上也有相关的例子
using System;
namespace Test
{
// Only allow this attribute to be added to classes
[AttributeUsage(AttributeTargets.Class)]
public class TestAttribute : Attribute
{
// A number with some imaginary importance
public int TheNumber;
// A string that could be useful somewhere
public string Name;
// The only constructor requiring that
// TheNumber be set
public TestAttribute(int TheNumber)
{
this.TheNumber = TheNumber;
Name = "None";
}
// Method to illustrate that an attribute is really just
// a class at heart. This will be used in Driver.cs
public void PrintOut()
{
Console.WriteLine("/tTheNumber = {0}", TheNumber);
Console.WriteLine("/tName = /"{0}/"", Name);
}
}
}
上面就是一个属性类了,很简单的,注意的是,属性类前面必须加上AttributeUsage属性描述,里面的AttributeTargets定义了该属性的应用范围,比如只应用于方法还是类还是全部适用,在上面这段代码中,设置该属性只能被类使用
你就可以在自己的类中用上自己的属性了,比如
[Test(4, Name = "TestClassB")]
public class TestMyAtt
{}
仔细看,首先在TestAttribute 类的构造方法中,需要初始化TheNumber属性,所以,在用TestAttribute的时候,必须有整数值,而Name 根据实际情况则可有可无了
实现自己的属性其实还是很有用处的,个人认为对实现AOP模式是大有裨益的.
在.NET中,属性的作用非常重要,无论是写WEB控件或是WEB服务,属性的作用几乎不可或缺,而序列化.程序安装特征等更离不开属性,看上去很神秘,其实写一个属于自己的属性也不难,在CodeProject和C#Corner上都有类似的示范代码.这下面只是普通属性,如果是AOP,则需要从ContextAttribute中继承,关于AOP以及ContextAttribute,以后的文章将会专门讲述,Code Project上也有相关的例子
using System;
namespace Test
{
// Only allow this attribute to be added to classes
[AttributeUsage(AttributeTargets.Class)]
public class TestAttribute : Attribute
{
// A number with some imaginary importance
public int TheNumber;
// A string that could be useful somewhere
public string Name;
// The only constructor requiring that
// TheNumber be set
public TestAttribute(int TheNumber)
{
this.TheNumber = TheNumber;
Name = "None";
}
// Method to illustrate that an attribute is really just
// a class at heart. This will be used in Driver.cs
public void PrintOut()
{
Console.WriteLine("/tTheNumber = {0}", TheNumber);
Console.WriteLine("/tName = /"{0}/"", Name);
}
}
}
上面就是一个属性类了,很简单的,注意的是,属性类前面必须加上AttributeUsage属性描述,里面的AttributeTargets定义了该属性的应用范围,比如只应用于方法还是类还是全部适用,在上面这段代码中,设置该属性只能被类使用
你就可以在自己的类中用上自己的属性了,比如
[Test(4, Name = "TestClassB")]
public class TestMyAtt
{}
仔细看,首先在TestAttribute 类的构造方法中,需要初始化TheNumber属性,所以,在用TestAttribute的时候,必须有整数值,而Name 根据实际情况则可有可无了
实现自己的属性其实还是很有用处的,个人认为对实现AOP模式是大有裨益的.
相关文章推荐
- C#首席设计师Anders Hejlsberg专访
- [C#]Windows服务程序[转载西门吹雪的好文章]
- 设计模式-简单工厂模式(SimpleFactory-C#)
- 软件设计模式C#
- Bridge Pattern using C#
- 用C#编写发手机中文短信息Windows服务
- Changing IE setting in C#
- Inside c#_Chapter1(02)
- 使用 Visual C# .NET 向 Microsoft Excel 2002 传输 XML 数据
- 天天学C#-委托(delegate)
- Inside c#_Chapter1(01)
- C#使用CDO发送邮件
- Proxy Pattern using C#
- P/Invoke
- c#中在DataGrid里创建一个弹出式窗口------可实现源代码
- 刀兄写的IIS管理类(C#) 选择自 iwebsms 的 Blog
- C#实现的18位身份证格式验证算法[转载] 选择自 mYang 的 Blog
- C# 编码规范和编程好习惯
- c#中结构与类的区别
- [翻译]C#中的泛型 (From dotNet SDK 2.0 Beta1)