可以通过实现 ISubListCommand 和 IContainSubCommands 实现给命令添加子命令。
using GrapeCity.Forguncy.Commands; using System.Collections.Generic; using System.ComponentModel; using System.Threading.Tasks; namespace MyPlugin { public class MyPluginServerCommand : Command, ICommandExecutableInServerSideAsync, ISubListCommand, IContainSubCommands { [Browsable(false)] public List<Command> CommandList { get; set; } = new List<Command>(); public IEnumerable<List<Command>> EnumSubCommands() { yield return CommandList; } public async Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext) { dataContext.Log.AppendLine("子命令开始执行"); var result = await dataContext.ExecuteCommandsAsync(this.CommandList); dataContext.Log.AppendLine("子命令执行结束"); return result; } public override CommandScope GetCommandScope() { return CommandScope.ExecutableInServer; } } }
代码说明:
- 给 CommandList属性标注 [Browsable(false)] 避免CommandList属性出现在主命令的属性中。
- 通过 dataContext.ExecuteCommandsAsync 方法执行子命令。
效果: