public void Initialize(IServiceProvider serviceProvider, Type commandType, CommandOptions commandOptions) { var command = ActivatorUtilities.CreateInstance(serviceProvider, commandType) as ICommand; var cmdMeta = GetCommandMetadata(commandType); try { Key = (TKey)cmdMeta.Key; } catch (Exception e) { throw new Exception($"The command {cmdMeta.Name}'s Key {cmdMeta.Key} cannot be converted to the desired type '{typeof(TKey).Name}'.", e); } Command = command as ICommand <TAppSession, TPackageInfo>; AsyncCommand = command as IAsyncCommand <TAppSession, TPackageInfo>; var filters = new List <ICommandFilter>(); if (commandOptions.GlobalCommandFilterTypes.Any()) { filters.AddRange(commandOptions.GlobalCommandFilterTypes.Select(t => ActivatorUtilities.CreateInstance(serviceProvider, t) as CommandFilterBaseAttribute)); } filters.AddRange(commandType.GetCustomAttributes(false).OfType <CommandFilterBaseAttribute>()); Filters = filters; }
public void Initialize(IServiceProvider serviceProvider, CommandTypeInfo commandTypeInfo, CommandOptions commandOptions) { var command = commandTypeInfo.Command; if (command == null) { if (commandTypeInfo.CommandType != commandTypeInfo.ActualCommandType) { var commandFactory = ActivatorUtilities.CreateFactory(commandTypeInfo.CommandType, new [] { typeof(IServiceProvider) }); command = commandFactory.Invoke(serviceProvider, new object[] { serviceProvider }) as ICommand; } else { command = ActivatorUtilities.CreateInstance(serviceProvider, commandTypeInfo.CommandType) as ICommand; } } SetCommand(command); var cmdMeta = GetCommandMetadata(commandTypeInfo.ActualCommandType); try { Key = (TKey)cmdMeta.Key; Metadata = cmdMeta; } catch (Exception e) { throw new Exception($"The command {cmdMeta.Name}'s Key {cmdMeta.Key} cannot be converted to the desired type '{typeof(TKey).Name}'.", e); } var filters = new List <ICommandFilter>(); if (commandOptions.GlobalCommandFilterTypes.Any()) { filters.AddRange(commandOptions.GlobalCommandFilterTypes.Select(t => ActivatorUtilities.CreateInstance(serviceProvider, t) as CommandFilterBaseAttribute)); } filters.AddRange(commandTypeInfo.ActualCommandType.GetCustomAttributes(false).OfType <CommandFilterBaseAttribute>()); Filters = filters; }
public ICommandSet Create(IServiceProvider serviceProvider, Type commandType, CommandOptions commandOptions) { var commandSet = new CommandSet <TAppSession>(); commandSet.Initialize(serviceProvider, commandType, commandOptions); return(commandSet); }
/// <summary> /// 添加全局命令筛选器 /// </summary> /// <typeparam name="TCommandFilter">命令筛选器</typeparam> /// <param name="commandOptions">命令选项</param> public static void AddGlobalCommandFilter <TCommandFilter>(this CommandOptions commandOptions) where TCommandFilter : CommandFilterBaseAttribute { commandOptions.AddGlobalCommandFilterType(typeof(TCommandFilter)); }
/// <summary> /// 添加命令装配 /// </summary> /// <param name="commandOptions">命令选项</param> /// <param name="commandAssembly">命令装配</param> public static void AddCommandAssembly(this CommandOptions commandOptions, Assembly commandAssembly) { commandOptions.CommandSources.Add(new ActualCommandAssembly { Assembly = commandAssembly }); }
/// <summary> /// 添加命令 /// </summary> /// <param name="commandOptions">命令选项</param> /// <param name="commandType">命令类型</param> public static void AddCommand(this CommandOptions commandOptions, Type commandType) { commandOptions.CommandSources.Add(new ActualCommand { CommandType = commandType }); }
/// <summary> /// 添加命令 /// </summary> /// <typeparam name="TCommand">命令</typeparam> /// <param name="commandOptions">命令选项</param> public static void AddCommand <TCommand>(this CommandOptions commandOptions) { commandOptions.CommandSources.Add(new ActualCommand { CommandType = typeof(TCommand) }); }