反射访问私有成员变量

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

public class NumberInfo
{
    private static  string  aaa ="sdjfhkjsd";
}
public class Test : MonoBehaviour
{
    void Start()
    {
        // 指明当前对象  
        object obj = new NumberInfo();
        // 获取对象的类型  
        Type type = obj.GetType();
        //字段绑定标志  
        BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public|BindingFlags.Static;
        // 获取type 中的 私有变量a  
        FieldInfo ff = type.GetField("aaa", flag);
        // 读取变量值  
         object value = ff.GetValue(obj);
        Debug.Log(ff+" "+value);
        
    }
  
}

posted @ 2018-09-08 17:12  低小调  阅读(86)  评论(0编辑  收藏  举报