C# 特性学习(二)
2014-06-07 17:47
253 查看
自定义了一个特性类:
[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;
}
}
运用特性类:
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;
}
}
}
处理特性类:
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;
[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;
}
}
运用特性类:
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;
}
}
}
处理特性类:
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;
相关文章推荐
- C# 特性的学习(一)
- C# 基本特性
- C# Windows form 学习积累《一》
- Java学习笔记22 jdk5.0新特性
- C# 特性(Attribute)学习
- DotNet 3.0+ 新特性学习 之 匿名方法&Lambda表达式(Anonymous Method&Lambda Expression)
- C#基础学习日志===>Method<===
- 我的C# 3.0之新特性学习笔记(2)
- C#基础学习日志===>数组<===
- C# 根据反射和特性实现ORM 映射[学习]
- C# 特性(Attribute)学习
- C#学习笔记--类的重要特性:继承(转载)
- 学习中C#中···
- 20100626 学习记录:奇怪的stringbuilder && 过滤字符串中的非数字字符 && C#中Trim()、TrimStart()、TrimEnd()的错误认识
- 二、 C#学习心得体会
- C# 3 新特性 Linq 学习笔记(一)
- C#基础学习日志===>命名空间的使用<===
- 重新过一遍ASP.NET 2.0(C#)(4) - Cache&SqlCacheDependency(缓存和SqlCacheDependency特性)(转帖)
- C# 特性(Attribute)学习。
- C#==> 泛型类的几点特性