Tencent / xLua

xLua is a lua programming solution for C# ( Unity, .Net, Mono) , it supports android, ios, windows, linux, osx, etc.
Other
9.35k stars 2.45k forks source link

使用ExampleConfig.cs中的热更新Delegate配置,会使C#方法通过反射调用 #1132

Open iamabigartist opened 5 months ago

iamabigartist commented 5 months ago
// 配置某Assembly下所有涉及到的delegate到CSharpCallLua下,Hotfix下拿不准那些delegate需要适配到lua function可以这么配置
    [CSharpCallLua]
    static IEnumerable<Type> AllDelegate
    {
        get
        {
            BindingFlags flag = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public;
            List<Type> allTypes = new List<Type>();
            var allAssemblys = new Assembly[]
            {
                Assembly.Load("Assembly-CSharp")
            };
            foreach (var t in from assembly in allAssemblys from type in assembly.GetTypes() select type)
            {
                var p = t;
                while (p != null)
                {
                    allTypes.Add(p);
                    p = p.BaseType;
                }
            }
            allTypes = allTypes.Distinct().ToList();
            var allMethods = from type in allTypes
                from method in type.GetMethods(flag)
                select method;
            var returnTypes = from method in allMethods
                select method.ReturnType;
            var paramTypes = allMethods.SelectMany(m => m.GetParameters()).Select(pinfo => pinfo.ParameterType.IsByRef ? pinfo.ParameterType.GetElementType() : pinfo.ParameterType);
            var fieldTypes = from type in allTypes
                from field in type.GetFields(flag)
                select field.FieldType;
            return returnTypes.Concat(paramTypes).Concat(fieldTypes).Where(t => t.BaseType == typeof(MulticastDelegate) && !hasGenericParameter(t) && !delegateHasEditorRef(t)).Distinct();
        }
    }

开启符号NOT_GEN_WARNING,然后运行Example/09_GenericMethod,会提示方法通过反射调用。 image