Skip to content

Commit 417ae32

Browse files
committed
elevate values from array binding
1 parent 73bdb2f commit 417ae32

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

src/Columns/TableViewBoundColumn.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@ public abstract class TableViewBoundColumn : TableViewColumn
1414
private Type? _listType;
1515
private string? _propertyPath;
1616
private Binding _binding = new();
17-
private (PropertyInfo, object?)[]? _propertyInfo;
17+
private (MemberInfo, object?)[]? _memberInfo;
1818

1919
public override object? GetCellContent(object? dataItem)
2020
{
2121
if (dataItem is null) return null;
2222

23-
if (_propertyInfo is null || dataItem.GetType() != _listType)
23+
if (_memberInfo is null || dataItem.GetType() != _listType)
2424
{
2525
_listType = dataItem.GetType();
26-
dataItem = dataItem.GetValue(_listType, PropertyPath, out _propertyInfo);
26+
dataItem = dataItem.GetValue(_listType, PropertyPath, out _memberInfo);
2727
}
2828
else
2929
{
30-
dataItem = dataItem.GetValue(_propertyInfo);
30+
dataItem = dataItem.GetValue(_memberInfo);
3131
}
3232

3333
if (Binding?.Converter is not null)

src/Extensions/ObjectExtensions.cs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,21 @@ internal static class ObjectExtensions
1212
/// Gets the value of a property from an object using a sequence of property info and index pairs.
1313
/// </summary>
1414
/// <param name="obj">The object from which to get the value.</param>
15-
/// <param name="pis">An array of property info and index pairs.</param>
15+
/// <param name="members">An array of member info and index pairs.</param>
1616
/// <returns>The value of the property, or null if the object is null.</returns>
17-
internal static object? GetValue(this object? obj, (PropertyInfo pi, object? index)[] pis)
17+
internal static object? GetValue(this object? obj, (MemberInfo info, object? index)[] members)
1818
{
19-
foreach (var pi in pis)
19+
foreach (var (info, index) in members)
2020
{
2121
if (obj is null)
2222
{
2323
break;
2424
}
2525

26-
obj = pi.index is not null ? pi.pi.GetValue(obj, [pi.index]) : pi.pi.GetValue(obj);
26+
if (info is PropertyInfo pi)
27+
obj = index is not null ? pi.GetValue(obj, [index]) : pi.GetValue(obj);
28+
else if (info is MethodInfo mi)
29+
obj = index is not null ? mi.Invoke(obj, [index]) : mi.Invoke(obj, []);
2730
}
2831

2932
return obj;
@@ -35,19 +38,19 @@ internal static class ObjectExtensions
3538
/// <param name="obj">The object from which to get the value.</param>
3639
/// <param name="type">The type of the object.</param>
3740
/// <param name="path">The property path.</param>
38-
/// <param name="pis">An array of property info and index pairs.</param>
41+
/// <param name="members">An array of member info and index pairs.</param>
3942
/// <returns>The value of the property, or null if the object is null.</returns>
40-
internal static object? GetValue(this object? obj, Type? type, string? path, out (PropertyInfo pi, object? index)[] pis)
43+
internal static object? GetValue(this object? obj, Type? type, string? path, out (MemberInfo info, object? index)[] members)
4144
{
4245
var parts = path?.Split('.');
4346

4447
if (parts is null)
4548
{
46-
pis = [];
49+
members = [];
4750
return obj;
4851
}
4952

50-
pis = new (PropertyInfo, object?)[parts.Length];
53+
members = new (MemberInfo, object?)[parts.Length];
5154

5255
for (var i = 0; i < parts.Length; i++)
5356
{
@@ -59,16 +62,22 @@ internal static class ObjectExtensions
5962
part = "Item";
6063
}
6164

62-
var pi = type?.GetProperty(part);
63-
if (pi is not null)
65+
if (type?.GetProperty(part) is { } pi)
6466
{
65-
pis[i] = (pi, index);
67+
members[i] = (pi, index);
6668
obj = index is not null ? pi?.GetValue(obj, [index]) : pi?.GetValue(obj);
6769
type = obj?.GetType();
6870
}
71+
else if (type?.IsArray is true && type.GetMethod("GetValue", [typeof(int)]) is { } mi)
72+
{
73+
members[i] = (mi, index);
74+
obj = index is not null ? mi?.Invoke(obj, [index]) : mi?.Invoke(obj, []);
75+
type = obj?.GetType();
76+
77+
}
6978
else
7079
{
71-
pis = null!;
80+
members = null!;
7281
return null;
7382
}
7483
}

0 commit comments

Comments
 (0)