lundi 19 septembre 2016

load object to stack in Reflection.Emit C#

I'm trying to adapt this workaround to my project. Instead of a string I want to give an object (of type RevitFamily) to the base constructor.

Unfortunately, I can't find out how to load an object or object reference onto the stack using Reflection.Emit. Can Anyone help me?

So far my code looks like this, but I can't give the object to gen.Emit(opcode,_):

                TypeBuilder tb = mb.DefineType(newtypename, new TypeAttributes(), typeof(LoadFamily));
                var ci = typeof(LoadFamily).GetConstructor(new[] { typeof(RevitFamily) });
                var constructorBuilder = tb.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[0]);
                var gen = constructorBuilder.GetILGenerator();
                gen.DeclareLocal(typeof(RevitFamily));

                Label ilLabel = gen.DefineLabel();
                gen.Emit(OpCodes.Ldarg_0);                // Load "this" onto eval stack
                // Here I should some how get he object f in the stack....
                gen.Emit(OpCodes.Ldloca, f);              // Load pointer to RevitFamily on stack
                gen.Emit(OpCodes.Call, ci);               // call base constructor (consumes "this" and the string)
                gen.Emit(OpCodes.Nop);                    // Fill some space - this is how it is generated for equivalent C# code
                gen.Emit(OpCodes.Nop);
                gen.Emit(OpCodes.Nop);
                gen.Emit(OpCodes.Ret);                    // return from constructor
                t = tb.CreateType();

It is supposed to create dynamic type with a constructor similar to this:

public class LoadconcreteFamily : LoadFamily
{
    public LoadconcreteFamily() : base(f)
    { }
}





Aucun commentaire:

Enregistrer un commentaire