C# 反射 Type.GetFields 方法

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Reflection;

public class Test01 : MonoBehaviour {

    void Start ()
    {
        FieldInfo[] array =  GetComponent<Test01>().GetType().GetFields();
        foreach (var item in array)
        {
            print("字段名称:" + item);
        }
    }

    [DataStoreSave]
    public string str = "abc";
    [DataStoreSave]
    public int i = 5;
    [DataStoreSave]
    public bool b = true;
    [DataStoreSave]
    public Vector3 V3 = new Vector3(4, 5, 6);
}

结果如下图:

 

Type.GetFields

返回当前 Type 的所有公共字段。

注意要引用命名空间:using System.Reflection;

 

posted @ 2018-03-21 19:46  朋丶Peng  阅读(6966)  评论(0编辑  收藏  举报