Kra*_*ros 8 c# compiler-construction recursion
我目前正在使用 C# 开发一个编译器,其中行为由 LambdaExpressions 定义,然后使用 CompileToMethod,转换为 MethodBuilders 并保存到 DLL。所有函数都是公共和静态的。
但是,在定义行为并创建/密封声明类型之前,我找不到从 MethodBuilder 中提取可用 MethodInfo(或其他引用方法)的方法。这意味着在那之前,不可能使用 Expression.Call 来调用这些函数。这使得两个函数之间不可能进行自递归或相互引用。
我最终使用反射在运行时调用函数,但它非常次优,我仍然很好奇是否有更好的方法。
如何确保使用 LambdaExpression.CompileToMethod(MethodBuilder) 创建的函数可以自调用?
或者,有没有其他方法可以使用 LambdaExpressions 来实现这一点并支持将静态方法保存为 dll?
我希望这有帮助。
这是完整的代码示例,它使用单个静态递归方法生成运行时定义的类型。
为了示例的简单性,递归方法是无限的 - 在 Main 方法的末尾调用递归方法
static void Main(string[] args)
{
var moduleBuilder = CreateDynamicModuleBuilder();
var typeBuilder = moduleBuilder.DefineType("Person", TypeAttributes.Public | TypeAttributes.Class);
var methodBuilder = typeBuilder.DefineMethod("SayHello", MethodAttributes.Static | MethodAttributes.Public);
var methodExpression = CreateRecursiveExpression();
var lambda = Expression.Lambda(methodExpression);
lambda.CompileToMethod(methodBuilder);
var typeInfo = typeBuilder.CreateType();
var methodInfo = typeInfo.GetMethod("SayHello", BindingFlags.Public | BindingFlags.Static);
methodInfo.Invoke(null, null);
}
private static Expression CreateRecursiveExpression()
{
var methodInfo = typeof(Console).GetMethod("WriteLine", new[] { typeof(String) });
var arg = Expression.Constant("Hello");
var consoleCall = Expression.Call(methodInfo, arg);
var sayHelloActionVariable = Expression.Variable(typeof(Action), "sayHelloAction");
var block = Expression.Block(
new[] { sayHelloActionVariable },
Expression.Assign(
sayHelloActionVariable,
Expression.Lambda(
Expression.Block(
consoleCall,
Expression.Invoke(sayHelloActionVariable)
)
)
),
Expression.Invoke(sayHelloActionVariable)
);
return block;
}
private static ModuleBuilder CreateDynamicModuleBuilder()
{
var name = new AssemblyName("Example.DynamicRecursion");
var am = AssemblyBuilder.DefineDynamicAssembly(name, AssemblyBuilderAccess.RunAndSave);
var mb = am.DefineDynamicModule(name.Name, $"{name.Name}.dll");
return mb;
}
Run Code Online (Sandbox Code Playgroud)
此代码将创建具有以下签名的类型
public class Person
{
public static void SayHello()
{
Action sayHelloAction;
sayHelloAction = () =>
{
Console.WriteLine("Hello");
sayHelloAction();
}
sayHelloAction();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
108 次 |
| 最近记录: |