C#---高级|反射

【千锋合集】史上最全Unity3D全套教程|匠心之作_哔哩哔哩_bilibili

命名空间不存在父子关系

 

 

 

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

public class Singleton
{
    public static Singleton instance;
    public static Singleton GetInstance()
    {
        if (instance == null)
        {
            instance = new Singleton();
        }
        return instance;
    }
    private Singleton() { }
    public Singleton(string name) {
        this.name = name;
    }

    public Singleton(int age) {
        this.age = age;
    }

    private Singleton(string name,int age,string address):this(name) {
        this.age = age;
        this.address = address;
    }
    public string name = "xiaoming";
    public int age;
    public string address;

    private float money = 400;
    private void SetMoney(float money) {
        this.money = money;

    }
    private void SetMoney(string money) {
        this.money = float.Parse(money+"0");
    }

    private void SetMoney(string money,float scale) {
        this.money = float.Parse(money + "0")*scale;
    }


    public float GetMoney() {
        Debug.Log(money);
        return money;
    }
}

public class ReflectionInstance : MonoBehaviour {
    private void Start()
    {
        #region 使用public且无参的构造函数
        ---------------------使用public且无参的构造函数------------------------
        获取类型
        //Type goType = typeof(GameObject);
        用这个类型实例化对象---必会调用构造函数
        //object goObj=Activator.CreateInstance(goType);
        使用的时候需要类型转换
        //(goObj as GameObject).AddComponent<ParticleSystem>();
        #endregion
        #region 使用private且无参的构造函数去实例化
        ---------------------使用private且无参的构造函数去实例化--------------------
        Singleton single=new Singleton();正常情况:私有构造是无法实例化的
        ------------------------------------------------------------------
        //Type singletonType = typeof(Singleton);
        //object sgObj=Activator.CreateInstance(singletonType,true);
        访问单例中的字段
        //Debug.Log((sgObj as Singleton).name);
        #endregion
        #region 使用public且有参的构造函数去实例化
        ---------------------使用public且有参的构造函数去实例化--------------------
        获取类型
        //Type singletonType = typeof(Singleton);
        用public有参的构造函数去实例化对象
        //object stObj=Activator.CreateInstance(singletonType,"laowang");
        打印name
        //Debug.Log((stObj as Singleton).name);
        #endregion
        #region 使用private且有参的构造函数去实例化
        //Type singletonType = typeof(Singleton);
        使用private且有参的构造函数去实例化
        //object obj=Activator.CreateInstance(singletonType,BindingFlags.Instance|BindingFlags.Public,null,new object[] { 12},null);
        //obj = Activator.CreateInstance(singletonType, BindingFlags.Instance | BindingFlags.NonPublic, null, new object[] { "xiaomei",12,"beijing" }, null);
        //Debug.Log((obj as Singleton).name);
        //Debug.Log((obj as Singleton).age);
        //Debug.Log((obj as Singleton).address);
        #endregion
        #region 通过反射访问字段
        获取类型
        //Type sgType = typeof(Singleton);
        获取name字段
        //FieldInfo nameField = sgType.GetField("name");
        实例化单例对象
        //object sgObj=Activator.CreateInstance(sgType, true);
        设置单例对象的name字段值
        //nameField.SetValue(sgObj,"胖枫");
        获取单例对象的name字段的值
        //object nameValue=nameField.GetValue(sgObj);
        打印结果
        //Debug.Log(nameValue);

        获取私有字段
        //FieldInfo moneyField=sgType.GetField("money",BindingFlags.Instance|BindingFlags.NonPublic);
        设置
        //moneyField.SetValue(sgObj,999999999);
        //(sgObj as Singleton).GetMoney();

        //测试不加BindingFlag返回的成员的,还是成员+静态的
        //Type sgType = typeof(Singleton);
        //FieldInfo[] infos = sgType.GetFields();
        //for (int i = 0; i < infos.Length; i++)
        //{
        //    Debug.Log(infos[i].Name);
        //    Debug.Log(infos[i].IsStatic);
        //}
        #endregion

        #region 通过反射访问方法
        Type sgType = typeof(Singleton);
        获取私有成员方法
        //MethodInfo setMoneyMethod=sgType.GetMethod("SetMoney",BindingFlags.Instance|BindingFlags.NonPublic);
        调用方法
        //setMoneyMethod.Invoke(Singleton.GetInstance(),new object[] { 88888});
        //Debug.Log(Singleton.GetInstance().GetMoney());

        如果方法有重载,如何获取?
        //MethodInfo setMoneyMethod = sgType.GetMethod("SetMoney", BindingFlags.Instance | BindingFlags.NonPublic,null,new Type[] { typeof(string)},null);
        //setMoneyMethod.Invoke(Singleton.GetInstance(), new object[] { "88888" });
        //Singleton.GetInstance().GetMoney();


        如果方法有重载,如何获取?
        //MethodInfo setMoneyMethod = sgType.GetMethod("SetMoney", BindingFlags.Instance | BindingFlags.NonPublic,null,new Type[] { typeof(string),typeof(float)},null);
        //setMoneyMethod.Invoke(Singleton.GetInstance(), new object[] { "88888",2 });
        //Singleton.GetInstance().GetMoney();

        //批量调用方法
        //{"methods":["A","B","C"]}
        //InvokeMethod("SetMoney", new Type[] { typeof(string), typeof(float) }, new object[] { "88888", 2 });

        //for (int i = 0; i < 3; i++)
        //{
        //    InvokeMethod("SetMoney", new Type[] { typeof(string), typeof(float) }, new object[] { "88888", 2 });
        //}


        string[] methods = new string[] { "SetName","SetMoney","SetEquips"};
        try
        {
            for (int i = 0; i < methods.Length; i++)
            {
                InvokeMethod(methods[i], new Type[] { typeof(string), typeof(float) }, new object[] { "88888", 2 });
            }
        }
        catch (Exception ex) {

        }
       
        
        
        
        #endregion
    }
    public void InvokeMethod(string methodName,Type[] types, object[] realParams) {
        Type sgType = typeof(Singleton);
        MethodInfo setMoneyMethod = sgType.GetMethod("SetMoney", BindingFlags.Instance | BindingFlags.NonPublic, null, types, null);
        //执行方法
        setMoneyMethod.Invoke(Singleton.GetInstance(), realParams);
        Singleton.GetInstance().GetMoney();

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值