feat: serialize all attribute
This commit is contained in:
parent
c3876add1e
commit
c704173183
4
Engine.Serialization/SerializeAllAttribute.cs
Normal file
4
Engine.Serialization/SerializeAllAttribute.cs
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
namespace Syntriax.Engine.Serialization;
|
||||||
|
|
||||||
|
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
|
||||||
|
public class SerializeAllAttribute : Attribute;
|
102
Engine.Serialization/SerializedClass.cs
Normal file
102
Engine.Serialization/SerializedClass.cs
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
using Syntriax.Engine.Core.Factory;
|
||||||
|
|
||||||
|
namespace Syntriax.Engine.Serialization;
|
||||||
|
|
||||||
|
public class SerializedClass
|
||||||
|
{
|
||||||
|
private const BindingFlags PRIVATE_BINDING_FLAGS = BindingFlags.Instance | BindingFlags.NonPublic;
|
||||||
|
private const BindingFlags PUBLIC_BINDING_FLAGS = BindingFlags.Instance | BindingFlags.Public;
|
||||||
|
|
||||||
|
public string Type { get; set; } = string.Empty;
|
||||||
|
public Dictionary<string, object?> Public { get; set; } = [];
|
||||||
|
public Dictionary<string, object?> Private { get; set; } = [];
|
||||||
|
|
||||||
|
public SerializedClass() { }
|
||||||
|
public SerializedClass(object @class)
|
||||||
|
{
|
||||||
|
UpdateClass(@class);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void UpdateClass(object @class)
|
||||||
|
{
|
||||||
|
Type type = @class.GetType();
|
||||||
|
Type = type.FullName ?? type.Name;
|
||||||
|
|
||||||
|
bool isFullySerializable = type.HasAttribute<SerializeAllAttribute>();
|
||||||
|
|
||||||
|
Public.Clear();
|
||||||
|
Private.Clear();
|
||||||
|
|
||||||
|
foreach (PropertyInfo privatePropertyInfo in Utils.GetPropertyInfosIncludingBaseClasses(type, PRIVATE_BINDING_FLAGS))
|
||||||
|
{
|
||||||
|
if (privatePropertyInfo.HasAttribute<IgnoreSerializationAttribute>())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (privatePropertyInfo.SetMethod is null)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (!isFullySerializable && !privatePropertyInfo.HasAttribute<SerializeAttribute>())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
Private.Add(privatePropertyInfo.Name, privatePropertyInfo.GetValue(@class));
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (PropertyInfo publicPropertyInfo in Utils.GetPropertyInfosIncludingBaseClasses(type, PUBLIC_BINDING_FLAGS))
|
||||||
|
{
|
||||||
|
if (publicPropertyInfo.HasAttribute<IgnoreSerializationAttribute>())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (publicPropertyInfo.SetMethod is null)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (!isFullySerializable && !publicPropertyInfo.HasAttribute<SerializeAttribute>())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
Public.Add(publicPropertyInfo.Name, publicPropertyInfo.GetValue(@class));
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (FieldInfo privateFieldInfo in Utils.GetFieldInfosIncludingBaseClasses(type, PRIVATE_BINDING_FLAGS))
|
||||||
|
{
|
||||||
|
if (privateFieldInfo.HasAttribute<System.Runtime.CompilerServices.CompilerGeneratedAttribute>())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (!isFullySerializable && !privateFieldInfo.HasAttribute<SerializeAttribute>())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
Private.Add(privateFieldInfo.Name, privateFieldInfo.GetValue(@class));
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (FieldInfo publicFieldInfo in Utils.GetFieldInfosIncludingBaseClasses(type, PUBLIC_BINDING_FLAGS))
|
||||||
|
{
|
||||||
|
if (publicFieldInfo.HasAttribute<System.Runtime.CompilerServices.CompilerGeneratedAttribute>())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (!isFullySerializable && !publicFieldInfo.HasAttribute<SerializeAttribute>())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
Public.Add(publicFieldInfo.Name, publicFieldInfo.GetValue(@class));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public object CreateInstance()
|
||||||
|
{
|
||||||
|
Type type = TypeFactory.GetType(Type);
|
||||||
|
object instance = TypeFactory.Get(type);
|
||||||
|
|
||||||
|
foreach ((string key, object? value) in Private)
|
||||||
|
if (type.GetField(key, PRIVATE_BINDING_FLAGS) is FieldInfo fieldInfo)
|
||||||
|
fieldInfo.SetValue(instance, value);
|
||||||
|
else if (type.GetProperty(key, PRIVATE_BINDING_FLAGS) is PropertyInfo propertyInfo)
|
||||||
|
propertyInfo.SetValue(instance, value);
|
||||||
|
|
||||||
|
foreach ((string key, object? value) in Public)
|
||||||
|
if (type.GetField(key, PUBLIC_BINDING_FLAGS) is FieldInfo fieldInfo)
|
||||||
|
fieldInfo.SetValue(instance, value);
|
||||||
|
else if (type.GetProperty(key, PUBLIC_BINDING_FLAGS) is PropertyInfo propertyInfo)
|
||||||
|
propertyInfo.SetValue(instance, value);
|
||||||
|
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
}
|
@ -1,59 +0,0 @@
|
|||||||
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