C# MSIL call method and pass an object[]

1k views Asked by At

I want to convert a method in C# to MSIL code using the reflection emit dependency.

The method that I'm trying to convert is the transformation method:

public class AClass
{
    public string Transformation(string a, string b, string c)
    {
        return new AnotherClass().Method(new object[] { a, b, c });
    }
}

public class AnotherClass
{
    public string Method(params object[] objs)
    {
        return "AAA";
    }
}

So basically create an instance of a class and call a Method Function with a new object[] that will handle have ALL the parameters passed to it.

When I look into IlSpy I have this:

.method public hidebysig 
instance string Transformation (
    string a,
    string b,
    string c
) cil managed 
{
// Method begins at RVA 0x20d0
// Code size 34 (0x22)
.maxstack 5
.locals init (
    [0] string
)

IL_0000: nop
IL_0001: newobj instance void ConsoleApp1.AnotherClass::.ctor()
IL_0006: ldc.i4.3
IL_0007: newarr [mscorlib]System.Object
IL_000c: dup
IL_000d: ldc.i4.0
IL_000e: ldarg.1
IL_000f: stelem.ref
IL_0010: dup
IL_0011: ldc.i4.1
IL_0012: ldarg.2
IL_0013: stelem.ref
IL_0014: dup
IL_0015: ldc.i4.2
IL_0016: ldarg.3
IL_0017: stelem.ref
IL_0018: call instance string ConsoleApp1.AnotherClass::Method(object[])
IL_001d: stloc.0
IL_001e: br.s IL_0020

IL_0020: ldloc.0
IL_0021: ret
} // end of method AClass::Transformation

And tried to make a function that would represent that:

 public static void CallFucntionThroughIl(ILGenerator il, Type[] parameters, MethodInfo method)
    {
        il.Emit(OpCodes.Nop);

        il.Emit(OpCodes.Newobj, typeof(AnotherClass).GetConstructor(Type.EmptyTypes));

        il.Emit(OpCodes.Ldc_I4, parameters.Length);

        il.Emit(OpCodes.Newarr, typeof(object));

        for (int i = 0; i < parameters.Length; i++)
        {
            il.Emit(OpCodes.Dup);
            il.Emit(OpCodes.Ldc_I4, i);
            il.Emit(OpCodes.Ldarg, i + 1);
            il.Emit(OpCodes.Stelem_Ref);
        }

        il.EmitCall(OpCodes.Callvirt,
            typeof(AnotherClass).GetMethod("Method").MakeGenericMethod(method.ReturnType),
            new Type[] { typeof(object[]) });

        il.Emit(OpCodes.Stloc_0);

        var label = il.DefineLabel();

        il.Emit(OpCodes.Br_S, label);

        il.MarkLabel(label);
        il.Emit(OpCodes.Ldloc_0);

        il.Emit(OpCodes.Ret);
    }

But there's a time where I don't know what I'm doing wrong and the only error I get when debugging is: System.InvalidProgramException: 'Common Language Runtime detected an invalid program.'

The goal is just replicate what the "Transformation" function does, grab all the parameters pass it into an objects[] call a method with that object[] in the parameters and return what the other method returns.

Note: the transformation function does not always going to receive 3 strings, can also be other objects.

1

There are 1 answers

0
Diogo Neves On BEST ANSWER

After digging for a long time:

It was missing a DeclareLocal after the Nop:

il.Emit(OpCodes.Nop);
il.DeclareLocal(typeof(object[]));

Probably crashed when trying to allocate to a local non existing variable.

il.Emit(OpCodes.Stloc_0);