cgy*_*per 10 .net c# dsl ironpython
感谢上一个问题的建议,我正忙着尝试使用IronPython,IronRuby和Boo为我的C#应用程序创建一个DSL.由于更大的用户和知识库,第一步是IronPython.如果我能在这里找到合适的东西,我可以停下来.
这是我的问题:
我希望我的IronPython脚本可以访问名为Lib的类中的函数.现在我可以将程序集添加到IronPython运行时并通过执行我创建的作用域中的语句来导入该类:
// load 'ScriptLib' assembly
Assembly libraryAssembly = Assembly.LoadFile(libraryPath);
_runtime.LoadAssembly(libraryAssembly);
// import 'Lib' class from 'ScriptLib'
ScriptSource imports = _engine.CreateScriptSourceFromString("from ScriptLib import Lib", SourceCodeKind.Statements);
imports.Execute(_scope);
// run .py script:
ScriptSource script = _engine.CreateScriptSourceFromFile(scriptPath);
script.Execute(_scope);
Run Code Online (Sandbox Code Playgroud)
如果我想运行Lib :: PrintHello,这只是一个hello world style语句,我的Python脚本包含:
Lib.PrintHello()
Run Code Online (Sandbox Code Playgroud)
或(如果它不是静态的):
library = new Lib()
library.PrintHello()
Run Code Online (Sandbox Code Playgroud)
如何更改我的环境,以便我可以在Python脚本中获得如下的基本语句:
PrintHello
TurnOnPower
VerifyFrequency
TurnOffPower
etc...
Run Code Online (Sandbox Code Playgroud)
我希望这些脚本对于非程序员来说很简单.我不希望他们必须知道课程是什么或它是如何工作的.IronPython真的就在那里,所以像for,do,if和基本函数定义这样的基本操作不需要我为我的DSL编写编译器.
Din*_*and 20
你应该可以这样做:
var objOps = _engine.Operations;
var lib = new Lib();
foreach (string memberName in objOps.GetMemberNames(lib)) {
_scope.SetVariable(memberName, objOps.GetMember(lib, memberName));
}
Run Code Online (Sandbox Code Playgroud)
这将从lib objec中获取所有成员,然后将它们注入ScriptScope.这是通过Python ObjectOperations类完成的,这样你下载的成员就是Python成员.所以,如果你做类似的东西w/IronRuby,相同的代码基本上应该工作.
| 归档时间: |
|
| 查看次数: |
1160 次 |
| 最近记录: |