public void AddSubcommand(ICommand command, string?group = null) { ThrowUtil.ThrowIfArgumentNull(command); if (Subcommands.FindIndex(item => item.Name == command.Name) >= 0) { throw new ArgumentException($"Command with same name '{command.Name}' already exists."); } group ??= BUILTIN_GROUP; if (mGroups.FindIndex(item => item == group) < 0) { // 越早注册的group排序越靠前 mGroups.Add(group); } var commands = mGroup2Subcommands.AddOrCreateValue(group, () => new PriorityList <Tuple <ICommand, int> >((tuple1, tuple2) => tuple2.Item2 - tuple1.Item2)); commands.Add(new Tuple <ICommand, int>(command, 0)); // 有Subcommand才有Help if (!mAddHelp) { mMethods.Add(new Tuple <MethodSpecs, Func <object?>?, int>(new MethodSpecs(typeof(Command).GetMethodThrow(nameof(_HelpMethod))), () => this, -1)); // 保证help在最后 mAddHelp = true; var helpCommand = new Command("help", "帮助"); helpCommand.AddMethod(typeof(Command), nameof(_HelpCommand), this); AddSubcommand(helpCommand); } }
public static object?InvokeMethod( CommandLineSerializer serializer, Type type, string methodName, object?obj, LinkedList <string> argList) { var method = type.GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance); ThrowUtil.ThrowIfArgumentNull(method); return(InvokeMethod(serializer, method, obj, argList)); }
public static bool TryFind <T>(this IReadOnlyList <T> list, Predicate <T> match, out T result) #endif { ThrowUtil.ThrowIfArgumentNull(list); ThrowUtil.ThrowIfArgumentNull(match); result = default; foreach (var item in list) { if (match(item)) { result = item; return(true); } } return(false); }
public static void SwapItem <T>(this IList <T> list, int targetIndex, Func <T, bool> predicate) { ThrowUtil.ThrowIfArgumentNull(list); ThrowUtil.ThrowIfArgumentNull(predicate); if (targetIndex >= list.Count) { throw new ArgumentOutOfRangeException(nameof(targetIndex)); } var index = list.FindIndex(predicate); if (index < 0 || index == targetIndex) { return; } T v = list[targetIndex]; list[targetIndex] = list[index]; list[index] = v; }
public void AddMethod(MethodBase method, Func <object?>?objGetter = null) { ThrowUtil.ThrowIfArgumentNull(method); mMethods.Add(new Tuple <MethodSpecs, Func <object?>?, int>(new MethodSpecs(method), objGetter, 0)); }