public void RegisterFunction(object target) { var targetType = target.GetType(); foreach (var info in targetType.GetMethods( )) { if (Attribute.GetCustomAttributes(info).Any(a => a.GetType() == typeof(RegisterScriptFunctionAttribute))) { try { foreach (var attribute in Attribute.GetCustomAttributes(info)) { if (attribute.GetType() == typeof(RegisterScriptFunctionAttribute)) { var attr = ( RegisterScriptFunctionAttribute )attribute; var parameters = new ArrayList(); var parameterDocs = new ArrayList(); // Now get the expected parameters from the MethodInfo object var parameterInfo = info.GetParameters(); // If they don't match, someone forgot to add some documentation to the // attribute, complain and go to the next method if (attr.FunctionParameters != null && (parameterInfo.Length != attr.FunctionParameters.Length)) { //throw an error about missing information Console.WriteLine("Function " + info.Name + " (exported as " + attr.FunctionName + ") argument number mismatch. Declared " + attr.FunctionParameters.Length + " but requires " + parameterInfo.Length + "."); break; } //Build a parameter <-> parameter doc hashtable for (var i = 0; i < parameterInfo.Length; i++) { parameters.Add(parameterInfo[i].Name); parameterDocs.Add(attr.FunctionParameters[i]); } // Get a new function descriptor from this information var descriptor = new LuaFunctionDescriptor(attr.FunctionName, attr.FunctionDescription, parameters, parameterDocs); // Add it to the global hashtable _functions.Add(attr.FunctionName, descriptor); // And tell the VM to register it. Messenger.Default.Send(new RegisterScriptMethodMessage(attr.FunctionName, target, info)); } } } catch (Exception ex) { Console.WriteLine("[LuaFunctionRegistrar.RegisterFunction({0})] Exception caught: {1}", target, ex.InnerException); } } } }
public void RegisterFunction( object target ) { var targetType = target.GetType(); foreach ( var info in targetType.GetMethods( ) ) { if ( Attribute.GetCustomAttributes( info ).Any( a => a.GetType() == typeof( RegisterScriptFunctionAttribute ) ) ) { try { foreach ( var attribute in Attribute.GetCustomAttributes( info ) ) { if ( attribute.GetType() == typeof ( RegisterScriptFunctionAttribute ) ) { var attr = ( RegisterScriptFunctionAttribute ) attribute; var parameters = new ArrayList(); var parameterDocs = new ArrayList(); // Now get the expected parameters from the MethodInfo object var parameterInfo = info.GetParameters(); // If they don't match, someone forgot to add some documentation to the // attribute, complain and go to the next method if ( attr.FunctionParameters != null && ( parameterInfo.Length != attr.FunctionParameters.Length ) ) { //throw an error about missing information Console.WriteLine( "Function " + info.Name + " (exported as " + attr.FunctionName + ") argument number mismatch. Declared " + attr.FunctionParameters.Length + " but requires " + parameterInfo.Length + "." ); break; } //Build a parameter <-> parameter doc hashtable for ( var i = 0; i < parameterInfo.Length; i++ ) { parameters.Add( parameterInfo[ i ].Name ); parameterDocs.Add( attr.FunctionParameters[ i ] ); } // Get a new function descriptor from this information var descriptor = new LuaFunctionDescriptor( attr.FunctionName, attr.FunctionDescription, parameters, parameterDocs ); // Add it to the global hashtable _functions.Add( attr.FunctionName, descriptor ); // And tell the VM to register it. Messenger.Default.Send( new RegisterScriptMethodMessage( attr.FunctionName, target, info ) ); } } } catch ( Exception ex ) { Console.WriteLine( "[LuaFunctionRegistrar.RegisterFunction({0})] Exception caught: {1}", target, ex.InnerException ); } } } }