假设我有以下代码:
public static class Foo
{
public static void Bar() {}
}
Run Code Online (Sandbox Code Playgroud)
在IronPython中,我想:
Bar()
Run Code Online (Sandbox Code Playgroud)
无需在线上包含Foo.现在,我知道我可以说:
var Bar = Foo.Bar
Bar()
Run Code Online (Sandbox Code Playgroud)
但我想使用SetVariable在我的C#代码中将Bar添加到ScriptScope.我怎样才能做到这一点?
创建委托方法并设置范围.
public class Program
{
public static void Main(string[] args)
{
var python = Python.CreateEngine();
var scriptScope = python.CreateScope();
scriptScope.SetVariable("Print", new Action<int>(Bar.Print));
python.Execute(
"Print(10)",
scriptScope
);
}
}
public static class Bar
{
public static void Print(int a)
{
Console.WriteLine("Print:{0}", a);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1821 次 |
| 最近记录: |