c#-4.0 – 向IronPython范围添加静态方法
发布时间:2020-12-15 08:20:22 所属栏目:百科 来源:网络整理
导读:假设我有以下代码: public static class Foo{ public static void Bar() {}} 在IronPython中,我想: Bar() 无需在线上包含Foo.现在,我知道我可以说: var Bar = Foo.BarBar() 但我想使用SetVariable在我的C#代码中将Bar添加到ScriptScope.我怎样才能做到这
假设我有以下代码:
public static class Foo { public static void Bar() {} } 在IronPython中,我想: Bar() 无需在线上包含Foo.现在,我知道我可以说: var Bar = Foo.Bar Bar() 但我想使用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); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |