.NET Object Scripts

I have a simple .NET application that runs as a Windows service. Say he 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 humanoid commands like

run left
walk right

Pretty similar to what you do in the Windows console. I wonder what is the best way to implement this mapping. Direct methods, of course, create their own line parser with a lot of switch statements, but I think if there is a better and faster method.

+5
source share
4 answers

, , , , , , :

[ScriptableClass]
public class MyClass
{
     [ScriptableMethod(typeof (Destination),typeof(int))]
     public void Run (Destination f, int distance) {}
}

, ScriptableClass. , , ScriptableMethod, ( ). "MyClass.Run", . , / .

, , .

+3

:

  • , approrpiate .

  • , Run(). , .

UPDATE: Aggreed, (. Guffa). - , IMHO.

+2

. , , , , .

, .
:

var methods = new Dictionary<string, Action<int>>();
methods.Add("run", n => MyClass.Run(n));
methods.Add("walk", n => MyClass.Walk(n));
methods.Add("wash", n => MyClass.Wash(n));

string cmd = "run";
int param = 42;
if (methods.ContainsKey(cmd)) {
   methods[cmd](param);
} else {
  Console.WriteLine('Say what, human?');
}
+2

Perhaps PowerShell CommandLet will be an option, although it will limit you or your PowerShell users (which is an excellent IMO shell).

+1
source

All Articles