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);
}
}
}
}