Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of extended commands in C #

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you the example analysis of extended commands in C#, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's learn about it!

Demand

I don't know whether this approach is a pattern or a technology used in framework development. I'll call it a command controller for the time being.

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 as a function that corresponds to a command string of type String, and the command controller is allowed to be extended.

Realize

First of all, I defined an attribute class, which is used in the extended command class, or command function, there is only one CommandName attribute, if it acts on the command function, then it represents the command name, if it is acting on the class, then it represents the name of the command category, just consider it as a classification, this part will be discussed later, you can customize the implementation.

1 / 2 / the command code attribute class 3 / 4 [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)] 5 public class CommandAttribute: Attribute6 {7 private string commandName = null;89 public string CommandName10 {11 get {return commandName;} 12 set {commandName = value } 13} 1415 public CommandAttribute (string CommandName) 16 {17 commandName = CommandName;18} 19}

Now that we have this property class, we will use it to define some command sample classes that we will use later.

1 / 2 / / example: command classes and command functions can also be considered as extensions 3 / 4 [Command ("Test")] 5 public class CommandTest: CommandMethod6 {7 [Command ("MyCommandone")] 8 public object test (ICommandParameter commandparameter) 9 {10 return null 11} 1213 [Command ("MyCommandone1")] 14 public object test1 (ICommandParameter commandparameter) 15 {16 return null;17} 1819 [Command ("MyCommandone2")] 20 public object test2 (ICommandParameter commandparameter) 21 {22 return null 23} 242526 [Command ("MyCommandone3")] 27 public object test3 (ICommandParameter commandparameter) 28 {29 return null;30} 3132 [Command ("MyCommandone4")] 33 public object test4 (ICommandParameter commandparameter) 34 {35 return null;36} 37}

In the above sample code, you can see that the CommandTest class inherits from the CommandMethod class, and the signatures of some functions in the class are the same, and the function parameters are of the type of ICommandParameter interface. These are some of the specifications to be followed and defined when extending command methods:

1 / / 2 / / Parameter specification for extended command functions 3 / 4 public interface ICommandParameter5 {67} 89 / / 10 / / extended command method class base class 11 / 12 public class CommandMethod13 {1415}

You can customize whatever you need to implement. For example, you can define a property with the name of object type ContainerObject in the ICommandParameter interface, which means container property. When you call the command later, you can set the value of the container property when you pass in the instance parameter, which can be set to any value you want, and then use it in the command function. You can also implement a parameter context object specifically for processing extension commands according to the abstract definition, which needs to be customized.

Then we write the code of a command controller, which is mainly responsible for handling the types registered by the client, such as judging the type, obtaining function commands and function information by reflection types, converting command functions into delegates, maintaining command lists, and so on, as well as using a delegate of CommandMethodActionDelegate type:

