C# 调用IronPython函数时CodeContext的相关性

C# 调用IronPython函数时CodeContext的相关性,c#,ironpython,C#,Ironpython,我正在从C#调用IronPython函数。从定义上看,此函数似乎捕获了其原始范围。当我以后在没有显式作用域的情况下调用它时,它仍然可以访问原始作用域中的值。即使我更改了范围值,它也会正确读取新值。看看这个例子: using IronPython.Hosting; using IronPython.Runtime; using IronPython.Runtime.Operations; using Microsoft.Scripting; using Microsoft.Scripting.Ho

我正在从C#调用IronPython函数。从定义上看,此函数似乎捕获了其原始范围。当我以后在没有显式作用域的情况下调用它时,它仍然可以访问原始作用域中的值。即使我更改了范围值,它也会正确读取新值。看看这个例子:

using IronPython.Hosting;
using IronPython.Runtime;
using IronPython.Runtime.Operations;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;

namespace IronPythonFuncTest {
    class Program {
        static void Main() {
            ScriptEngine scriptEngine = Python.CreateEngine();

            // Create scope with a global value for the script to use
            ScriptScope scriptScope = scriptEngine.CreateScope();
            scriptScope.SetVariable("someGlobalValue", 10);

            // Execute script defining function foo(x)
            string script = "def foo(): print(someGlobalValue)";
            ScriptSource scriptSource = scriptEngine.
                CreateScriptSourceFromString(script, SourceCodeKind.Statements);
            scriptSource.Execute(scriptScope);

            // Extract foo from the scope
            PythonFunction foo = scriptScope.GetVariable<PythonFunction>("foo");

            // Change someGlobalValue
            scriptScope.SetVariable("someGlobalValue", 42);

            // Call foo. This prints 42, not 10 (or nothing).
            PythonCalls.Call(foo);
        }
    }
}
使用IronPython.Hosting;
使用IronPython.Runtime;
使用IronPython.Runtime.Operations;
使用Microsoft.Scripting;
使用Microsoft.Scripting.Hosting;
名称空间IronPythonFuncTest{
班级计划{
静态void Main(){
ScriptEngine ScriptEngine=Python.CreateEngine();
//使用脚本要使用的全局值创建作用域
ScriptScope ScriptScope=scriptEngine.CreateScope();
SetVariable(“someGlobalValue”,10);
//执行脚本定义函数foo(x)
string script=“def foo():print(someGlobalValue)”;
ScriptSource ScriptSource=scriptEngine。
CreateScriptSourceFromString(脚本、SourceCodeKind.Statements);
scriptSource.Execute(scriptScope);
//从范围中提取foo
PythonFunction foo=scriptScope.GetVariable(“foo”);
//改变某些全局值
SetVariable(“someGlobalValue”,42);
//打电话给foo,这打印的是42,不是10(或者什么都没有)。
PythonCalls.Call(foo);
}
}
}

现在我想知道:
PythonCalls.Call()
的大多数重载都需要一个
CodeContext
对象(如果我理解正确,它主要表示一个范围)。如果调用上述IronPython函数而不传递代码上下文,是否会丢失一些东西?鉴于函数在创建时显然捕获了其原始范围,因此传递额外的代码上下文似乎没有任何意义。在某些情况下,我是否这样做会产生影响吗?

为什么要在
PythonCall.call上调用
foo
?试着这样做:
scriptScope.Host.ScriptEngine.Operations.InvokeMember(类实例、方法名、参数)
比Ironpython能够自己正确处理
CodeContext
。您可以在此处找到一个示例实现:

据我所知,
CodeContext
不仅仅是范围,请看一下定义:

/// <summary>
/// Captures and flows the state of executing code from the generated 
/// Python code into the IronPython runtime.
/// </summary>    
[DebuggerTypeProxy(typeof(CodeContext.DebugProxy)), DebuggerDisplay("module: {ModuleName}", Type="module")]
public sealed class CodeContext { // ... }
//
///捕获并流式处理从生成的
///将Python代码导入IronPython运行时。
///     
[DebuggerTypeProxy(typeof(CodeContext.DebugProxy)),DebuggerDisplay(“模块:{ModuleName}”,Type=“模块”)]
公共密封类CodeContext{//…}
希望这有帮助