Attribute特性学习笔记

1 得到type
通过  typeof(类) 或者 对象.GetType(), 或者Assembly.GetType("name")

2.得到函数,字段,属性等的信息。注意只有public的才能获取。
字段 FieldInfo通过 type.GetField("num");
属性 PropertyInfo 通过 type.GetProperties());
方法 MethodInfo 通过 type.GetMethods();

3 得到特性 
Attribute.GetCustomAttributes(mInfo)

4.处理

using UnityEngine;
using System.Collections;
using System.Reflection;
using System;

public class CodeAtt:Attribute
{
public string reviewer;
public string time;
public CodeAtt(string _reviewer,string _time)
{
reviewer = _reviewer;
time = _time;
}
}
public class SomeClass
{
[CodeAtt("王五","20140623")]
public void Cal()
{

}

[CodeAtt("李四","20140306")]
public void Display()
{
}
}


public class CheckCode : MonoBehaviour {
void Start()
{
Type t = typeof(SomeClass);
foreach(var info in t.GetMethods())
{
foreach(var att in info.GetCustomAttributes(typeof(CodeAtt),false))
{
CodeAtt codeAtt = att as CodeAtt;
string s = string.Format("最近一次{0}在{1}检查了{2}方法",codeAtt.reviewer,codeAtt.time,info.Name);
Debug.Log(s);
}
}
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值