diff --git a/src/DynamicExpresso.Core/Lambda.cs b/src/DynamicExpresso.Core/Lambda.cs index 78a5279..183f360 100644 --- a/src/DynamicExpresso.Core/Lambda.cs +++ b/src/DynamicExpresso.Core/Lambda.cs @@ -83,63 +83,23 @@ public object Invoke(params object[] args) var parameters = new List(); var declaredParameters = DeclaredParameters.ToArray(); - int[] actualArgOrdering = null; - object[] orderedArgs = args; - var argsAreReordered = false; - if (args != null && args.Length > 0) + if (args != null) { if (declaredParameters.Length != args.Length) throw new InvalidOperationException("Arguments count mismatch."); - actualArgOrdering = new int[args.Length]; - var usedParametersIndex = new Dictionary(_parserArguments.Settings.KeyComparer); - foreach (var v in UsedParameters) - { - if (declaredParameters.Any(x => string.Equals(x.Name, v.Name, _parserArguments.Settings.KeyComparison))) - { - usedParametersIndex[v.Name] = usedParametersIndex.Count; - } - } - for (var i = 0; i < args.Length; i++) { - if (!usedParametersIndex.TryGetValue(declaredParameters[i].Name, out var actualArgIndex)) - { - actualArgIndex = -1; - } - if (actualArgIndex != i) - { - if (!argsAreReordered) - { - orderedArgs = (object[])orderedArgs.Clone(); - argsAreReordered = true; - } - if (actualArgIndex == -1) - { - Array.Resize(ref orderedArgs, args.Length - 1); - } - else - { - orderedArgs[actualArgIndex] = args[i]; - } - } - actualArgOrdering[i] = actualArgIndex; - } - } + var parameter = new Parameter( + declaredParameters[i].Name, + declaredParameters[i].Type, + args[i]); - var result = InvokeWithUsedParameters(orderedArgs); - if (argsAreReordered) - { - for (var i = 0; i < actualArgOrdering.Length; i++) - { - var pullFrom = actualArgOrdering[i]; - if (pullFrom >= 0) - { - args[i] = orderedArgs[pullFrom]; - } + parameters.Add(parameter); } } - return result; + + return Invoke(parameters); } private object InvokeWithUsedParameters(object[] orderedArgs) diff --git a/test/DynamicExpresso.UnitTest/ConstructorTest.cs b/test/DynamicExpresso.UnitTest/ConstructorTest.cs index dedc6c8..31435b3 100644 --- a/test/DynamicExpresso.UnitTest/ConstructorTest.cs +++ b/test/DynamicExpresso.UnitTest/ConstructorTest.cs @@ -225,8 +225,6 @@ public void Ctor_NewMyClassWithCross() strProp.Value }; Assert.AreEqual(new MyClassAdder() { { 1, 2, 3, 4, 5 }, "6", 7 }, target.Parse("new MyClassAdder(){{ 1, 2, 3, 4, 5},{StrProp = \"6\" },7}", strProp).Invoke(args)); - Assert.AreEqual("6", args[0]); - args[0] = "0"; Assert.AreEqual(new MyClassAdder() { { 1, 2, 3, 4, 5 }, string.Empty, 7 }, target.Eval("new MyClassAdder(){{ 1, 2, 3, 4, 5},string.Empty, 7}")); Assert.AreEqual(new MyClassAdder() {StrProp = string.Empty, MyArr = new[] { 1, 2, 3, 4, 5 }, IntField = int.MinValue }, target.Eval("new MyClassAdder() {StrProp = string.Empty, MyArr = new int[] {1, 2, 3, 4, 5}, IntField = int.MinValue }")); Assert.Throws(() => target.Eval("new MyClassAdder(){{ 1, 2, 3, 4, 5},{StrProp = \"6\" },7 }")); diff --git a/test/DynamicExpresso.UnitTest/ParametersTest.cs b/test/DynamicExpresso.UnitTest/ParametersTest.cs index a222c1a..cf2ca42 100644 --- a/test/DynamicExpresso.UnitTest/ParametersTest.cs +++ b/test/DynamicExpresso.UnitTest/ParametersTest.cs @@ -332,85 +332,6 @@ public void When_lambda_is_invoked_input_parameters_must_follow_in_the_same_orde Assert.AreEqual(4, lambda.Invoke(1, 5)); } - [Test] - public void When_lambda_is_invoked_byref_parameters_are_updated_on_invoke() - { - var a = 1; - var b = 2; - var c = 3; - var d = 4; - var e = 5; - - var parameters = new[]{ - new Parameter(nameof(a), typeof(int).MakeByRefType()), - new Parameter(nameof(b), typeof(int).MakeByRefType()), - new Parameter(nameof(c), typeof(int).MakeByRefType()), - new Parameter(nameof(d), typeof(int).MakeByRefType()), - new Parameter(nameof(e), typeof(int).MakeByRefType()), - }; - - var args = new object[] - { - a, - b, - c, - d, - e - }; - - a += b *= c -= d /= e %= 3; - - var target = new Interpreter(); - - var lambda = target.Parse("a = a + (b = b * (c = c - (d = d / (e = e % 3))))", parameters); - - Assert.AreEqual(a, lambda.Invoke(args)); - Assert.AreEqual(a, args[0]); - Assert.AreEqual(b, args[1]); - Assert.AreEqual(c, args[2]); - Assert.AreEqual(d, args[3]); - Assert.AreEqual(e, args[4]); - } - - [Test] - public void When_lambda_is_invoked_byref_parameters_are_updated_on_invoke_and_I_can_omit_parameters_not_used() - { - var target = new Interpreter(); - - var a = 1; - var b = 2; - var c = 3; - var d = 4; - var e = 5; - - var parameters = new[]{ - new Parameter(nameof(a), typeof(int).MakeByRefType()), - new Parameter(nameof(b), typeof(int).MakeByRefType()), - new Parameter(nameof(c), typeof(int).MakeByRefType()), - new Parameter(nameof(d), typeof(int).MakeByRefType()), - new Parameter(nameof(e), typeof(int).MakeByRefType()), - }; - - var args = new object[] - { - a, - b, - c, - d, - e - }; - - a += b *= d /= e %= 3; - - var lambda = target.Parse("a = a + (b = b * (d = d / (e = e % 3)))", parameters); - - Assert.AreEqual(a, lambda.Invoke(args)); - Assert.AreEqual(a, args[0]); - Assert.AreEqual(b, args[1]); - Assert.AreEqual(c, args[2]); - Assert.AreEqual(d, args[3]); - Assert.AreEqual(e, args[4]); - } [Test] public void When_lambda_is_invoked_I_can_omit_parameters_not_used()