unity中利用反射遍历类或者结构体中的每一个字段属性 、类型 、值

来源:互联网 发布:s90pos机无法连接网络 编辑:程序博客网 时间:2024/06/06 13:25
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 initializationvoid 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 framevoid Update () {}}

运行结果

阅读全文
0 0
原创粉丝点击