【发布时间】:2018-12-19 21:29:07
【问题描述】:
我正在使用 CodeDom 创建代码。但是,CodeDom 不支持 lambda 语句。所以我现在试图通过创建一个本地方法来模仿 lambda 语句,并将该方法作为参数传递给另一个方法。像这样:
public string MainMethod(string myParameter)
{
string g() { return instance.MainMethod(myParameter); }
return someOtherMethod(g);
}
方法 'MainMethod' 已经使用 CodeDom 生成 我正在尝试在那里获取本地方法。但是,到目前为止,我找不到办法做到这一点。我可以使用一些帮助。
我已经尝试将 CodeMemberMethod 添加到 CodeMemberMethod 但似乎没有办法做到这一点。我似乎找不到任何替代方案。
目前我的 CodeDom 代码使用 MethodInfo 作为基础:
var method = new CodeMemberMethod();
method.Name = methodInfo.Name;
method.ReturnType = new CodeTypeReference(methodInfo.ReturnType);
//left out parameter stuff
var gMethod = new CodeMemberMethod() { Name = "g", ReturnType = new CodeTypeReference(methodInfo.ReturnType) };
gMethod.Statements.Add(new CodeMethodReturnStatement(new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(new CodeVariableReferenceExpression("instance"), methodInfo.Name), parameterReferences.ToArray())));
method.Statements.Add(gMethod);
现在,痛苦在于最后一句话。我实际上是在尝试将 CodeMemberMethod 添加到 CodeMemberMethod,这在 Statements 属性中是不允许的。有没有其他方法可以做到这一点?
【问题讨论】: