2
this SO问题提供了用于在C#中创建python类的实例的代码。使用类名称将python类实例化为C#使用类名作为字符串
以下代码强制提前知道python函数的名称。不过,我需要指定类名和字符串执行的函数名。要做到这一点
ScriptEngine engine = Python.CreateEngine();
ScriptSource source = engine.CreateScriptSourceFromFile("Calculator.py");
ScriptScope scope = engine.CreateScope();
source.Execute(scope);
dynamic class_object = scope.GetVariable("Calculator");
dynamic class_instance = class_object();
int result = class_instance.add(4, 5); // I need to call the function by a string