I have embedded an IronPython engine in a C# application. I want to expose some custom commands (methods) to the interpreter. How do I do this?
Currently, I have something like this:
public delegate void MyMethodDel(string printText);
Main(string[] args)
{
ScriptEngine engine = Python.CreateEngine();
ScriptScope scope = engine.CreateScope();
MyMethodDel del = new MyMethodDel(MyPrintMethod);
scope.SetVariable("myprintcommand", del);
while(true)
{
Console.Write(">>>");
string line = Console.ReadLine();
ScriptSource script = engine.CreateScriptSourceFromString(line, SourceCodeKind.SingleStatement);
CompiledCode code = script.Compile();
script.Execute(scope);
}
}
void MyPrintMethod(string text)
{
Console.WriteLine(text);
}
I can use this like this:
>>>myprintcommand("Hello World!")
Hello World!
>>>
This works fine. I wanted to know, if this is the correct way/best practice to do what I want to achieve?
How can I expose overloads of the same method. For example, if I wanted to expose a method like myprintcommand(string format, object[] args).
With the way I am currently doing it, the key "myprintcommand" can be mapped to only one delegate. Therefore I will have to change the name of the command/method if I want to expose the overloaded "myprintcommand" to the interpreter. Is there any other way to achieve what I want?