我们有时候需要遍历类中的变量与值,对应方法如下:
using System.Reflection;//引用库
//对应方法
public void ParsingClass(System.Object a)
{
foreach (PropertyInfo info in a.GetType().GetProperties())
{
Debug.Log(info.Name + " " + a.GetType().GetProperty(info.Name).GetValue(a, null));
}
}
注意的是,传入的变量要公开封装,示例:
public class AA
{
public string a { get; set; }
public string b { get; set; }
}