jeudi 16 avril 2015

Calling Base Class Method using Reflection.Emit

I have set up my code to define a type, set the parent type as well as implement an interface. The problem I am having is that when I go to create the type it says that it cannot find the method implementations for the interface even though those methods are implemented on the parent type. From what I have seen you need to define pass through methods for a situation like this, here is my code to do that:



private void DefinePassThroughs(ref TypeBuilder typeBuilder, Type baseType, Type iServiceType)
{
var virtualMethods = iServiceType.GetMethods();
foreach (var imethod in virtualMethods)
{
var method = baseType.GetMethod(imethod.Name);
var paramTypes = method.GetParameters().Select(x => x.ParameterType).ToArray();

var passThroughMethod =
typeBuilder.DefineMethod(
method.Name,
MethodAttributes.Public,
CallingConventions.Standard,
method.ReturnType,
paramTypes);

var il = passThroughMethod.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
for (var i = 0; i < paramTypes.Length; i++)
{
il.Emit(OpCodes.Ldarg, i + 1);
}

il.EmitCall(OpCodes.Callvirt, method, null);
il.Emit(OpCodes.Ret);
typeBuilder.DefineMethodOverride(passThroughMethod, imethod);
}
}


When I try to create the type instance I get this error: "Signature of the body and declaration in a method implementation do not match." What am I missing here?






Aucun commentaire:

Enregistrer un commentaire