VR Development Framework
v 1.0.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
static void AVR.Core.AVR_DevConsole.register_all_attribute_commands ( )
inlinestaticprivate

Definition at line 292 of file AVR_DevConsole.cs.

292  {
293  // Find any and all methods marked as "ConsoleCommand" in all assemplies:
294  var methodsMarkedAsCommand =
295  from a in System.AppDomain.CurrentDomain.GetAssemblies().AsParallel()
296  from t in a.GetTypes()
297  from m in t.GetMethods(System.Reflection.BindingFlags.Static | (System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic))
298  let attributes = m.GetCustomAttributes(typeof(AVR.Core.Attributes.ConsoleCommand), true)
299  where attributes != null && attributes.Length > 0
300  select new { method = m, attributes = attributes.Cast<AVR.Core.Attributes.ConsoleCommand>() };
301 
302  // Register each as a command:
303  foreach(var result in methodsMarkedAsCommand) {
304  foreach(var att in result.attributes) {
305  try {
306  // Single string function static void func(string arg)
307  if (result.method.GetParameters().Count() == 1 && result.method.GetParameters()[0].ParameterType == typeof(System.String))
308  {
309  System.Action<string> action = (System.Action<string>)System.Delegate.CreateDelegate(typeof(System.Action<string>), result.method);
310  register_command(att.getCommandString(), (args) => action(args[0]), att.getMinArgs(), att.getDescription());
311  }
312  // String-array function static void func(string[] args)
313  else if(result.method.GetParameters().Count()>0) {
314  System.Action<string[]> action = (System.Action<string[]>)System.Delegate.CreateDelegate(typeof(System.Action<string[]>), result.method);
315  register_command(att.getCommandString(), action, att.getMinArgs(), att.getDescription());
316  }
317  // No-parameter function static void func()
318  else {
319  System.Action action = (System.Action)System.Delegate.CreateDelegate(typeof(System.Action), result.method);
320  register_command(att.getCommandString(), (args) => action(), att.getMinArgs(), att.getDescription());
321  }
322  }
323  catch (System.Exception) {
324  error("Following method is marked as ConsoleCommand, but doesnt follow the necessary requirements: " + result.method + "\n It needs to be static and void. Parameters have to be either none, a single string or a single array of strings.");
325  }
326  }
327  }
328  }
static void error(string s)
Print a given error string on the console.
static void register_command(AVR_ConsoleCommand cmd)
Registers a new command to the console.
This attribute can be used to easily make a function into a command runnable through the AVR_DevConso...