chore: added a generic converter
This commit is contained in:
@@ -4,28 +4,38 @@ namespace Syntriax.Engine.Serialization;
|
||||
|
||||
internal static class Utils
|
||||
{
|
||||
internal static TypeData GetTypeData(this Type type)
|
||||
internal static bool IsEnumerable(this Type type) => typeof(System.Collections.IEnumerable).IsAssignableFrom(type) && type != typeof(string);
|
||||
|
||||
internal static TypeData GetTypeData(this Type objectType)
|
||||
{
|
||||
IEnumerable<EventInfo> eventInfos = type.GetEvents(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
|
||||
.OrderBy(ei => ei.Name)
|
||||
.AsEnumerable();
|
||||
IEnumerable<FieldInfo> fieldInfos = type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
|
||||
.Where(fi => !eventInfos.Any(ei => fi.Name.CompareTo(ei.Name) == 0))
|
||||
// IEnumerable<EventInfo> eventInfos = objectType.GetEvents(BindingFlags.Instance | 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)
|
||||
.AsEnumerable();
|
||||
IEnumerable<PropertyInfo> propertyInfos = type.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
|
||||
.ToList();
|
||||
|
||||
List<PropertyInfo> propertyInfos = objectType.GetProperties(BindingFlags.Instance | BindingFlags.Public)
|
||||
.Where(pi => pi.SetMethod is not null)
|
||||
.OrderBy(ei => ei.Name)// ei => ei.PropertyType.IsPrimitive || ei.PropertyType == typeof(string))
|
||||
// .ThenByDescending(ei => ei.Name)
|
||||
.AsEnumerable();
|
||||
.ToList();
|
||||
|
||||
return new TypeData(eventInfos, fieldInfos, propertyInfos);
|
||||
propertyInfos.AddRange(
|
||||
objectType.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic)
|
||||
.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);
|
||||
}
|
||||
}
|
||||
|
||||
internal record struct TypeData(IEnumerable<EventInfo> Events, IEnumerable<FieldInfo> Fields, IEnumerable<PropertyInfo> Properties)
|
||||
internal record struct TypeData(IEnumerable<FieldInfo> Fields, IEnumerable<PropertyInfo> Properties)
|
||||
{
|
||||
public static implicit operator (IEnumerable<EventInfo> events, IEnumerable<FieldInfo> fields, IEnumerable<PropertyInfo> properties)(TypeData value) => (value.Events, value.Fields, value.Properties);
|
||||
public static implicit operator TypeData((IEnumerable<EventInfo> events, IEnumerable<FieldInfo> fields, IEnumerable<PropertyInfo> properties) value) => new TypeData(value.events, value.fields, value.properties);
|
||||
public static implicit operator (IEnumerable<FieldInfo> fields, IEnumerable<PropertyInfo> properties)(TypeData value) => (value.Fields, value.Properties);
|
||||
public static implicit operator TypeData((IEnumerable<FieldInfo> fields, IEnumerable<PropertyInfo> properties) value) => new(value.fields, value.properties);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user