基本上我有两个问题:
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!!!");
答案 0 :(得分:2)
Reflection.Emit
用于生成IL代码,而不是用于生成高级语言代码。因此,如果您的目标语言是IronPython,那么在StringBuilder
中构建它可能是您最好的选择。
这当然取决于您的需求。如果您只想生成代码而不想更改方法的顺序,或者在定义了方法之后修改方法等,那么只需在StringBuilder
中构建代码然后编译它将是最简单的方法