C#利用反射遍历类或者结构体中的每一个字段的属性 类型 值
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Reflection; using System; public class ProductInfo{ public char a = 'b'; public int b = 4; public bool c =false; } public class test : MonoBehaviour { // Use this for initialization void Start () { ProductInfo pro = new ProductInfo (); Type type = typeof(ProductInfo); FieldInfo[] fields = type.GetFields(); foreach (FieldInfo f in fields) { Debug.Log ("属性 "+f.Attributes+" "+f+"="+f.GetValue (pro)); // Debug.Log(f.Name); } } // Update is called once per frame void Update () { } }
运行结果