Home>

In the previous question , we showed you how to fetch fields in bulk
I want to get access modifier or static for the fetched field.
Documentation, I don't know the method.

Applicable source code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Reflection;
public class Model
{
    public int mId = 0;
    public string mName = "";
    public string mPath = "";
    public int mType = 0;
    private int i = 10;
    static int si = 3;
    public static int psi = 5;
}
public class Sample: MonoBehaviour
{
    void Start ()
    {
        Model m = new Model ();
        m.mId = 1;
        m.mName = "name";
        m.mPath = "root";
        m.mType = 2;
        FieldInfo [] infoArray = m.GetType (). GetFields (BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
        foreach (FieldInfo info in infoArray)
        {
            Debug.Log (info. (Access modifier) ​​+ info.Name + ":" + info.GetValue (m));
        }
    }
}
Tried

Document ,
I found properties such as IsPrivate, IsPublic, IsStatic.
I think that it is possible to output conditional branching with these properties, but
If this is the case, the code will be long, so is it possible to get access modifiers and static with the Get ~ method, etc.?

  • Answer # 1

    There is a FieldInfo.Attributes property.
    This is an enumerated type that indicates the state by a combination of bits, so if you make it a string, you can get the string of the member whose flag is ON.

    Debug.Log (info.Attributes + "" + info.Name + ":" + info.GetValue (m));
    // result obtained
    // Public mId: 1
    // Public mName: name
    // Public mPath: root
    // Public mType: 2
    // Private i: 10
    // Private, Static si: 3
    // Public, Static psi: 5


    If you do, I think that you will get something similar to the result you want.