xLua icon indicating copy to clipboard operation
xLua copied to clipboard

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

Open iamabigartist opened this issue 1 year ago • 0 comments

// 配置某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

iamabigartist avatar Apr 14 '24 09:04 iamabigartist