mercredi 9 septembre 2020

C# - Reflection.Emit: Return constant

I want to make a DynamicMethod which in turn behave like the following for example:

AnyClass myClass = new AnyClass();
Func<AnyClass> myFunc = () => myClass;

I know that when I want to make it work with an int instead of AnyClass I have to use the following snippet to return every time the number 12:

// Define method
DynamicMethod method = new DynamicMethod(
    "Name",
    typeof(int),
    new Type[] { });

// Define method body
ILGenerator il = method.GetILGenerator();
il.Emit(OpCodes.Ldc_I4, 12);
il.Emit(OpCodes.Ret);

But now I ask myself how to do it with a not build-in class.





Aucun commentaire:

Enregistrer un commentaire