C#中的属性类
来源:互联网 发布:徐州公交查询软件 编辑:程序博客网 时间:2024/06/04 08:17
这个问题来自论坛提问,对于初学者来说确实比较陌生,msdn参考文档
http://msdn.microsoft.com/zh-cn/library/system.attribute.aspx
我们先直观的表达一下他的作用之一:
比如一个自定义windows控件的DefaultEvent可以表示一个控件默认事件:
如你双击form他会默认到onLoad事件中,双击button,他会默认到onclick事件中。
一个简单的测试,你添加一个windows窗体1叫Form1,然后增加如下代码
[DefaultEvent("FormClosed")]
public partial class Form1 : Form
public partial class Form1 : Form
然后新建一个窗体2,继承自窗体1
public partial class Form2 : Form1
然后把Form2切换到设计器的模式,然后双击Form2的工作区域,会直接定位到代码的
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
...{
}
...{
}
而不是先前的
private void Form2_Load(object sender, EventArgs e)
...{
}
...{
}
对于这个语法比较好奇的朋友请仔细阅读上面msdn的参考文档.
简要的说他不是系统规定死的写法,是属性类,
- 可以在运行期去决定做什么,就像一般的类.
- 除了系统已经定义的还可以自己定义.
- 中括号中的语法其实就是一个类的构造函数的调用.
- 我们可以通过反射得到属性类的实例.
- 属性类可以修饰类,变量,属性,成员函数,事件等
运行下面的测试代码,可能更加直观的理解他,下面展现了如何自己定义属性类并使用.
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication11
...{
class Program
...{
static void Main(string[] args)
...{
//获取类上的属性类
foreach (object obj in typeof(TestClass).GetCustomAttributes(false))
...{
Console.WriteLine(obj.GetType().Name + ":" + obj.ToString());
}
//获取字段属性类
foreach (object obj in typeof(TestClass).GetField("_aa").GetCustomAttributes(typeof(Test1), true))
...{
Console.WriteLine(obj.GetType().Name + ":" + obj.ToString());
}
//获取属性属性类
foreach (object obj in typeof(TestClass).GetProperty("aa").GetCustomAttributes(typeof(Test1), true))
...{
Console.WriteLine(obj.GetType().Name + ":" + obj.ToString());
}
//获取方法属性类
foreach (object obj in typeof(TestClass).GetMethod("test").GetCustomAttributes(typeof(Test1), true))
...{
Console.WriteLine(obj.GetType().Name + ":" + obj.ToString());
}
//获取字事件性类
foreach (object obj in typeof(TestClass).GetEvent("onTest").GetCustomAttributes(typeof(Test1), true))
...{
Console.WriteLine(obj.GetType().Name + ":" + obj.ToString());
}
Console.Read();
}
}
//测试属性类,传入string
public class Test1 : System.Attribute
...{
string strName = "";
public Test1() ...{ }
public Test1(string str)
...{
strName = str;
}
public override string ToString()
...{
return strName;
}
}
//测试属性类,传入int
public class Test2 : Attribute
...{
int _f;
public Test2(int f) ...{ _f = f; }
public override string ToString()
...{
return _f.ToString();
}
}
//给class测试
[Test1("jinjazz for TestClass")]
[Test2(12356)]
public class TestClass
...{
//给字段和属性测试
[Test1("jinjazz for _aa字段 ")]
public string _aa = "";
[Test1("jinjazz for aa 属性")]
public string aa
...{
get ...{ return _aa; }
}
//给方法和事件测试
[Test1("jinjazz for test 方法")]
public void test() ...{ }
[Test1("jinjazz for onTest 事件")]
public event System.EventHandler onTest;
}
}
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication11
...{
class Program
...{
static void Main(string[] args)
...{
//获取类上的属性类
foreach (object obj in typeof(TestClass).GetCustomAttributes(false))
...{
Console.WriteLine(obj.GetType().Name + ":" + obj.ToString());
}
//获取字段属性类
foreach (object obj in typeof(TestClass).GetField("_aa").GetCustomAttributes(typeof(Test1), true))
...{
Console.WriteLine(obj.GetType().Name + ":" + obj.ToString());
}
//获取属性属性类
foreach (object obj in typeof(TestClass).GetProperty("aa").GetCustomAttributes(typeof(Test1), true))
...{
Console.WriteLine(obj.GetType().Name + ":" + obj.ToString());
}
//获取方法属性类
foreach (object obj in typeof(TestClass).GetMethod("test").GetCustomAttributes(typeof(Test1), true))
...{
Console.WriteLine(obj.GetType().Name + ":" + obj.ToString());
}
//获取字事件性类
foreach (object obj in typeof(TestClass).GetEvent("onTest").GetCustomAttributes(typeof(Test1), true))
...{
Console.WriteLine(obj.GetType().Name + ":" + obj.ToString());
}
Console.Read();
}
}
//测试属性类,传入string
public class Test1 : System.Attribute
...{
string strName = "";
public Test1() ...{ }
public Test1(string str)
...{
strName = str;
}
public override string ToString()
...{
return strName;
}
}
//测试属性类,传入int
public class Test2 : Attribute
...{
int _f;
public Test2(int f) ...{ _f = f; }
public override string ToString()
...{
return _f.ToString();
}
}
//给class测试
[Test1("jinjazz for TestClass")]
[Test2(12356)]
public class TestClass
...{
//给字段和属性测试
[Test1("jinjazz for _aa字段 ")]
public string _aa = "";
[Test1("jinjazz for aa 属性")]
public string aa
...{
get ...{ return _aa; }
}
//给方法和事件测试
[Test1("jinjazz for test 方法")]
public void test() ...{ }
[Test1("jinjazz for onTest 事件")]
public event System.EventHandler onTest;
}
}
- C#中的属性类
- C#中的属性类
- C#中的属性类
- C#类中的属性是什么?
- C#中的属性类(Attribute类)
- C#中的属性
- 浅谈C#中的属性
- C#中的属性
- c# 中的只读属性
- c#中的自定义属性
- c#中的属性和字段
- C#中的字段与属性
- C#中的字段与属性
- [C#]C#中的自定义属性
- 黑马程序员--C#中的属性
- C#中的域与属性
- C#接口中的属性用法
- C#中的属性,字段,变量
- NO6::RegisterClass
- Tomcat启动分析
- 设计页面学到的东西!
- 新手上路系列之玩转倒车泊车[图解]ZT - 薄荷.红茶o(*^^*)o - 网易博客
- asp.net跳转页面的三种方法比较
- C#中的属性类
- java6.0打开浏览器
- Zju 1109 Language of FatMouse 解题报告
- 流行AJAX框架大比拼http://tech.it168.com/j/2007-04-25/200704250946187.shtml
- Thinking in c#没有想象中那么好!
- Emacs 教程中文版
- 大一就这样过了
- 2008-05-08 在Linux下安装企鹅五笔
- 我的面试生涯