C#特性学习笔记二
2014-03-20 10:22
211 查看
实例探讨:
自定义了一个特性类:
[c-sharp] view plaincopyprint?
[AttributeUsage(AttributeTargets.Class|AttributeTargets.Method)]
class WahAttribute:System.Attribute
{
private string description;
public string Description
{
get { return description; }
set { description = value; }
}
private string author;
public string Author
{
get { return author; }
set { author = value; }
}
public WahAttribute(string desc)
{
this.description = desc;
}
}
处理特性类:
[c-sharp] view plaincopyprint?
private void button_Click(object sender, EventArgs e)
{
//得到类型
Type type = typeof(Teacher);
//得到此类型所有方法
MethodInfo[] methods = type.GetMethods();
foreach (MethodInfo method in methods)
{
//得到此方法的所有特性
object[] attributes = method.GetCustomAttributes(false);
foreach (object o in attributes)
{
//判断是否是自己定义的特性
if (o.GetType() == typeof(WahAttribute))
{
//强转取得值
WahAttribute waha = (WahAttribute)o;
this.listBox1.Items.Add("author=" + waha.Author);
this.listBox1.Items.Add("description=" + waha.Description);
}
}
}
}
private void button_Click(object sender, EventArgs e)
{
//得到类型
Type type = typeof(Teacher);
//得到此类型所有方法
MethodInfo[] methods = type.GetMethods();
foreach (MethodInfo method in methods)
{
//得到此方法的所有特性
object[] attributes = method.GetCustomAttributes(false);
foreach (object o in attributes)
{
//判断是否是自己定义的特性
if (o.GetType() == typeof(WahAttribute))
{
//强转取得值
WahAttribute waha = (WahAttribute)o;
this.listBox1.Items.Add("author=" + waha.Author);
this.listBox1.Items.Add("description=" + waha.Description);
}
}
}
}
C#特性可以应用于各种类型和成员。加在类前面的是类特性,加在方法前面的是方法特性。无论他们被用在哪里,无论他们之间有什么区别,特性的最主要的目的就是自描述。并且因为特性是可以由自己定制的,而不仅仅局限于.net提供的那几个现成的,因此给C#程序开发带来了很大的灵活性。
自定义了一个特性类:
[c-sharp] view plaincopyprint?
[AttributeUsage(AttributeTargets.Class|AttributeTargets.Method)]
class WahAttribute:System.Attribute
{
private string description;
public string Description
{
get { return description; }
set { description = value; }
}
private string author;
public string Author
{
get { return author; }
set { author = value; }
}
public WahAttribute(string desc)
{
this.description = desc;
}
}
[c-sharp] view plaincopyprint? namespace attributeDemo { public class Teacher { private string name; public string Name { get { return name; } set { name = value; } } private int age; public int Age { get { return age; } set { age = value; } } private string sex; public string Sex { get { return sex; } set { sex = value; } } //只有用户名为wah的才可以调用此方法 [Wah("this is my attribute test", Author = "wah", Description = "test")] public string getMessage() { return "好好学习,天天向上"; } [Wah("this is with parameters test",Author="wanggaihui",Description="test with parameters")] public int Test(int a,int b) { return a+b; } } } namespace attributeDemo { public class Teacher { private string name; public string Name { get { return name; } set { name = value; } } private int age; public int Age { get { return age; } set { age = value; } } private string sex; public string Sex { get { return sex; } set { sex = value; } } //只有用户名为wah的才可以调用此方法 [Wah("this is my attribute test", Author = "wah", Description = "test")] public string getMessage() { return "好好学习,天天向上"; } [Wah("this is with parameters test",Author="wanggaihui",Description="test with parameters")] public int Test(int a,int b) { return a+b; } } }
处理特性类:
[c-sharp] view plaincopyprint?
private void button_Click(object sender, EventArgs e)
{
//得到类型
Type type = typeof(Teacher);
//得到此类型所有方法
MethodInfo[] methods = type.GetMethods();
foreach (MethodInfo method in methods)
{
//得到此方法的所有特性
object[] attributes = method.GetCustomAttributes(false);
foreach (object o in attributes)
{
//判断是否是自己定义的特性
if (o.GetType() == typeof(WahAttribute))
{
//强转取得值
WahAttribute waha = (WahAttribute)o;
this.listBox1.Items.Add("author=" + waha.Author);
this.listBox1.Items.Add("description=" + waha.Description);
}
}
}
}
private void button_Click(object sender, EventArgs e)
{
//得到类型
Type type = typeof(Teacher);
//得到此类型所有方法
MethodInfo[] methods = type.GetMethods();
foreach (MethodInfo method in methods)
{
//得到此方法的所有特性
object[] attributes = method.GetCustomAttributes(false);
foreach (object o in attributes)
{
//判断是否是自己定义的特性
if (o.GetType() == typeof(WahAttribute))
{
//强转取得值
WahAttribute waha = (WahAttribute)o;
this.listBox1.Items.Add("author=" + waha.Author);
this.listBox1.Items.Add("description=" + waha.Description);
}
}
}
}
C#特性可以应用于各种类型和成员。加在类前面的是类特性,加在方法前面的是方法特性。无论他们被用在哪里,无论他们之间有什么区别,特性的最主要的目的就是自描述。并且因为特性是可以由自己定制的,而不仅仅局限于.net提供的那几个现成的,因此给C#程序开发带来了很大的灵活性。
相关文章推荐
- JAVA学习笔记37——泛型3:泛型嵌套+泛型数组+1.7新特性
- C++11新特性学习笔记
- spring学习笔记:ioc容器高级特性
- HTML5+CSS3学习笔记(二) 页面布局:HTML5新元素及其特性
- MySQL学习笔记——存储引擎的索引特性
- 【Java学习笔记之二十八】深入了解Java8新特性
- C++11新特性学习笔记
- C++11新特性学习笔记(一)
- JDK 1.5 新特性学习笔记(1)
- Python3 学习笔记16_高级特性(列表生成式)_20180307
- C++11新特性学习笔记—noexcept关键字
- java学习笔记-类, 面向对象特性,包机制
- Java学习笔记---类的继承与多态特性
- 【JavaSE学习笔记】JDK1.8常用新特性
- HTML入门学习笔记--CSS三大特性(4)
- Spring学习笔记-IOC高级特性3-用Java Config简化XML配置
- C++学习笔记->面向对象的三个特性
- JavaScript高级程序设计(第3版)学习笔记13 ECMAScript5新特性
- C#3.0 新特性学习笔记(4):扩展方法
- 流畅python学习笔记:第十九章:动态属性和特性