与IronPython的一个C#应用程序的书面Simplfying DSL [英] Simplfying DSL written for a C# app with IronPython
问题描述
由于从<一个建议href="http://stackoverflow.com/questions/1324904/extending-c-net-application-build-a-custom-scripting-language-or-not">$p$pvious问题,我忙着出来的IronPython,IronRuby和嘘声来创建一个DSL为我的C#应用程序。停止一个是IronPython的,由于较大的用户和知识库。如果我能得到的东西很好地在这里工作,我就可以停下来。
Thanks to suggestions from a previous question, I'm busy trying out IronPython, IronRuby and Boo to create a DSL for my C# app. Stop one is IronPython, due to the larger user and knowledge base. If I can get something to work well here, I can just stop.
下面是我的问题:
我希望我的IronPython的脚本可以访问功能的一类被称为库。现在,我可以将集添加到IronPython的运行时间,并通过执行该语句在我创建的范围导入类:
I want my IronPython script to have access to the functions in a class called Lib. Right now I can add the assembly to the IronPython runtime and import the class by executing the statement in the scope I created:
// 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);
如果我想运行库:: PrintHello,这仅仅是一个Hello World风格的说法,我的Python脚本包含:
If I want to run Lib::PrintHello, which is just a hello world style statement, my Python script contains:
Lib.PrintHello()
或者(如果它不是静态的):
or (if it's not static):
library = new Lib()
library.PrintHello()
我怎样才能改变我的环境,这样我就可以有在Python脚本基本statments是这样的:
How can I change my environment so that I can just have basic statments in the Python script like this:
PrintHello
TurnOnPower
VerifyFrequency
TurnOffPower
etc...
我想这些脚本是简单的非程序员来写。我不希望他们必须知道什么是类或者它如何工作的。 IronPython的是真的就在那里,这样像,一些基本的操作去做,如果和基本功能的定义不要求我写一个编译器,我的DSL。
I want these scripts to be simple for a non-programmer to write. I don't want them to have to know what a class is or how it works. IronPython is really just there so that some basic operations like for, do, if, and a basic function definition don't require my writing a compiler for my DSL.
推荐答案
您应该能够做这样的事情:
You should be able to do something like:
var objOps = _engine.Operations;
var lib = new Lib();
foreach (string memberName in objOps.GetMemberNames(lib)) {
_scope.SetVariable(memberName, objOps.GetMember(lib, memberName));
}
这会得到所有成员的LIB objec,然后将它们注入了ScriptScope。这样做是瓦特/ Python的ObjectOperations类,让你下车成员将Python的成员。所以,如果你那么做类似的事情W¯¯/ IronRuby的同一code应该主要工作。
This will get all of the members from the lib objec and then inject them into the ScriptScope. This is done w/ the Python ObjectOperations class so that the members you get off will be Python members. So if you then do something similar w/ IronRuby the same code should basically work.
这篇关于与IronPython的一个C#应用程序的书面Simplfying DSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!