/// <summary> /// Registers all public static methods in a class tagged with <see cref="LuaGlobalAttribute"/> as Lua global functions /// </summary> /// <param name="lua">The Lua VM to add the methods to</param> /// <param name="type">The class type to get the methods from</param> public static void TaggedStaticMethods(LuaState lua, Type type) { #region Sanity checks 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"); } #endregion foreach (MethodInfo method in type.GetMethods(BindingFlags.Static | BindingFlags.Public)) { foreach (LuaGlobalAttribute attribute in method.GetCustomAttributes(typeof(LuaGlobalAttribute), false)) { if (string.IsNullOrEmpty(attribute.Name)) { lua.RegisterFunction(method.Name, null, method); // CLR name } else { lua.RegisterFunction(attribute.Name, null, method); // Custom name } } } }
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); } } } }
/// <summary> /// Registers all public instance methods in an object tagged with <see cref="LuaGlobalAttribute"/> as Lua global functions /// </summary> /// <param name="lua">The Lua VM to add the methods to</param> /// <param name="o">The object to get the methods from</param> public static void TaggedInstanceMethods(LuaState lua, object o) { #region Sanity checks if (lua == null) { throw new ArgumentNullException("lua"); } if (o == null) { throw new ArgumentNullException("o"); } #endregion foreach (MethodInfo method in o.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public)) { foreach (LuaGlobalAttribute attribute in method.GetCustomAttributes(typeof(LuaGlobalAttribute), true)) { if (string.IsNullOrEmpty(attribute.Name)) { lua.RegisterFunction(method.Name, o, method); // CLR name } else { lua.RegisterFunction(attribute.Name, o, method); // Custom name } } } }
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); } } } }