zoukankan      html  css  js  c++  java
  • Unity c#反射查找类中符合条件的方法并执行

    我用在了事件注册上面,再也不用一个一个去写了

    下面直接上代码

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Reflection;
    using UnityEngine;
    
    public class Main : MonoBehaviour {
    
        // Use this for initialization
        void Start () {
            Type t = GetType();
    
            //BindingFlags作为一个特别的标志量,在反射中,通过这个标志量,可以指定搜索到的成员的类型. 
            //Instance   指定实例成员将包括在搜索中。
            //Public       指定Public修饰的成员
            //NonPublic  指定非Public修饰的成员
            //DeclaredOnly  指定只应考虑在所提供类型的层次结构级别上声明的成员。不考虑继承成员。(就是当前类,不包括父类)
            MethodInfo[] a = t.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
    
            foreach (MethodInfo item in a)
            {
                Debug.Log(item.Name);
                //搜索带req的方法
                if (item.Name.Contains("req"))
                {
                    //执行
                    item.Invoke(this, new object[] { });
                }
            }
        }
        
    
        public void reqTest()
        {
            Debug.Log("测试1");
        }
    
        private void reqTest2()
        {
            Debug.Log("测试2");
        }
    }
  • 相关阅读:
    基本语句
    mysql多表查询方法(join)
    MySQL JOIN 多表连接
    MySQL SHOW INDEX语法的实际应用
    1.索引作用
    MySQL索引和优化查询
    mysql复合索引、普通索引总结
    mysql 索引相关
    for循环的break和continue
    保护程序猿滴眼睛---修改VS 2012 编辑器颜色
  • 原文地址:https://www.cnblogs.com/sanyejun/p/8810149.html
Copyright © 2011-2022 走看看