32 lines
1.8 KiB
C#
32 lines
1.8 KiB
C#
using System.Reflection;
|
|
|
|
namespace Syntriax.Engine.Serialization;
|
|
|
|
internal static class Utils
|
|
{
|
|
internal static TypeData GetTypeData(this Type type)
|
|
{
|
|
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))
|
|
.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)
|
|
.Where(pi => pi.SetMethod is not null)
|
|
.OrderBy(ei => ei.Name)// ei => ei.PropertyType.IsPrimitive || ei.PropertyType == typeof(string))
|
|
// .ThenByDescending(ei => ei.Name)
|
|
.AsEnumerable();
|
|
|
|
return new TypeData(eventInfos, fieldInfos, propertyInfos);
|
|
}
|
|
}
|
|
|
|
internal record struct TypeData(IEnumerable<EventInfo> Events, 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);
|
|
}
|