I have a simple .NET application which runs as Windows Service. Say it has one Class
MyClass
{
Run(Destination d)
Walk(Destination d)
Wash(Dishes d)
}
I want to create a console application where I would type simple human readable commands like
run left
walk right
Pretty much like you do in windows console. I wonder what is the best way to implement this mapping. A straight methods is of course create your own string parser with lots of switch statements but I think if there is better and faster method.
This might be too much for your needs, but I think a robust, flexible way to do it would be creating a couple of meta-data attributes to decorate the classes you might want to call from your shell, something like:
[ScriptableClass]
public class MyClass
{
[ScriptableMethod(typeof (Destination),typeof(int))]
public void Run (Destination f, int distance) {}
}
Then, during your shell's startup, you load your assemblies via reflection and look for types marked with the ScriptableClass attribute. For each of them, you inspect their public methods looking for those marked with the ScriptableMethod attribute, and you can build a dictionary of classes and their scriptable methods (along with info about parameters). For the previous example, you would have a 'MyClass.Run' command you can could from the shell. When received, your scripting engine would create / lookup an instance of the class and execute the method.
The advantage of this method is that you wouldn't have to modify your scripting engine every time you add new classes or modify methods.
You have two options here:
Use reflection to find the class method with the approrpiate name and invoking it.
Use the command pattern, where each command object would have a name and a Run()
method. You can easily find the desired command by name, then run it.
UPDATE: Aggreed, there are more than two options (see Guffa's answer). But these two are the ones that make the code clearer IMHO.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With