private void RegisterCommands(Type type, object instance) { foreach (MethodInfo m in type.GetMethods()) { if (m.GetCustomAttributes(typeof(ConsoleCommandAttribute), true).Length <= 0) { continue; } ConsoleCommandAttribute attribute = (ConsoleCommandAttribute)m.GetCustomAttributes(typeof(ConsoleCommandAttribute), true)[0]; CommandHelpAttribute helpAttribute = null; if (m.GetCustomAttributes(typeof(CommandHelpAttribute), true).Length > 0) { helpAttribute = (CommandHelpAttribute)m.GetCustomAttributes(typeof(CommandHelpAttribute), true)[0]; } string helpMessage = string.Empty; if (helpAttribute != null) { helpMessage = helpAttribute.Help; } CommandUsageAttribute usageAttribute = null; if (m.GetCustomAttributes(typeof(CommandUsageAttribute), true).Length > 0) { usageAttribute = (CommandUsageAttribute)m.GetCustomAttributes(typeof(CommandUsageAttribute), true)[0]; } string usageMessage = string.Empty; if (usageAttribute != null) { usageMessage = usageAttribute.Usage; } var cmdName = m.Name.ToLower(); if (!string.IsNullOrEmpty(attribute.Name)) { cmdName = attribute.Name; } var contextInstance = instance; if (m.IsStatic) { contextInstance = null; } ConsoleCommand command = new ConsoleCommand(cmdName, contextInstance, m); command.Help = helpMessage; command.Usage = usageMessage; command.CommandRuntime = attribute.Runtime; RegisterCommand(command); } }
public void UnregisterCommands(Type type) { foreach (MethodInfo m in type.GetMethods()) { if (m.GetCustomAttributes(typeof(ConsoleCommandAttribute), true).Length <= 0) { continue; } ConsoleCommandAttribute attribute = (ConsoleCommandAttribute)m.GetCustomAttributes(typeof(ConsoleCommandAttribute), true)[0]; var cmdName = m.Name.ToLower(); if (!string.IsNullOrEmpty(attribute.Name)) { cmdName = attribute.Name; } UnregisterCommand(cmdName); } }