public void SwitchTest()
{
// set up temp assembly.
var assembly = Utilities.CreateTempNetAssembly();
var tableStream = assembly.NetDirectory.MetadataHeader.GetStream<TableStream>();
var methodTable = tableStream.GetTable<MethodDefinition>();
// write code.
var body = methodTable[0].MethodBody;
body.Instructions.Clear();
var targets = new[]
{
MsilInstruction.Create(MsilOpCodes.Nop),
MsilInstruction.Create(MsilOpCodes.Nop),
MsilInstruction.Create(MsilOpCodes.Nop),
MsilInstruction.Create(MsilOpCodes.Nop),
};
var end = MsilInstruction.Create(MsilOpCodes.Ret);
body.Instructions.Add(MsilInstruction.Create(MsilOpCodes.Ldc_I4_1));
body.Instructions.Add(MsilInstruction.Create(MsilOpCodes.Switch, targets));
foreach (var target in targets)
{
body.Instructions.Add(target);
body.Instructions.Add(MsilInstruction.Create(MsilOpCodes.Br, end));
}
body.Instructions.Add(end);
body.CalculateOffsets();
// build and validate.
assembly = Utilities.RebuildNetAssembly(assembly);
methodTable = assembly.NetDirectory.MetadataHeader.GetStream<TableStream>().GetTable<MethodDefinition>();
var operand = methodTable[0].MethodBody.Instructions[1].Operand;
Assert.IsInstanceOfType(operand, typeof(MsilInstruction[]));
var newTargets = (MsilInstruction[])operand;
Assert.AreEqual(targets.Length, newTargets.Length);
for (int i = 0; i < targets.Length; i++)
Assert.AreEqual(targets[i].Offset, newTargets[i].Offset);
}