假设我有以下代码:
public static class Foo
{
public static void Bar() {}
}
在 IronPython 中,我想要:
Bar()
无需包含 Foo 就行了。现在,我知道我可以说:
var Bar = Foo.Bar
Bar()
但我想使用 SetVariable 将 Bar 添加到我的 C# 代码中的 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);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)