public void EmitSwitch_EmitsProvidedInstructions(int value) { var method = new DynamicMethod("Test", typeof(int), new Type[0]); var generator = method.GetILGenerator(); var subject = new ILGeneratorWrapper(generator); // Act var endOfMethod = subject.DefineLabel(); var labels = new[] { subject.DefineLabel(), subject.DefineLabel() }; subject.Emit(OpCodes.Ldc_I4_0); subject.Emit(OpCodes.Switch, labels); subject.MarkLabel(labels[0]); subject.Emit(OpCodes.Ldc_I4, value); subject.Emit(OpCodes.Br_S, endOfMethod); subject.MarkLabel(labels[1]); subject.Emit(OpCodes.Ldc_I4_8, value); subject.Emit(OpCodes.Br_S, endOfMethod); subject.MarkLabel(endOfMethod); subject.Emit(OpCodes.Ret); int result = (int)method.Invoke(null, new object[0]); // Assert Assert.AreEqual(value, result); }
public void EmitBranch_EmitsProvidedInstructions(int value) { var method = new DynamicMethod("Test", typeof(int), new Type[0]); var generator = method.GetILGenerator(); var subject = new ILGeneratorWrapper(generator); // Act var label = subject.DefineLabel(); subject.Emit(OpCodes.Ldc_I4, value); subject.Emit(OpCodes.Br, label); subject.Emit(OpCodes.Ldc_I4, 123); subject.MarkLabel(label); subject.Emit(OpCodes.Ret); int result = (int)method.Invoke(null, new object[0]); // Assert Assert.AreEqual(value, result); }