mardi 30 mars 2021

DynamicMethod pass objects to method

The problem

I need to generate a fast reflection based method during runtime. Therefore i looked into dynamic methods. The problem is im stuck with my limited knowledge.

Thats the method i need to call with a dynamic method.


public struct EntityManager{

    public void SetComponent<T>(Entity e, T component) where T : struct, IComponentData{
       // Simplified
    }
}

// The component to pass in it
public struct ComponentExample : IComponentData{}

And im stuck with the part where i need to pass the Entity and the Component into the method. I just cant find any example on how we do that.

var dm = new DynamicMethod(
    "SetComponent",       
    typeof(void),  
    new [] { typeof(Entity), typeof(ComponentExample)}, 
    false
);             

var il = dm.GetILGenerator();
// Emit Entity & the ComponentExample ???
il.EmitCall(OpCodes.Call, genericMethod, null);
il.Emit(OpCodes.Ret);

The question

How exactly do we "emit" an struct ( Entity ) and an object into the DynamicMethod for executing it ? Glad for any help on this topic !





Aucun commentaire:

Enregistrer un commentaire