public void TestEmitConstructorInfo() { var c = new ILEngineUnitTestModel(0); var cType = c.GetType(); var cMethod = cType.GetConstructor(Type.EmptyTypes); var cResult = cMethod.Invoke(null); Assert.IsNotNull(cMethod); var expected = c; var builder = new ILInstructionBuilder(); builder.Write(OpCodes.Newobj, cMethod); builder.Write(OpCodes.Ret); var ilMethod = new ILMethod(MethodBase.GetCurrentMethod().Name, expected.GetType()); ilMethod.AddInstructions(builder.Instructions.ToArray()); ilMethod.Module = cType.GetType().Module; var method = ilMethod.Compile(); var actual = method.Invoke(null, null); Assert.IsTrue((ILEngineUnitTestModel)actual == expected, $"Actual: {actual}\r\nExpected:{expected}"); }
public void TestEmitMethod() { var c = new ILEngineUnitTestModel(1); var cType = c.GetType(); var cMethod = cType.GetMethod(nameof(c.GetValue)); var expected = c.Value; var builder = new ILInstructionBuilder(); builder.Write(OpCodes.Ldarg_0); builder.Write(OpCodes.Call, cMethod.MetadataToken); builder.Write(OpCodes.Ret); var frame = ILStackFrameBuilder.Build(builder.Instructions); frame.SetResolver(this.GetType()); frame.Args = new object[] { c }; frame.Execute(); var ilMethod = new ILMethod(MethodBase.GetCurrentMethod().Name, expected.GetType()); ilMethod.AddParameters(new[] { cType }); ilMethod.AddInstructions(builder.Instructions.ToArray()); ilMethod.Module = this.GetType().Module; var method = ilMethod.Compile(); var actual = method.Invoke(null, new object[] { c }); Assert.IsTrue((int)actual == expected, $"Actual: {actual}\r\nExpected:{expected}"); }