public void Bind(MethodBase method, object instance = null) { if (method == null) { throw new ArgumentNullException(nameof(method)); } if (!method.IsStatic && instance == null) { throw new ArgumentNullException(nameof(instance)); } CommandAttribute[] attributes = GetCommandInfo(method); if (attributes == null || attributes.Length == 0) { return; } for (int i = 0; i < attributes.Length; i++) { CommandAttribute attribute = attributes[i]; if (string.IsNullOrWhiteSpace(attribute.Name)) { attribute.Name = method.Name; } if (string.IsNullOrWhiteSpace(attribute.Group)) { attribute.Group = "global"; } Command cmd = new Command(this, method, instance, attribute); if (!commands.Contains(cmd)) { commands.Add(cmd); } } }
private CommandAttribute[] GetCommandInfo(MethodBase method) { CommandAttribute[] commands = method.GetCustomAttributes <CommandAttribute>(true).ToArray(); for (int i = 0; i < commands.Length; i++) { CommandAttribute c = commands[i]; if (string.IsNullOrWhiteSpace(c.Name)) { c.Name = method.Name; } if (string.IsNullOrWhiteSpace(c.Group)) { c.Group = "global"; } } return(commands); }
public Command(ICommandHandler handler, MethodBase method, object instance, CommandAttribute attribute) { Handler = handler; Name = attribute.Name.ToLowerInvariant(); Group = attribute.Group.ToLowerInvariant(); HelpText = attribute.HelpText; FullName = $"{Group}.{Name}"; Method = method; Instance = instance; }