运行在C#与IronPython的一个特定的Python函数 [英] Run a particular Python function in C# with IronPython

查看:1554
本文介绍了运行在C#与IronPython的一个特定的Python函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我有一个简单的类,它包装为我使用的Python引擎(IronPython的)。虽然代码看起来大它是非常简单的,所以我把它复制在这里与我的问题更加清晰

So far I have a simple class that wraps a python engine (IronPython) for my use. Although code looks big it's really simple so I copy it here to be more clear with my issue.

下面的代码:

public class PythonInstance
{
    ScriptEngine engine;
    ScriptScope scope;
    ScriptSource source;

    public PythonInstance()
    {
        engine = Python.CreateEngine();
        scope = engine.CreateScope();
    }

    public void LoadCode(string code)
    {
        source = engine.CreateScriptSourceFromString(code, Microsoft.Scripting.SourceCodeKind.Statements);
        source.Compile();
    }

    public void SetVariable(string key, dynamic variable)
    {
        scope.SetVariable(key, variable);
    }

    public void RunCode()
    {
        source.Execute(scope);
    }

    public void CallFunction(string function)
    {
        //?????? no idea what to call here
    }

}



所以, ,它的伟大工程,但只允许我在一次执行所有python脚本...但我想这样做是为了能够从一个pythos脚本中调用特定的功能。

So, it works great but it only allows me to execute all python script at once... but what I would like to do is to be able to call particular functions from within a pythos script.

所以,我的问题:我如何调用加载脚本特定功能

So, my question: How do I call particular function in the loaded script?

我试图找到一些信息?或教程可惜找不到任何东西。

I was trying to find some information or tutorials but unfortunately couldn't find anything.

推荐答案

由于在建议意见,我是能够弄清楚如何使用它。下面是我现在有:

Thanks to suggestion in comments I was able to figure out how to use it. Here's what I have now:

public class PythonInstance
{
    private ScriptEngine engine;
    private ScriptScope scope;
    private ScriptSource source;
    private CompiledCode compiled;
    private object pythonClass;

    public PythonInstance(string code, string className = "PyClass")
    {
        //creating engine and stuff
        engine = Python.CreateEngine();
        scope = engine.CreateScope();

        //loading and compiling code
        source = engine.CreateScriptSourceFromString(code, Microsoft.Scripting.SourceCodeKind.Statements);
        compiled = source.Compile();

        //now executing this code (the code should contain a class)
        compiled.Execute(scope);

        //now creating an object that could be used to access the stuff inside a python script
        pythonClass = engine.Operations.Invoke(scope.GetVariable(className));
    }

    public void SetVariable(string variable, dynamic value)
    {
        scope.SetVariable(variable, value);
    }

    public dynamic GetVariable(string variable)
    {
        return scope.GetVariable(variable);
    }

    public void CallMethod(string method, params dynamic[] arguments)
    {
        engine.Operations.InvokeMember(pythonClass, method, arguments);
    }

    public dynamic CallFunction(string method, params dynamic[] arguments)
    {
        return engine.Operations.InvokeMember(pythonClass, method, arguments);
    }

}

要测试它:

        PythonInstance py = new PythonInstance(@"
class PyClass:
    def __init__(self):
        pass

    def somemethod(self):
        print 'in some method'

    def isodd(self, n):
        return 1 == n % 2
");
        py.CallMethod("somemethod");
        Console.WriteLine(py.CallFunction("isodd", 6));

这篇关于运行在C#与IronPython的一个特定的Python函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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