fix: GetTypeData not including base class proprety & fields
This commit is contained in:
parent
f51d5f342e
commit
35f6c3850e
@ -8,30 +8,67 @@ internal static class Utils
|
|||||||
|
|
||||||
internal static TypeData GetTypeData(this Type objectType)
|
internal static TypeData GetTypeData(this Type objectType)
|
||||||
{
|
{
|
||||||
// IEnumerable<EventInfo> eventInfos = objectType.GetEvents(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
|
List<FieldInfo> fieldInfos = GetFieldInfosIncludingBaseClasses(objectType, BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Public);
|
||||||
// .OrderBy(ei => ei.Name)
|
|
||||||
// .ToList();
|
|
||||||
List<FieldInfo> fieldInfos = objectType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
|
|
||||||
.OrderBy(ei => ei.Name) //ei => ei.FieldType.IsPrimitive || ei.FieldType == typeof(string))
|
|
||||||
// .ThenByDescending(ei => ei.Name)
|
|
||||||
.ToList();
|
|
||||||
|
|
||||||
List<PropertyInfo> propertyInfos = objectType.GetProperties(BindingFlags.Instance | BindingFlags.Public)
|
List<PropertyInfo> propertyInfos = GetPropertyInfosIncludingBaseClasses(objectType, BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.Public)
|
||||||
.Where(pi => pi.SetMethod is not null)
|
.Where(pi => pi.SetMethod is not null)
|
||||||
.OrderBy(ei => ei.Name)// ei => ei.PropertyType.IsPrimitive || ei.PropertyType == typeof(string))
|
|
||||||
// .ThenByDescending(ei => ei.Name)
|
|
||||||
.ToList();
|
.ToList();
|
||||||
|
|
||||||
propertyInfos.AddRange(
|
propertyInfos.AddRange(
|
||||||
objectType.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic)
|
GetPropertyInfosIncludingBaseClasses(objectType, BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.NonPublic)
|
||||||
.Where(pi => pi.SetMethod is not null && pi.GetCustomAttribute<SerializeAttribute>() is not null)
|
.Where(pi => pi.SetMethod is not null && pi.GetCustomAttribute<SerializeAttribute>() is not null)
|
||||||
.OrderBy(ei => ei.Name)// ei => ei.PropertyType.IsPrimitive || ei.PropertyType == typeof(string))
|
|
||||||
// .ThenByDescending(ei => ei.Name)
|
|
||||||
.ToList()
|
|
||||||
);
|
);
|
||||||
|
|
||||||
return new TypeData(fieldInfos, propertyInfos);
|
return new TypeData(fieldInfos, propertyInfos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal static List<FieldInfo> GetFieldInfosIncludingBaseClasses(Type type, BindingFlags bindingFlags)
|
||||||
|
{
|
||||||
|
if (type.BaseType is null)
|
||||||
|
return [.. type.GetFields(bindingFlags)];
|
||||||
|
|
||||||
|
Type currentType = type;
|
||||||
|
FieldInfoComparer fieldComparer = new();
|
||||||
|
HashSet<FieldInfo> fieldInfoList = new(type.GetFields(bindingFlags), fieldComparer);
|
||||||
|
|
||||||
|
while (currentType.BaseType is Type baseType)
|
||||||
|
{
|
||||||
|
currentType = baseType;
|
||||||
|
fieldInfoList.UnionWith(currentType!.GetFields(bindingFlags));
|
||||||
|
}
|
||||||
|
|
||||||
|
return [.. fieldInfoList.OrderBy(fi => fi.Name)];
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static List<PropertyInfo> GetPropertyInfosIncludingBaseClasses(Type type, BindingFlags bindingFlags)
|
||||||
|
{
|
||||||
|
if (type.BaseType is null)
|
||||||
|
return [.. type.GetProperties(bindingFlags)];
|
||||||
|
|
||||||
|
Type currentType = type;
|
||||||
|
PropertyInfoComparer propertyComparer = new();
|
||||||
|
HashSet<PropertyInfo> propertyInfoList = new(type.GetProperties(bindingFlags), propertyComparer);
|
||||||
|
|
||||||
|
while (currentType.BaseType is Type baseType)
|
||||||
|
{
|
||||||
|
currentType = baseType;
|
||||||
|
propertyInfoList.UnionWith(currentType.GetProperties(bindingFlags));
|
||||||
|
}
|
||||||
|
|
||||||
|
return [.. propertyInfoList.OrderBy(pi => pi.Name)];
|
||||||
|
}
|
||||||
|
|
||||||
|
private class FieldInfoComparer : IEqualityComparer<FieldInfo>
|
||||||
|
{
|
||||||
|
public bool Equals(FieldInfo? x, FieldInfo? y) => x?.DeclaringType == y?.DeclaringType && x?.Name == y?.Name;
|
||||||
|
public int GetHashCode(FieldInfo obj) => obj.Name.GetHashCode() ^ obj.DeclaringType!.GetHashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
private class PropertyInfoComparer : IEqualityComparer<PropertyInfo>
|
||||||
|
{
|
||||||
|
public bool Equals(PropertyInfo? x, PropertyInfo? y) => x?.DeclaringType == y?.DeclaringType && x?.Name == y?.Name;
|
||||||
|
public int GetHashCode(PropertyInfo obj) => obj.Name.GetHashCode() ^ obj.DeclaringType!.GetHashCode();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal record struct TypeData(IEnumerable<FieldInfo> Fields, IEnumerable<PropertyInfo> Properties)
|
internal record struct TypeData(IEnumerable<FieldInfo> Fields, IEnumerable<PropertyInfo> Properties)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user