fix(module: table): exception is thrown when the DataIndex ends with the get_Item method. (#1485)

This commit is contained in:
Zonciu Liang 2021-05-08 01:47:41 +08:00 committed by GitHub
parent a7fabae919
commit d98b402b10
2 changed files with 68 additions and 9 deletions

View File

@ -13,18 +13,31 @@ namespace AntDesign.Internal
internal static MemberInfo GetReturnMemberInfo(LambdaExpression expression) internal static MemberInfo GetReturnMemberInfo(LambdaExpression expression)
{ {
var accessorBody = expression.Body; var accessorBody = expression.Body;
while (true)
if (accessorBody is ConditionalExpression conditionalExpression)
{ {
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) if (accessorBody is not MemberExpression memberExpression)
{
accessorBody = unaryExpression.Operand;
}
if (!(accessorBody is MemberExpression memberExpression))
{ {
throw new ArgumentException($"The type of the provided expression {accessorBody.GetType().Name} is not supported, it should be {nameof(MemberExpression)}."); throw new ArgumentException($"The type of the provided expression {accessorBody.GetType().Name} is not supported, it should be {nameof(MemberExpression)}.");
} }

View File

@ -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<int> Arr { get; set; }
}
}
}