chore: added serialized entity class
This commit is contained in:
parent
35a75d993b
commit
c3876add1e
59
Engine.Serialization/SerializedEntity.cs
Normal file
59
Engine.Serialization/SerializedEntity.cs
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
using System.Reflection;
|
||||||
|
using Syntriax.Engine.Core;
|
||||||
|
|
||||||
|
namespace Syntriax.Engine.Serialization;
|
||||||
|
|
||||||
|
public class SerializedEntity
|
||||||
|
{
|
||||||
|
public string Id { get; set; } = string.Empty;
|
||||||
|
public string Type { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
public Dictionary<string, object?> Public = [];
|
||||||
|
public Dictionary<string, object?> Private = [];
|
||||||
|
|
||||||
|
public SerializedEntity() { }
|
||||||
|
public SerializedEntity(object entity)
|
||||||
|
{
|
||||||
|
Type type = entity.GetType();
|
||||||
|
Type = type.FullName ?? throw new($"Object {entity} has no {nameof(System.Type.FullName)}");
|
||||||
|
Id = type.GetProperty(nameof(IEntity.Id), BindingFlags.Instance | BindingFlags.Public)?.GetValue(entity)?.ToString() ?? string.Empty;
|
||||||
|
|
||||||
|
foreach (PropertyInfo privatePropertyInfo in Utils.GetPropertyInfosIncludingBaseClasses(type, BindingFlags.Instance | BindingFlags.NonPublic))
|
||||||
|
{
|
||||||
|
if (privatePropertyInfo.HasAttribute<IgnoreSerializationAttribute>())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
Private.Add(privatePropertyInfo.Name, privatePropertyInfo.GetValue(entity));
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (PropertyInfo publicPropertyInfo in Utils.GetPropertyInfosIncludingBaseClasses(type, BindingFlags.Instance | BindingFlags.Public))
|
||||||
|
{
|
||||||
|
if (publicPropertyInfo.HasAttribute<IgnoreSerializationAttribute>())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
Public.Add(publicPropertyInfo.Name, publicPropertyInfo.GetValue(entity));
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (FieldInfo privateFieldInfo in Utils.GetFieldInfosIncludingBaseClasses(type, BindingFlags.Instance | BindingFlags.NonPublic))
|
||||||
|
{
|
||||||
|
if (privateFieldInfo.HasAttribute<System.Runtime.CompilerServices.CompilerGeneratedAttribute>())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// if (!privateFieldInfo.HasAttribute<SerializeAttribute>())
|
||||||
|
// continue;
|
||||||
|
|
||||||
|
Private.Add(privateFieldInfo.Name, privateFieldInfo.GetValue(entity));
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (FieldInfo publicFieldInfo in Utils.GetFieldInfosIncludingBaseClasses(type, BindingFlags.Instance | BindingFlags.Public))
|
||||||
|
{
|
||||||
|
if (publicFieldInfo.HasAttribute<System.Runtime.CompilerServices.CompilerGeneratedAttribute>())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// if (!publicFieldInfo.HasAttribute<SerializeAttribute>())
|
||||||
|
// continue;
|
||||||
|
|
||||||
|
Public.Add(publicFieldInfo.Name, publicFieldInfo.GetValue(entity));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user