[英]Call other method in IlGenerator
我正在通過TypeBuilder
構建自己的類型,並且試圖添加到該方法中,該方法將調用從不同對象收集的methodInfo
。
問題是我不知道如何使用ILGenerator.Emit
或ILGenerator.EmitCall
。
我嘗試使用il.EmitCall(OpCodes.Call, methodInfo, arguments)
和il.Emit(OpCodes.Call, methodInfo)
但它們都il.Emit(OpCodes.Call, methodInfo)
。 總是我得到這個錯誤:
[InvalidProgramException: Common Language Runtime detected an invalid program.]
MyImplementationController.Hello1() +0
[TargetInvocationException: Exception has been thrown by the target of an invocation.]
System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0
System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +192
System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +155
System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) +19
這是我的代碼:
foreach (var methodInfo in methodInfosFromSomewhere)
{
var arguments = methodInfo.GetParameters().Select(a => a.ParameterType).ToArray();
MethodBuilder mb = tb.DefineMethod(
methodInfo.Name,
MethodAttributes.Final | MethodAttributes.Public,
CallingConventions.HasThis,
methodInfo.ReturnType,
arguments);
// method
ILGenerator il = mb.GetILGenerator();
int numParams = arguments.Length;
for (byte x = 0; x < numParams; x++)
{
//il.Emit(OpCodes.Ldarg_S, x);
il.Emit(OpCodes.Ldstr, x);
}
//il.EmitCall(OpCodes.Call, methodInfo, arguments);
il.Emit(OpCodes.Call, methodInfo);
il.Emit(OpCodes.Ret);
}
@編輯
最后,我知道(可能)問題出在哪里! 當我調用Emit.Call
我不想在此對象中調用method。 我想從另一個對象調用方法。
請看這個例子:
// this is interface that we want to 'decorate'
public interface IMyInterface
{
MyResponse Hello1();
MyResponse Hello2(MyRequest request);
MyResponse Hello3(MyRequest request);
}
public class MyImplementation : IMyInterface
{
public MyResponse Hello1()
{
return new MyResponse { Name = "empty" };
}
// ... rest of implementation, it doesn't matter
}
我想生成的類像這樣:
public class GeneratedClass : ApiController
{
public MyInterface myImplementation { get; set; }
public MyResponse Hello1()
{
return myImplementation.Hello1();
}
// ... rest of implementation, it doesn't matter
}
如您所見,我想從其他對象調用方法。 我知道如何為對象創建屬性,但不知道如何從其他對象調用方法
public virtual void Emit(OpCode opcode, MethodInfo meth)
{
//...
if (opcode.Equals(OpCodes.Call) || opcode.Equals(OpCodes.Callvirt) || opcode.Equals(OpCodes.Newobj))
{
EmitCall(opcode, meth, null);
}
else
{
// ...
}
}
如您所見,如果OpCode
是Call
, Callvirt
或Newobj
,則Emit()
調用EmitCall()
,因此Emit()
與EmitCall()
不會有所區別。
使用OpCodes.Ldstr
發射需要一個string
類型的操作數。 您要做的是在發出OpCodes.Call
指令之前,將參數逐個加載到堆棧中。
代替:
for (byte x = 0; x < numParams; x++)
{
il.Emit(OpCodes.Ldstr, x);
}
嘗試這個:
switch (numParams)
{
case 0:
break;
case 1:
il.Emit(OpCodes.Ldarg_0);
break;
case 2:
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldarg_1);
break;
case 3:
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Ldarg_2);
break;
default:
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Ldarg_2);
il.Emit(OpCodes.Ldarg_3);
for (int i = 4; i < numParams; i++)
{
il.Emit(OpCodes.Ldarg, mb.GetParameters()[i]);
}
break;
}
問題更新后進行編輯:您必須在新類型中定義屬性myImplementation
。
嘗試這個:
// Create field to back your "myImplementation" property
FieldBuilder newBackingField = tb.DefineField("backingField_myImplementation", typeof(MyInterface), System.Reflection.FieldAttributes.Private);
// Create your "myImplementation" property
PropertyBuilder newProp = tb.DefineProperty("myImplementation", System.Reflection.PropertyAttributes.None, typeof(MyInterface), Type.EmptyTypes);
// Create get-method for your property
MethodBuilder getter = tb.DefineMethod("get_myImplementation", System.Reflection.MethodAttributes.Private);
ILGenerator getterILGen = getter.GetILGenerator();
// Basic implementation (return backing field value)
getterILGen.Emit(OpCodes.Ldarg_0);
getterILGen.Emit(OpCodes.Ldfld, newBackingField);
getterILGen.Emit(OpCodes.Ret);
// Create set-method for your property
MethodBuilder setter = tb.DefineMethod("set_myImplementation", System.Reflection.MethodAttributes.Private);
setter.DefineParameter(1, System.Reflection.ParameterAttributes.None, "value");
ILGenerator setterILGen = setter.GetILGenerator();
// Basic implementation (set backing field)
setterILGen.Emit(OpCodes.Ldarg_0);
setterILGen.Emit(OpCodes.Ldarg_1);
setterILGen.Emit(OpCodes.Stfld, newBackingField);
setterILGen.Emit(OpCodes.Ret);
// Hello1 Method
MethodBuilder hello1 = tb.DefineMethod("Hello1", System.Reflection.MethodAttributes.Public);
ILGenerator il = hello1.GetILGenerator();
// Here, add code to load arguments, if any (as shown previously in answer)
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Call, getter);
il.Emit(OpCodes.Callvirt, typeof(MyInterface).GetMethod("Hello1"));
il.Emit(OpCodes.Ret);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.