From d98b402b10283530d833644706e1d0c960798c24 Mon Sep 17 00:00:00 2001 From: Zonciu Liang Date: Sat, 8 May 2021 01:47:41 +0800 Subject: [PATCH] fix(module: table): exception is thrown when the DataIndex ends with the get_Item method. (#1485) --- .../table/Internal/ColumnExpressionHelper.cs | 31 +++++++++---- .../table/ColumnExpressionHelperTest.cs | 46 +++++++++++++++++++ 2 files changed, 68 insertions(+), 9 deletions(-) create mode 100644 tests/AntDesign.Tests/table/ColumnExpressionHelperTest.cs diff --git a/components/table/Internal/ColumnExpressionHelper.cs b/components/table/Internal/ColumnExpressionHelper.cs index 0ddfa5bd..8c88d9b9 100644 --- a/components/table/Internal/ColumnExpressionHelper.cs +++ b/components/table/Internal/ColumnExpressionHelper.cs @@ -13,18 +13,31 @@ namespace AntDesign.Internal internal static MemberInfo GetReturnMemberInfo(LambdaExpression expression) { var accessorBody = expression.Body; - - if (accessorBody is ConditionalExpression conditionalExpression) + while (true) { - accessorBody = conditionalExpression.IfTrue; + if (accessorBody is UnaryExpression unaryExpression) + { + accessorBody = unaryExpression.Operand; + } + else if (accessorBody is ConditionalExpression conditionalExpression) + { + accessorBody = conditionalExpression.IfTrue; + } + else if (accessorBody is MethodCallExpression methodCallExpression) + { + accessorBody = methodCallExpression.Object; + } + else if (accessorBody is BinaryExpression binaryExpression) + { + accessorBody = binaryExpression.Left; + } + else + { + break; + } } - if (accessorBody is UnaryExpression unaryExpression && unaryExpression.NodeType == ExpressionType.Convert) - { - accessorBody = unaryExpression.Operand; - } - - if (!(accessorBody is MemberExpression memberExpression)) + if (accessorBody is not MemberExpression memberExpression) { throw new ArgumentException($"The type of the provided expression {accessorBody.GetType().Name} is not supported, it should be {nameof(MemberExpression)}."); } diff --git a/tests/AntDesign.Tests/table/ColumnExpressionHelperTest.cs b/tests/AntDesign.Tests/table/ColumnExpressionHelperTest.cs new file mode 100644 index 00000000..ac3addb9 --- /dev/null +++ b/tests/AntDesign.Tests/table/ColumnExpressionHelperTest.cs @@ -0,0 +1,46 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using AntDesign.core.Helpers; +using AntDesign.Internal; +using Xunit; + +namespace AntDesign.Tests.Table +{ + public class ColumnExpressionHelperTest + { + [Fact] + public void EndsWithGetItem() + { + var t1 = new T1() + { + T2 = new() + { + Arr = new() + { + 5, + 6, + 7 + } + } + }; + + var exp = typeof(T1).BuildAccessPropertyLambdaExpression("T2.Arr[2]"); + + var memberInfo = ColumnExpressionHelper.GetReturnMemberInfo(exp); + Assert.Equal(nameof(T2.Arr), memberInfo.Name); + } + + public class T1 + { + public T2 T2 { get; set; } + } + + public class T2 + { + public List Arr { get; set; } + } + } +}