I'm trying to dynamically generate an assembly which to call a func created by a user.
private MethodBuilder DefineGetMethod<TInput, TReturn>(
TypeBuilder tb, MethodDescriptor methodInfo, Func<TInput, TReturn> dynamicMethod)
{
//Define the function
var dynamicMethodBuilder = tb.DefineMethod(methodInfo.MethodName,
MethodAttributes.Public,
methodInfo.ReturnType, methodInfo.InputParameters.Select(x => x.Type).ToArray());
//Define the labels for the method inputs
for(var i = 0; i < methodInfo.InputParameters.Length; i++ )
{
// Position 0 is the return value, 1 is the 1st param, 2 is 2nd, etc.
var position = 1 + i;
var inputParam = methodInfo.InputParameters[i];
dynamicMethodBuilder.DefineParameter(position, ParameterAttributes.None, inputParam.Name);
}
var ilGenerator = dynamicMethodBuilder.GetILGenerator();
//Loads arg1
ilGenerator.Emit(OpCodes.Ldarg_1);
//Not sure how to pass the arg1 to the method body to return
var ilMethodBody = dynamicMethod.Method.GetMethodBody().GetILAsByteArray();
//Generates return
ilGenerator.Emit(OpCodes.Ret);
}
How I pass the loaded argument to the ilMethodBody
and return?
Aucun commentaire:
Enregistrer un commentaire