Skip to content

Commit

Permalink
handle paths and use caching when getting data type
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward Miller committed Jan 1, 2024
1 parent d9dd3c4 commit d9c50cc
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 11 deletions.
32 changes: 25 additions & 7 deletions Maui.DataGrid/DataGridColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,20 +363,38 @@ public bool IsSortable()

internal void InitializeDataType()
{
if (DataType != null || string.IsNullOrEmpty(PropertyName))
{
return;
}

ArgumentNullException.ThrowIfNull(DataGrid);

if (DataType == null && PropertyName != null)
try
{
try
Type? rowDataType = null;

var genericArguments = DataGrid.ItemsSource.GetType().GetGenericArguments();

if (genericArguments.Length == 1)
{
var rowDataType = DataGrid.ItemsSource.GetType().GetGenericArguments().Single();
DataType = rowDataType.GetProperty(PropertyName)?.PropertyType;
rowDataType = genericArguments[0];
}
catch (Exception ex)
when (ex is NotSupportedException or ArgumentNullException or InvalidOperationException)
else
{
Debug.WriteLine($"Initializing the data type for the column '{Title}' resulted in the following error: {ex.Message}");
var firstItem = DataGrid.ItemsSource.OfType<object>().FirstOrDefault(i => i != null);
if (firstItem != default)
{
rowDataType = firstItem.GetType();
}
}

DataType = rowDataType?.GetPropertyTypeByPath(PropertyName);
}
catch (Exception ex)
when (ex is NotSupportedException or ArgumentNullException or InvalidCastException)
{
Debug.WriteLine($"Attempting to obtain the data type for the column '{Title}' resulted in the following error: {ex.Message}");
}
}

Expand Down
53 changes: 49 additions & 4 deletions Maui.DataGrid/Extensions/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,63 @@ internal static class ReflectionExtensions
return result;
}

public static Type? GetPropertyTypeByPath(this Type type, string path)
{
if (type == null || string.IsNullOrWhiteSpace(path))
{
return null;
}

Type? resultType;

if (path.Contains(PropertyOfOp, StringComparison.Ordinal))
{
var tokens = path.Split(PropertyOfOp);

resultType = type;

foreach (var token in tokens)
{
resultType = resultType.GetPropertyType(token);

if (resultType == null)
{
break;
}
}
}
else
{
resultType = type.GetPropertyType(path);
}

return resultType;
}

private static Type? GetPropertyType(this Type type, string propertyName)
{
var propertyDescriptor = GetPropertyDescriptor(type, propertyName);

return propertyDescriptor?.PropertyType;
}

private static object? GetPropertyValue(object obj, string propertyName)
{
var type = obj.GetType();

var propertyDescriptor = GetPropertyDescriptor(type, propertyName);

return propertyDescriptor?.GetValue(obj);
}

private static PropertyDescriptor? GetPropertyDescriptor(Type type, string propertyName)
{
if (!PropertyTypeCache.TryGetValue(type, out var properties))
{
properties = TypeDescriptor.GetProperties(obj);
properties = TypeDescriptor.GetProperties(type);
PropertyTypeCache[type] = properties;
}

var propertyDescriptor = properties.Find(propertyName, false);

return propertyDescriptor?.GetValue(obj);
return properties.Find(propertyName, false);
}
}

0 comments on commit d9c50cc

Please sign in to comment.