mercredi 22 juin 2016

Invoke method on static filed using IL generator in C# reflection

I'm generating a new Type via TypeBuilder from C# reflection. The problem that I have right now is how to build method body to invoke method on static filed.

Right now my generated (at runtime) class looks like this:

public class Generated
{
    static private MyObject obj;

    public MyResponse Hello(MyRequest req1)
    {
        return obj.Hello(req1); // this part need to be grnerated
    }
}

So right now I'm generating method like this:

MethodBuilder mb = tb.DefineMethod(
    methodInfo.Name,
    MethodAttributes.Public | MethodAttributes.Final,
    CallingConventions.HasThis | CallingConventions.ExplicitThis,
    methodInfo.ReturnType,
    arguments);

ILGenerator il = mb.GetILGenerator();
il.Emit(OpCodes.Ldnull); // right now I'm just simply returning null
il.Emit(OpCodes.Ret);

So please tell me how can I load static field on stack in Il generator and then invoke certain method on it.





Aucun commentaire:

Enregistrer un commentaire