From 4f0a969559fdc253972afb43f6b1cf38f591e24a Mon Sep 17 00:00:00 2001 From: Abdella Solomon Date: Tue, 18 Jun 2024 18:06:06 +0300 Subject: [PATCH 1/2] Enhance SplitPropertyPath to conditionally handle parentheses in property paths --- .../Utils/ReflectionHelper.cs | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/Avalonia.Controls.DataGrid/Utils/ReflectionHelper.cs b/src/Avalonia.Controls.DataGrid/Utils/ReflectionHelper.cs index 38e18b57dee..a33ae0e80b8 100644 --- a/src/Avalonia.Controls.DataGrid/Utils/ReflectionHelper.cs +++ b/src/Avalonia.Controls.DataGrid/Utils/ReflectionHelper.cs @@ -19,6 +19,8 @@ internal static class TypeHelper internal const char LeftIndexerToken = '['; internal const char PropertyNameSeparator = '.'; internal const char RightIndexerToken = ']'; + internal const char LeftParenthesisToken = '('; + internal const char RightParenthesisToken = ')'; private static Type FindGenericType(Type definition, Type type) { @@ -482,12 +484,35 @@ internal static List SplitPropertyPath(string propertyPath) List propertyPaths = new List(); if (!string.IsNullOrEmpty(propertyPath)) { + bool parenthesisOn = false; int startIndex = 0; for (int index = 0; index < propertyPath.Length; index++) { - if (propertyPath[index] == PropertyNameSeparator) + if (parenthesisOn) { - propertyPaths.Add(propertyPath.Substring(startIndex, index - startIndex)); + if (propertyPath[index] == RightParenthesisToken) + { + parenthesisOn = false; + startIndex = index + 1; + } + continue; + } + + if (propertyPath[index] == LeftParenthesisToken) + { + parenthesisOn = true; + if (startIndex != index) + { + propertyPaths.Add(propertyPath.Substring(startIndex, index - startIndex)); + startIndex = index + 1; + } + } + else if (propertyPath[index] == PropertyNameSeparator) + { + if (startIndex != index) + { + propertyPaths.Add(propertyPath.Substring(startIndex, index - startIndex)); + } startIndex = index + 1; } else if (startIndex != index && propertyPath[index] == LeftIndexerToken) From 19115b9fec862d8d43140dd16aa8db731ce3a379 Mon Sep 17 00:00:00 2001 From: Abdella Solomon Date: Thu, 11 Jul 2024 11:06:07 +0300 Subject: [PATCH 2/2] added test for SplitPropertyPath --- .../Utils/ReflectionHelperTests.cs | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 tests/Avalonia.Controls.DataGrid.UnitTests/Utils/ReflectionHelperTests.cs diff --git a/tests/Avalonia.Controls.DataGrid.UnitTests/Utils/ReflectionHelperTests.cs b/tests/Avalonia.Controls.DataGrid.UnitTests/Utils/ReflectionHelperTests.cs new file mode 100644 index 00000000000..22471f61fef --- /dev/null +++ b/tests/Avalonia.Controls.DataGrid.UnitTests/Utils/ReflectionHelperTests.cs @@ -0,0 +1,20 @@ + +using Avalonia.Controls.Utils; +using Xunit; + +namespace Avalonia.Controls.DataGrid.UnitTests.Utils +{ + public class ReflectionHelperTests + { + [Fact] + public void SplitPropertyPath_Splits_PropertyPath_With_Cast() + { + var path = "(Type).Property"; + var expected = new [] { "Property" }; + + var result = TypeHelper.SplitPropertyPath(path); + + Assert.Equal(expected, result); + } + } +}