From a0403492ad79398be8d97979acae00a2aca4dfdd Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Tue, 13 Sep 2016 16:51:39 +1200 Subject: [PATCH] -Tests --- .../DynamicReflectionDelegateFactoryTests.cs | 31 +++++++++++++++++++ ...xpressionReflectionDelegateFactoryTests.cs | 31 +++++++++++++++++++ .../DynamicReflectionDelegateFactory.cs | 7 +---- Src/Newtonsoft.Json/Utilities/MethodBinder.cs | 4 +-- 4 files changed, 65 insertions(+), 8 deletions(-) diff --git a/Src/Newtonsoft.Json.Tests/Utilities/DynamicReflectionDelegateFactoryTests.cs b/Src/Newtonsoft.Json.Tests/Utilities/DynamicReflectionDelegateFactoryTests.cs index a9684e29..f11ff2cb 100644 --- a/Src/Newtonsoft.Json.Tests/Utilities/DynamicReflectionDelegateFactoryTests.cs +++ b/Src/Newtonsoft.Json.Tests/Utilities/DynamicReflectionDelegateFactoryTests.cs @@ -205,6 +205,37 @@ namespace Newtonsoft.Json.Tests.Utilities MyStruct myStruct2 = creator2.Invoke(); Assert.AreEqual(0, myStruct2.IntProperty); } + + public struct TestStruct + { + public TestStruct(int i) + { + Value = i; + } + + public int Value { get; } + } + + public static TestStruct StructMethod(TestStruct s) + { + return new TestStruct(s.Value + s.Value); + } + + [Test] + public void CreateStructMethodCall() + { + MethodInfo methodInfo = typeof(DynamicReflectionDelegateFactoryTests).GetMethod(nameof(StructMethod), new[] { typeof(TestStruct) }); + + Assert.IsNotNull(methodInfo); + + MethodCall call = DynamicReflectionDelegateFactory.Instance.CreateMethodCall(methodInfo); + + object result = call(null, new TestStruct(123)); + Assert.IsNotNull(result); + + TestStruct s = (TestStruct)result; + Assert.AreEqual(246, s.Value); + } } } diff --git a/Src/Newtonsoft.Json.Tests/Utilities/ExpressionReflectionDelegateFactoryTests.cs b/Src/Newtonsoft.Json.Tests/Utilities/ExpressionReflectionDelegateFactoryTests.cs index 28730208..ab02925c 100644 --- a/Src/Newtonsoft.Json.Tests/Utilities/ExpressionReflectionDelegateFactoryTests.cs +++ b/Src/Newtonsoft.Json.Tests/Utilities/ExpressionReflectionDelegateFactoryTests.cs @@ -374,6 +374,37 @@ namespace Newtonsoft.Json.Tests.Utilities MyStruct myStruct2 = creator2.Invoke(); Assert.AreEqual(0, myStruct2.IntProperty); } + + public struct TestStruct + { + public TestStruct(int i) + { + Value = i; + } + + public int Value { get; } + } + + public static TestStruct StructMethod(TestStruct s) + { + return new TestStruct(s.Value + s.Value); + } + + [Test] + public void CreateStructMethodCall() + { + MethodInfo methodInfo = typeof(ExpressionReflectionDelegateFactoryTests).GetMethod(nameof(StructMethod), new[] { typeof(TestStruct) }); + + Assert.IsNotNull(methodInfo); + + MethodCall call = ExpressionReflectionDelegateFactory.Instance.CreateMethodCall(methodInfo); + + object result = call(null, new TestStruct(123)); + Assert.IsNotNull(result); + + TestStruct s = (TestStruct)result; + Assert.AreEqual(246, s.Value); + } } } diff --git a/Src/Newtonsoft.Json/Utilities/DynamicReflectionDelegateFactory.cs b/Src/Newtonsoft.Json/Utilities/DynamicReflectionDelegateFactory.cs index 082bbaba..87151d2e 100644 --- a/Src/Newtonsoft.Json/Utilities/DynamicReflectionDelegateFactory.cs +++ b/Src/Newtonsoft.Json/Utilities/DynamicReflectionDelegateFactory.cs @@ -92,7 +92,6 @@ namespace Newtonsoft.Json.Utilities LocalBuilder localConvertible = generator.DeclareLocal(typeof(IConvertible)); LocalBuilder localObject = generator.DeclareLocal(typeof(object)); - int localVariableCount = 2; for (int i = 0; i < args.Length; i++) { @@ -140,11 +139,9 @@ namespace Newtonsoft.Json.Utilities } generator.Emit(OpCodes.Ldloca_S, localVariable); - - localVariableCount++; } else if (parameterType.IsValueType()) - { + { generator.PushArrayInstance(argsIndex, i); generator.Emit(OpCodes.Stloc_S, localObject); @@ -170,7 +167,6 @@ namespace Newtonsoft.Json.Utilities if (parameterType.IsPrimitive()) { // for primitive types we need to handle type widening (e.g. short -> int) - MethodInfo toParameterTypeMethod = typeof(IConvertible) .GetMethod("To" + parameterType.Name, new[] { typeof(IFormatProvider) }); @@ -209,7 +205,6 @@ namespace Newtonsoft.Json.Utilities // parameter finished, we out! generator.MarkLabel(finishedProcessingParameter); - localVariableCount++; } else { diff --git a/Src/Newtonsoft.Json/Utilities/MethodBinder.cs b/Src/Newtonsoft.Json/Utilities/MethodBinder.cs index ec9d2281..396f5af4 100644 --- a/Src/Newtonsoft.Json/Utilities/MethodBinder.cs +++ b/Src/Newtonsoft.Json/Utilities/MethodBinder.cs @@ -50,8 +50,8 @@ return true; } - int fromMask = 0, - toMask = 0; + int fromMask = 0; + int toMask = 0; for (int i = 0; i < PrimitiveTypes.Length; i++) {