1 public delegate object CommandMethodActionDelegate (ICommandParameter commandParameter); 23 public class CommandController4 {5 private static CommandController _ Instance = null;67 public static CommandController Instance8 {9 get10 {11 if (_ Instance = = null) 12 {13 _ Instance = new CommandController (HostDevelopment.Instance) 14} 15 return _ Instance;16} 17} 1819 private HostDevelopment _ CommandDevelopment = HostDevelopment.Instance;2021 public CommandController (HostDevelopment commandDevelopment) 22 {23 _ CommandDevelopment = commandDevelopment;24} 2526 private Dictionary commandlist = new Dictionary (); 272829 private List _ commandNames = null 30 / / 31 / / Command name collection 32 / 33 public List CommandNames34 {35 get36 {37 if (_ commandNames = = null) 38 {39 GetCommandNames (); 40} 41 return _ commandNames 42} 43} 4445 private void GetCommandNames () 46 {4748 if (commandlist.Count > 0) 49 {50 if (_ commandNames = = null) 51 {52 _ commandNames = new List () 53} 54 foreach (string name in commandlist.Keys) 55 {56 _ commandNames.Add (name); 57} 58} 59} 6061 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} 7273 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 {8283 CommandMethodActionDelegate commanddelegate = (CommandMethodActionDelegate) Delegate.CreateDelegate (typeof (CommandMethodActionDelegate), instance, methodinfo.Name); 84 commandlist.Add (cmdatt.CommandName, commanddelegate) 85} 8687} 88} 899091 internal object Execute (string commandName, ICommandParameter commandParameter) 92 {93 if (commandName = = null) 94 {95 throw new ArgumentNullException ("commandName") 96} 97 if (! commandlist.ContainsKey (commandName)) 98 {99 return new ArgumentNullException ("invalid command not included"); 100} 101 CommandMethodActionDelegate cmdaction = commandlist [commandName]; 102 return cmdaction.Invoke (commandParameter) In the CommandController type, the RegisterCommand () method registers the extended command to the command controller. In the example, the registration method is manually passed in the type instance to register, or the implementation can be modified to obtain the assembly of all the dependencies of the current system when the command controller starts, obtain all the extended command types that conform to the type specification, and register to the controller.

The HostDevelopment type mentioned in the above code is a host container object I defined, which is used to host the command controller, and in this series of articles, HostDevelopment is used to host the controller, which is later. Now take a look at the temporary definition of HostDevelopment:

1 public delegate object CommandMethodActionDelegate (ICommandParameter commandParameter); 23 public class CommandController4 {5 private static CommandController _ Instance = null;67 public static CommandController Instance8 {9 get10 {11 if (_ Instance = = null) 12 {13 _ Instance = new CommandController (HostDevelopment.Instance) 14} 15 return _ Instance;16} 17} 1819 private HostDevelopment _ CommandDevelopment = HostDevelopment.Instance;2021 public CommandController (HostDevelopment commandDevelopment) 22 {23 _ CommandDevelopment = commandDevelopment;24} 2526 private Dictionary commandlist = new Dictionary (); 272829 private List _ commandNames = null 30 / / 31 / / Command name collection 32 / 33 public List CommandNames34 {35 get36 {37 if (_ commandNames = = null) 38 {39 GetCommandNames (); 40} 41 return _ commandNames 42} 43} 4445 private void GetCommandNames () 46 {4748 if (commandlist.Count > 0) 49 {50 if (_ commandNames = = null) 51 {52 _ commandNames = new List () 53} 54 foreach (string name in commandlist.Keys) 55 {56 _ commandNames.Add (name); 57} 58} 59} 6061 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} 7273 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 {8283 CommandMethodActionDelegate commanddelegate = (CommandMethodActionDelegate) Delegate.CreateDelegate (typeof (CommandMethodActionDelegate), instance, methodinfo.Name); 84 commandlist.Add (cmdatt.CommandName, commanddelegate) 85} 8687} 88} 899091 internal object Execute (string commandName, ICommandParameter commandParameter) 92 {93 if (commandName = = null) 94 {95 throw new ArgumentNullException ("commandName") 96} 97 if (! commandlist.ContainsKey (commandName)) 98 {99 return new ArgumentNullException ("invalid command not included"); 100} 101 CommandMethodActionDelegate cmdaction = commandlist [commandName]; 102 return cmdaction.Invoke (commandParameter) 103} 104} after reading these, you should have a general understanding, but now this kind of code cannot be tested because of the lack of some entities. Define a default implementation of the command parameter specification: 1 public class CommandParameterCase:ICommandParameter2 {3 private string _ StrText;45 public string StrText6 {7 get {return _ StrText;} 8 set {_ StrText = value } 9} 10} and then modify the first function in the CommandTest type called test (the corresponding command name is MyCommandone), the name of the function is a bit casual, you do not mind this. 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} so everything you need is defined. Let's take another look at the calling 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 see its value either by hitting a breakpoint or by outputting strtext. There are no more explanations here. The above is all the contents of the article "sample Analysis of extended commands in C#". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report