Skip to content

Commit db8daa1

Browse files
authored
use complied expression to get property value for soting
1 parent ce28b5f commit db8daa1

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

src/ItemsSource/SortDescription.cs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using WinUI.TableView.Extensions;
45

56
namespace WinUI.TableView;
67

@@ -9,6 +10,8 @@ namespace WinUI.TableView;
910
/// </summary>
1011
public class SortDescription
1112
{
13+
private Func<object, object?>? _funcCompiled;
14+
1215
/// <summary>
1316
/// Initializes a new instance of the <see cref="SortDescription"/> class that describes
1417
/// a sort on the object itself
@@ -35,21 +38,15 @@ public SortDescription(string? propertyName,
3538
/// <returns>The value of the property.</returns>
3639
public virtual object? GetPropertyValue(object? item)
3740
{
38-
if (ValueDelegate is not null)
39-
{
40-
return ValueDelegate(item);
41-
}
42-
else if (PropertyName is not null)
43-
{
44-
return item?.GetType()
45-
.GetProperty(PropertyName)?
46-
.GetValue(item);
47-
}
48-
else
49-
{
50-
return default!;
51-
}
52-
}
41+
if (item == null) return null;
42+
43+
if (ValueDelegate is not null) return ValueDelegate(item);
44+
45+
if (_funcCompiled is null && !string.IsNullOrWhiteSpace(PropertyName))
46+
_funcCompiled = item.GetFuncCompiledPropertyPath(PropertyName!);
47+
48+
return _funcCompiled?.Invoke(item);
49+
}
5350

5451
/// <summary>
5552
/// Compares two objects based on the sort description.

0 commit comments

Comments
 (0)