前文说到了反射获得类中的成员信息。
这篇对类中的成员信息进行访问。
使用到的例子类如下:
public class Person
{
private int _test3;
private int _test1 { get; set; }
protected int Test2 { get; set; }
public int Test3 { get; set; }
private string name;
private string str { set; get; }
public void Show()
{
}
private static void Show2()
{
}
public static void Show3()
{
}
}
假设我们是不知道类的内部的。
首先对字段的访问:
获取字段
using UnityEngine;
using System.Collections;
using System;
using System.Reflection;
namespace Reflection1
{
public class Reflection_Test1 : MonoBehaviour
{
// Use this for initialization
void Start()
{
Type t = typeof(Person);
//仅获取字段
FieldInfo[] minfos = t.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Static);
foreach (FieldInfo f in minfos)
{
Debug.Log("字段名称:" + f.Name + " 类型: " + f.FieldType.ToString());
}
}
// Update is called once per frame
void Update()
{
}
}
}
获取字段的值
using UnityEngine;
using System.Collections;
using System;
using System.Reflection;
namespace Reflection1
{
public class Reflection_Test1 : MonoBehaviour
{
public int whichDebug = 0;
// Use this for initialization
void Start()
{
Type t = typeof(Person);
//获取字段的值
Person p = new Person();
p.Test3 = 3;
FieldInfo[] f = t.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
foreach (FieldInfo fi in f)
{
Debug.Log("字段名称:" + fi.Name + " 字段类型:" + fi.FieldType.ToString() + " p中的值为:" + fi.GetValue(p));
}
}
// Update is called once per frame
void Update()
{
}
}
}
修改字段的值
using UnityEngine;
using System.Collections;
using System;
using System.Reflection;
namespace Reflection1
{
public class Reflection_Test1 : MonoBehaviour
{
public int whichDebug = 0;
// Use this for initialization
void Start()
{
Type t = typeof(Person);
//修改字段的值
Person p = new Person();
p.Test3 = 3;
FieldInfo[] f = t.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
foreach (FieldInfo fi in f)
{
if (fi.FieldType == typeof(string))
fi.SetValue(p, "Jim");
else if (fi.FieldType == typeof(int))
fi.SetValue(p, 100);
Debug.Log(string.Format("字段名称:{0} 字段类型:{1} p中的值:{2}", fi.Name, fi.FieldType.ToString(), fi.GetValue(p)));
}
}
// Update is called once per frame
void Update()
{
}
}
}
输出:
获取并修改属性的值
using UnityEngine;
using System.Collections;
using System;
using System.Reflection;
namespace Reflection1
{
public class Reflection_Test1 : MonoBehaviour
{
// Use this for initialization
void Start()
{
Type t = typeof(Person);
//获取并修改属性的值
Person p = new Person();
p.Test3 = 3;
PropertyInfo[] f = t.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
foreach (PropertyInfo fi in f)
{
//get
MethodInfo getInfo = fi.GetGetMethod(true); //true 包含非公共成员
Debug.Log(string.Format("get方法的名称{0} 返回值类型:{1} 参数数量:{2} MSIL代码长度:{3} 局部变量数量:{4}",
getInfo.Name,
getInfo.ReturnType.ToString(),
getInfo.GetParameters().Length,
getInfo.GetMethodBody().GetILAsByteArray().Length,
getInfo.GetMethodBody().LocalVariables.Count));
//set
MethodInfo setInfo = fi.GetSetMethod(true);
Debug.Log(string.Format("set方法的名称{0} 返回值类型:{1} 参数数量:{2} MSIL代码长度:{3} 局部变量数量:{4}",
setInfo.Name,
setInfo.ReturnType.ToString(),
setInfo.GetParameters().Length,
setInfo.GetMethodBody().GetILAsByteArray().Length,
setInfo.GetMethodBody().LocalVariables.Count));
//设置值
if (fi.PropertyType == typeof(int))
setInfo.Invoke(p, new object[] { 123 });
else if (fi.PropertyType == typeof(string))
setInfo.Invoke(p, new object[] { "hello" });
object obj = getInfo.Invoke(p, null);
Debug.Log(string.Format("属性名:{0} 内部值:{1}", fi.Name, obj));
}
}
// Update is called once per frame
void Update()
{
}
}
}
获取方法
将例子类修改如下:
public class Person
{
private int _test3;
private int _test1 { get; set; }
protected int Test2 { get; set; }
public int Test3 { get; set; }
private string name;
public string Show(string str)
{
string name;
return str;
}
private static void Show2()
{
}
public static string Show3(string str)
{
int a;
int b;
return str;
}
}
代码
using UnityEngine;
using System.Collections;
using System;
using System.Reflection;
namespace Reflection2
{
public class Reflection_Test2 : MonoBehaviour
{
// Use this for initialization
void Start()
{
Type t = typeof(Person);
Person p = new Person();
p.Test3 = 3;
MethodInfo[] m = t.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Static);
foreach (MethodInfo f in m)
{
if (f.GetParameters().Length > 0 && f.GetParameters()[0].ParameterType == typeof(string))
{
object obj = f.Invoke(p, new[] { "hello" });
MethodBody mb = f.GetMethodBody();
Debug.Log(string.Format("拥有参数的方法名:{0} 返回值类型:{1} 参数1类型:{2} 参数1名称:{3} 方法调用后返回的值:{4}",
f.Name,
f.ReturnType.ToString(),
f.GetParameters()[0].ParameterType.ToString(),
f.GetParameters()[0].Name,
obj.ToString()));
}
else
{
MethodBody mb = f.GetMethodBody();
Debug.Log(string.Format("没有参数的方法名: {0} 返回值类型:{1}",
f.Name,
f.ReturnType.ToString()));
}
}
}
// Update is called once per frame
void Update()
{
}
}
}