public static void TaggedInstanceMethods(LuaState lua, object o) { if (lua == null) { throw new ArgumentNullException("lua"); } if (o == null) { throw new ArgumentNullException("o"); } MethodInfo[] methods = o.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public); for (int i = 0; i < methods.Length; i++) { MethodInfo methodInfo = methods[i]; object[] customAttributes = methodInfo.GetCustomAttributes(typeof(LuaGlobalAttribute), true); for (int j = 0; j < customAttributes.Length; j++) { LuaGlobalAttribute luaGlobalAttribute = (LuaGlobalAttribute)customAttributes[j]; if (string.IsNullOrEmpty(luaGlobalAttribute.Name)) { lua.RegisterFunction(methodInfo.Name, o, methodInfo); } else { lua.RegisterFunction(luaGlobalAttribute.Name, o, methodInfo); } } } }
public static void TaggedStaticMethods(LuaState lua, Type type) { if (lua == null) { throw new ArgumentNullException("lua"); } if (type == null) { throw new ArgumentNullException("type"); } if (!type.IsClass) { throw new ArgumentException("The type must be a class!", "type"); } MethodInfo[] methods = type.GetMethods(BindingFlags.Static | BindingFlags.Public); for (int i = 0; i < methods.Length; i++) { MethodInfo methodInfo = methods[i]; object[] customAttributes = methodInfo.GetCustomAttributes(typeof(LuaGlobalAttribute), false); for (int j = 0; j < customAttributes.Length; j++) { LuaGlobalAttribute luaGlobalAttribute = (LuaGlobalAttribute)customAttributes[j]; if (string.IsNullOrEmpty(luaGlobalAttribute.Name)) { lua.RegisterFunction(methodInfo.Name, null, methodInfo); } else { lua.RegisterFunction(luaGlobalAttribute.Name, null, methodInfo); } } } }