繁体   English   中英

放置IronPython运行时后,从C#调用IronPython函数

[英]Invoking IronPython function from C# after IronPython runtime was disposed

假设我们在IronPython中具有以下代码:

def hello():
    print "Hello from Python"

我们在以下C#代码中调用函数hello()

    private static void GetPythonFunction()
    {
        ScriptRuntime scriptRuntime = IronPython.Hosting.Python.CreateRuntime();
        ScriptScope scope = scriptRuntime.ExecuteFile(@"Python\helloFunc.py");
        Action hello = scope.GetVariable<Action>("hello");
        hello();
        scriptRuntime.Shutdown();
        hello(); // after IronPython runtime was disposed
    }

这给了我们结果:

Hello from Python
Hello from Python

为什么即使处置了运行时环境,第二个调用hello()也会工作两次?

Shutdown实际上并不会终止运行时,它只是运行一些清理例程。 但是,事后情况可能会变得很奇怪,因此无法保证任何事情都会起作用。

另外, hello (我不记得如何创建委托的确切细节)已经引用了运行时和引擎,从而避免了一切都成为GC。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM