-2

基本上我有两个问题:

1) 如何在 C# 应用程序中发出或生成IronPython代码(工具、库)。这个过程的结果应该是由真实IronPython代码而不是 IL 代码组成的字符串。

2) 上述方法比IronPython您自己生成代码有多大好处(只需使用StringBuilder)?

我正在寻找一些类似于这个 IMAGINARY 伪代码生成器的代码生成器库:

IronPythonCodeGenerator generator = new IronPythonCodeGenerator();
Parameter param = new Parameter("str");
ParameterValue value=new ParameterValue(param,"This piece is printed by the generated code!!!");
Function function = IronPythonCodeGenerator.CreateFunction("PrintExtended",param);
function.AppendStatement("print",param);
function.AppendStatement(Statements.Return);
FunctionCall functionCall = new FunctionCall(function,value);
generator.MainBody.Append(function);
generator.MainBody.Append(functionCall);

Console.WriteLine(generator.MainBody.ToString());

,它输出 IronPython 代码:

def PrintExtended( str ):
   print str;
   return;

PrintExtended("This piece is printed by the generated code!!!");
4

1 回答 1

2

Reflection.Emit用于生成 IL 代码,而不是用于生成高级语言代码。因此,如果您的目标语言是 IronPython,那么在 a 中构建它StringBuilder可能是您最好的选择。

当然,这取决于您的需求。如果您只想生成代码而不想更改方法的顺序,或者在定义方法后修改方法等,那么只需在 a 中构造代码StringBuilder然后编译它是最简单的方法。

于 2013-10-28T10:34:39.487 回答