问题描述
有没有办法通过名称调用方法和指定参数而不使用动态? (C#)
Is there any way to call method by name and specify arguments without using dynamic? (C#)
更新:
`
public class Program
{
public static void Main(string [] args)
{
ScriptEngine engine = Python.CreateEngine();
ScriptScope scope = engine.CreateScope();
`public class Program { public static void Main(string[] args) { ScriptEngine engine = Python.CreateEngine(); ScriptScope scope = engine.CreateScope();
engine.ExecuteFile("script.py", scope);
dynamic calculator = scope.GetVariable("Calculator");
dynamic calculatorInstance = engine.Operations.CreateInstance(calculator);
dynamic result = engine.Operations.InvokeMember(calculatorInstance, "sample");
Console.ReadKey();
}
}
`
class Calculator:
def sample(self):
return'test'
这很好用
推荐答案
您可以使用 ObjectOperations
执行动态操作的类,您可以从 ScriptEngine
实例获取一个实例:
You can use the ObjectOperations
class to perform dynamic operations, and you can get an instance of it from a ScriptEngine
instance:
ScriptEngine engine = ...;
object result = engine.Operations.InvokeMember(o, "foo", 1, 2, "3");
这篇关于IronPython调用方法按名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!