Hey guys,

I am trying to create a switch statement which should return a string value.
A similar example what I am trying to reproduce could look like this:

<http://puu.sh/cUuAi/e3f8482007.png>


That is my attempt creating the switch statement:


private static MethodDefinition CreateSwitchMethod(AssemblyDefinition 
AssemblyDef, string ReturnValue, int AmountOfCases)
        {
            // Create a new Methoddefinition
            MethodDefinition NewMethod = new 
MethodDefinition("SwitchMethodTest", MethodAttributes.Public | 
MethodAttributes.Static, AssemblyDef.MainModule.Import(typeof(string)));

            // Create Switch Method Body
            ILProcessor ilp = NewMethod.Body.GetILProcessor();
            List<Instruction> InstrList = new List<Instruction>();
            Instruction[] Switches = new Instruction[5];

            Switches[0] = ilp.Create(OpCodes.Ldstr, " ");
            Switches[1] = ilp.Create(OpCodes.Ldstr, " ");
            Switches[2] = ilp.Create(OpCodes.Ldstr, " ");
            Switches[3] = ilp.Create(OpCodes.Ldstr, ReturnValue);
            Switches[4] = ilp.Create(OpCodes.Ldstr, " ");

            // Add Isntructions to methodbody
            InstrList.Add(ilp.Create(OpCodes.Ldc_I4, 3));
            InstrList.Add(ilp.Create(OpCodes.Stloc_0));
            InstrList.Add(ilp.Create(OpCodes.Ldloc_0));
            InstrList.Add(ilp.Create(OpCodes.Stloc_1));
            InstrList.Add(ilp.Create(OpCodes.Ldloc_1));
            InstrList.Add(ilp.Create(OpCodes.Switch, Switches));
            InstrList.Add(Switches[0]);
            InstrList.Add(ilp.Create(OpCodes.Ret));
            InstrList.Add(Switches[1]);
            InstrList.Add(ilp.Create(OpCodes.Ret));
            InstrList.Add(Switches[2]);
            InstrList.Add(ilp.Create(OpCodes.Ret));
            InstrList.Add(Switches[3]);
            InstrList.Add(ilp.Create(OpCodes.Ret));
            InstrList.Add(Switches[4]);
            InstrList.Add(ilp.Create(OpCodes.Ret));

            foreach (Instruction Instr in InstrList)
            {
                NewMethod.Body.Instructions.Add(Instr);
            }

            return NewMethod;
        }


And thats how I call it:


 MethodDefinition MethodDef = CreateSwitchMethod(AssemblyDef, "Test with 
Switch", 3);


The problem is that the output is somehow invalid:

<http://puu.sh/cUuGi/c1d63f837f.png>

<http://puu.sh/cUuF1/f318e66733.png> 


Can someone explain me why that happens and how I can fix that?

-- 
-- 
--
mono-cecil
--- 
You received this message because you are subscribed to the Google Groups 
"mono-cecil" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to