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

本文详细介绍了在Unity引擎中如何利用反射技术遍历类和结构体的所有字段属性,包括其类型和值。通过示例代码展示了反射在游戏开发中的应用,帮助开发者更深入理解Unity的编程技巧。
摘要由CSDN通过智能技术生成
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 () {
		
	}
}

运行结果

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值