Preface according to the command mode in the previous article and some actual situations encountered at work, this article is always a learning process, in this process, I will find many good models or a development method. What I wrote today is often used in my work. I feel that this method is very elegant and beautiful, I tried to implement it myself. This may not be the case in the original framework. Please give us some advice if you have many shortcomings. I still don't know whether this method is a mode or a technology used in framework development. I will call it a Command Controller for the moment. The main function of the Command Controller is to obtain the commands provided by the user and then execute the commands. Here I design the "command" to be executed into a function, which corresponds to a String-type command String, and the Command Controller allows expansion. The implementation first defines an attribute class, which is used to have only one CommandName attribute on the extended command class or command function. if it acts on the command function, it indicates the command name, if it is applied to the class, it indicates the name of the command class, but it can be considered as a classification. This part is described later and can be customized. Copy code 1 // <summary> 2 // code attribute class used by the command 3 // </summary> 4 [AttributeUsage (AttributeTargets. class | AttributeTargets. method, AllowMultiple = false, Inherited = true)] 5 public class commandattriple: Attribute 6 {7 private string commandName = null; 8 9 public string CommandName10 {11 get {return commandName ;} 12 set {commandName = value;} 13} 14 15 public CommandAttribute (string CommandName) 16 {17 commandName = CommandName; 18} 19} now that the copied code has this attribute class, we need to use it to define some command sample classes to be used later, copy code 1 // <summary> 2 // example: Command class and command function, it can also be used as an extension 3 // </summary> 4 [Command ("Test")] 5 public class CommandTest: CommandMethod 6 {7 [Command ("MyCommandone")] 8 public object test (ICommandParameter commandparameter) 9 {10 return null; 11} 12 13 [Command ("MyCommandone1")] 14 public object test1 (ICommandParameter commandpara Meter) 15 {16 return null; 17} 18 19 [Command ("MyCommandone2")] 20 public object test2 (ICommandParameter commandparameter) 21 {22 return null; 23} 24 25 26 [Command ("MyCommandone3")] 27 public object test3 (ICommandParameter commandparameter) 28 {29 return null; 30} 31 32 [Command ("MyCommandone4")] 33 public object test4 (ICommandParameter commandparameter) 34 {35 return null; 36} 37} copy the code. The CommandTe can be seen in the sample code above. The st class inherits from the CommandMethod class, and some functions in the class have the same signature. The function parameters are of the ICommandParameter interface type, which is the specification to be followed when extending command methods, defined specifications: copy code 1 // <summary> 2 // parameter specification of the extended command function 3 // </summary> 4 public interface ICommandParameter 5 {6 7} 8 9 // /<summary> 10 // extended command method class base class 11 /// </summary> 12 public class CommandMethod13 {14 15} copy the code to implement what is customizable, for example, you can define the ContainerObject attribute in the ICommandParameter interface, that is, the container attribute. You can set the container attribute value when passing in the instance parameter when calling the command later, You can set any value you want to set to it, and then use it in the command function. You can also implement a parameter context object specifically used to process extended commands according to the abstract definition, you need to customize it. Then, the code of a command controller is written. It is mainly responsible for processing the types registered by the client, for example, a CommandMethodActionDelegate type delegate is used to obtain function commands and function information, convert command functions into delegates, and maintain command lists: copy code 1 public delegate object CommandMethodActionDelegate (ICommandParameter commandParameter); 2 3 public class CommandController 4 {5 private static CommandController _ Instance = null; 6 7 public static CommandController Instance 8 {9 get 10 {11 if (_ Instance = Null) 12 {13 _ Instance = new CommandController (HostDevelopment. instance); 14} 15 return _ Instance; 16} 17} 18 19 private HostDevelopment _ CommandDevelopment = HostDevelopment. instance; 20 21 public CommandController (HostDevelopment commandDevelopment) 22 {23 _ CommandDevelopment = commandDevelopment; 24} 25 26 private Dictionary <string, CommandMethodActionDelegate> commandlist = new Dictiona Ry <string, CommandMethodActionDelegate> (); 27 28 29 private List <string> _ commandNames = null; 30 /// <summary> 31 // command name set 32 /// </summary> 33 public List <string> CommandNames 34 {35 get 36 {37 if (_ commandNames = null) 38 {39 GetCommandNames (); 40} 41 return _ commandNames; 42} 43} 44 45 private void GetCommandNames () 46 {47 48 if (commandlist. count> 0) 49 {50 if (_ commandNames = nu Ll) 51 {52 _ commandNames = new List <string> (); 53} 54 foreach (string name in commandlist. keys) 55 {56 _ commandNames. add (name); 57} 58} 59} 60 61 public bool RegisterCommand (object instance) 62 {63 Type t = instance. getType (); 64 CommandAttribute cmdatt = (CommandAttribute) Attribute. getCustomAttribute (t, typeof (CommandAttribute), false); 65 if (cmdatt! = Null) 66 {67 AddCommandToModel (instance); 68 return true; 69} 70 else {return false;} 71} 72 73 private void AddCommandToModel (object instance) 74 {75 Type t = instance. getType (); 76 MethodInfo [] methods = t. getMethods (); 77 foreach (MethodInfo methodinfo in methods) 78 {79 CommandAttribute cmdatt = (CommandAttribute) Attribute. getCustomAttribute (methodinfo, typeof (CommandAttribute), false); 80 if (cmdatt! = Null) 81 {82 83 CommandMethodActionDelegate commanddelegate = (CommandMethodActionDelegate) Delegate. createDelegate (typeof (CommandMethodActionDelegate), instance, methodinfo. name); 84 commandlist. add (cmdatt. commandName, commanddelegate); 85} 86 87} 88} 89 90 91 internal object Execute (string commandName, ICommandParameter commandParameter) 92 {93 if (commandName = null) 94 {95 throw new Arg UmentNullException ("commandName"); 96} 97 if (! Commandlist. containsKey (commandName) 98 {99 return new ArgumentNullException ("the command is not included, the command is invalid"); 100} 101 CommandMethodActionDelegate into action = commandlist [commandName]; 102 return into action. invoke (commandParameter); 103} 104} copy the code in the CommandController type. The RegisterCommand () method registers the extended command to the Command Controller, in this example, the registration method is manual input type instance registration. You can also modify the implementation method to obtain the assembly of all the dependencies of the current system when the Command Controller is started, obtain all the extended command types that comply with the type specifications and register them in the controller. The HostDevelopment type mentioned in the above Code is a host container object that I have defined to carry command controllers. In this series of articles, hostDevelopment is used to carry the controller. Now let's take a look at the temporary definition of HostDevelopment: Copy code 1 public class HostDevelopment 2 {3 private static HostDevelopment _ commandDevelopment = null; 4 5 public static HostDevelopment Instance 6 {7 get 8 {9 if (_ commandDevelopment = null) 10 {11 _ commandDevelopment = new HostDevelopment (); 12} 13 return _ commandDevelopment; 14} 15} 16 17 private static ServiceContainer _ serviceContainer = new ServiceContainer (); 18 19 p Rivate void SetServiceContainer (Type t, object instance) 20 {21 if (instance = null) 22 {23 throw new ArgumentNullException ("instance"); 24} 25 _ serviceContainer. addService (t, instance); 26} 27 28 private object GetServiceContainer (Type t) 29 {30 return _ serviceContainer. getService (t); 31} 32 33 public CommandController CommandController34 {35 get36 {37 return (CommandController) GetServiceContainer (typ Eof (CommandController); 38} 39} 40 41 public void Start () 42 {43 SetServiceContainer (typeof (CommandController), new CommandController (this )); 44} 45 46 public object Execute (string commandName, ICommandParameter commandParameter) 47 {48 return CommandController. execute (commandName, commandParameter); 49} 50} after reading the code, you should have understood it roughly. However, such Code cannot be tested because it lacks some entities. Define a default command parameter specification: Copy code 1 public class CommandParameterCase: ICommandParameter 2 {3 private string _ StrText; 4 5 public string StrText 6 {7 get {return _ StrText ;} 8 set {_ StrText = value;} 9} 10} copy the code and modify the first function named test in the CommandTest type (the corresponding command name is MyCommandone ), the function name is a bit casual, so don't worry about it. Copy code 1 [Command ("MyCommandone")] 2 public object test (ICommandParameter commandparameter) 3 {4 if (commandparameter! = Null) 5 {6 CommandParameterCase commandparametercase = commandparameter as CommandParameterCase; 7 return commandparametercase. StrText; 8} 9 else {return null;} 10} copy the code and define all the required items. Let's take a look at the call code: 1 HostDevelopment. instance. start (); 2 HostDevelopment. instance. commandController. registerCommand (new CommandTest (); 3 var strtext = HostDevelopment. instance. execute ("MyCommandone", new CommandParameterCase () {StrText = "test"}); By the way, you can view the value of a breakpoint or the strtext output. There is no more explanation here.