Class CommandService
表示一个基于文本的命令服务。
public class CommandService : IDisposable
- Inheritance
-
CommandService
- Implements
- Inherited Members
- Extension Methods
Remarks
此类用于支持在运行时动态创建命令,或者在编译时静态创建命令。要在编译时创建命令模块,参见
ModuleBase 或 ModuleBuilder。
此服务还提供了几个事件,用于监视命令的使用情况;例如 Log 用于任何与命令相关的日志事件,
Constructors
CommandService()
初始化一个 CommandService 类的新实例。
public CommandService()
CommandService(CommandServiceConfig)
初始化一个 CommandService 类的新实例。
public CommandService(CommandServiceConfig config)
Parameters
configCommandServiceConfig命令服务的配置。
Exceptions
- InvalidOperationException
默认运行模式不能设置为 Default。
Properties
Commands
获取所有加载的命令。
public IEnumerable<CommandInfo> Commands { get; }
Property Value
Modules
获取所有加载的模块。
public IEnumerable<ModuleInfo> Modules { get; }
Property Value
TypeReaders
获取所有加载的类型读取器。
public ILookup<Type, TypeReader> TypeReaders { get; }
Property Value
Methods
AddModuleAsync(Type, IServiceProvider?)
添加一个命令模块。
public Task<ModuleInfo> AddModuleAsync(Type type, IServiceProvider? services)
Parameters
typeType要添加的模块的类型。
servicesIServiceProvider用于依赖注入的服务提供程序;如果不使用依赖注入,则传递
null。
Returns
- Task<ModuleInfo>
一个表示异步添加操作的任务。任务结果包含添加的模块。
Exceptions
- ArgumentException
此模块已经添加。
- InvalidOperationException
无法构建 ModuleInfo;可能提供了无效的类型。
AddModuleAsync<T>(IServiceProvider?)
添加一个命令模块。
public Task<ModuleInfo> AddModuleAsync<T>(IServiceProvider? services)
Parameters
servicesIServiceProvider用于依赖注入的服务提供程序;如果不使用依赖注入,则传递
null。
Returns
- Task<ModuleInfo>
一个表示异步添加操作的任务。任务结果包含添加的模块。
Type Parameters
T要添加的模块的类型。
Examples
以下示例代码将模块 MyModule 注册到 commandService 中。
await commandService.AddModuleAsync<MyModule>(serviceProvider);
Exceptions
- ArgumentException
此模块已经添加。
- InvalidOperationException
无法构建 ModuleInfo;可能提供了无效的类型。
AddModulesAsync(Assembly, IServiceProvider?)
添加程序集内的所有命令模块。
public Task<IEnumerable<ModuleInfo>> AddModulesAsync(Assembly assembly, IServiceProvider? services)
Parameters
assemblyAssembly要添加其所有模块的程序集。
servicesIServiceProvider用于依赖注入的服务提供程序;如果不使用依赖注入,则传递
null。
Returns
- Task<IEnumerable<ModuleInfo>>
一个表示异步添加操作的任务。任务结果包含所有添加的模块。
AddTypeReader(Type, TypeReader)
添加一个自定义的类型读取器。
public void AddTypeReader(Type type, TypeReader reader)
Parameters
typeType要读取的对象类型。
readerTypeReader要添加的类型读取器的实例。
Remarks
如果 type 是一个值类型,那么一个读取对应可空值类型的 TypeReader 也会被添加。
如果 type 的默认 TypeReader 已经存在,那么会记录一个警告,默认的 TypeReader 将会被替换。
AddTypeReader(Type, TypeReader, bool)
添加一个自定义的类型读取器。
public void AddTypeReader(Type type, TypeReader reader, bool replaceDefault)
Parameters
typeType要读取的对象类型。
readerTypeReader要添加的类型读取器的实例。
replaceDefaultbool是否替换默认的 TypeReader。
Remarks
如果 type 是一个值类型,那么一个读取对应可空值类型的 TypeReader 也会被添加。
AddTypeReader<T>(TypeReader)
添加一个自定义的类型读取器。
public void AddTypeReader<T>(TypeReader reader)
Parameters
readerTypeReader要添加的类型读取器的实例。
Type Parameters
T要读取的对象类型。
Remarks
如果 T 是一个值类型,那么一个读取对应可空值类型的 TypeReader 也会被添加。
如果 T 的默认 TypeReader 已经存在,那么会记录一个警告,默认的 TypeReader 将会被替换。
AddTypeReader<T>(TypeReader, bool)
添加一个自定义的类型读取器。
public void AddTypeReader<T>(TypeReader reader, bool replaceDefault)
Parameters
readerTypeReader要添加的类型读取器的实例。
replaceDefaultbool是否替换默认的 TypeReader。
Type Parameters
T要读取的对象类型。
Remarks
如果 T 是一个值类型,那么一个读取对应可空值类型的 TypeReader 也会被添加。
CreateModuleAsync(string, Action<ModuleBuilder>)
创建一个命令模块。
public Task<ModuleInfo> CreateModuleAsync(string primaryAlias, Action<ModuleBuilder> buildFunc)
Parameters
primaryAliasstring模块的首要别名。
buildFuncAction<ModuleBuilder>一个构建模块的委托。
Returns
- Task<ModuleInfo>
一个表示异步操作的任务。任务结果包含构建的模块。
Dispose(bool)
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
protected virtual void Dispose(bool disposing)
Parameters
disposingbool
ExecuteAsync(ICommandContext, int, IServiceProvider, MultiMatchHandling)
执行命令。
public Task<IResult> ExecuteAsync(ICommandContext context, int argPos, IServiceProvider services, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
Parameters
contextICommandContext命令的上下文。
argPosint命令的位置。
servicesIServiceProvider要用于命令执行的依赖注入服务。
multiMatchHandlingMultiMatchHandling当匹配到多个命令时的处理模式。
Returns
ExecuteAsync(ICommandContext, string, IServiceProvider, MultiMatchHandling)
执行命令。
public Task<IResult> ExecuteAsync(ICommandContext context, string input, IServiceProvider services, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
Parameters
contextICommandContext命令的上下文。
inputstring命令字符串。
servicesIServiceProvider要用于命令执行的依赖注入服务。
multiMatchHandlingMultiMatchHandling当匹配到多个命令时的处理模式。
Returns
RemoveModuleAsync(ModuleInfo)
移除命令模块。
public Task<bool> RemoveModuleAsync(ModuleInfo module)
Parameters
moduleModuleInfo要移除的模块。
Returns
RemoveModuleAsync(Type)
移除命令模块。
public Task<bool> RemoveModuleAsync(Type type)
Parameters
typeType要移除的模块的类型。
Returns
RemoveModuleAsync<T>()
移除命令模块。
public Task<bool> RemoveModuleAsync<T>()
Returns
Type Parameters
T要移除的模块的类型。
Search(ICommandContext, int)
搜索命令。
public SearchResult Search(ICommandContext context, int argPos)
Parameters
contextICommandContext命令的上下文。
argPosint命令的位置。
Returns
- SearchResult
命令搜索的结果。
Search(ICommandContext, string)
搜索命令。
public SearchResult Search(ICommandContext context, string input)
Parameters
contextICommandContext命令的上下文。
inputstring命令字符串。
Returns
- SearchResult
命令搜索的结果。
Search(string)
搜索命令。
public SearchResult Search(string input)
Parameters
inputstring命令字符串。
Returns
- SearchResult
命令搜索的结果。
TryRemoveTypeReader(Type, bool, out IDictionary<Type, TypeReader>)
移除一个类型读取器。
public bool TryRemoveTypeReader(Type type, bool isDefaultTypeReader, out IDictionary<Type, TypeReader> readers)
Parameters
typeType要移除的类型读取器所读取的对象类型。
isDefaultTypeReaderbool是否要移除默认的 TypeReader。
readersIDictionary<Type, TypeReader>移除的类型读取器。
Returns
- bool
如果成功移除,则为
true;否则为false。
Remarks
从命令服务中移除一个 TypeReader 不会从已加载的模块与命令的实例中取消引用 TypeReader。要使更改生效,您需要重新加载模块。
ValidateAndGetBestMatch(SearchResult, ICommandContext, IServiceProvider, MultiMatchHandling)
从指定的搜索结果中验证先决条件并获取最佳匹配。
public Task<IResult> ValidateAndGetBestMatch(SearchResult matches, ICommandContext context, IServiceProvider provider, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
Parameters
matchesSearchResult要验证的搜索结果。
contextICommandContext命令的上下文。
providerIServiceProvider要用于命令验证与解析的依赖注入服务。
multiMatchHandlingMultiMatchHandling当匹配到多个命令时的处理模式。
Returns
Events
CommandExecuted
当命令执行时引发。
public event Func<CommandInfo?, ICommandContext, IResult, Task> CommandExecuted
Event Type
Remarks
此事件在命令执行后引发,既包含了执行成功的情况,也包含了执行失败的情况。
事件参数:
- CommandInfo? 参数是执行的命令,如果命令在解析或先决条件阶段失败,则可能为
null。 - ICommandContext 参数是命令的上下文。
- IResult 参数是命令的结果。
Log
当产生与命令相关的日志信息时引发。
public event Func<LogMessage, Task> Log
Event Type
Remarks
事件参数:
- LogMessage 参数是日志消息。