IronPython调用方法按名称 [英] IronPython call method by name

查看:161
本文介绍了IronPython调用方法按名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法通过名称调用方法和指定参数而不使用动态? (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调用方法按名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