假设我有以下代码:
public static class Foo { public static void Bar() {} }
在IronPython中,我想:
Bar()
无需在线上包含Foo.现在,我知道我可以说:
var Bar = Foo.Bar Bar()
解决方法
创建委托方法并设置范围.
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); } }