feat: entity register for serialized entity references
This commit is contained in:
@@ -104,17 +104,54 @@ public class SerializedClass
|
||||
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);
|
||||
AssignVariable(key, type, instance, value, PRIVATE_BINDING_FLAGS);
|
||||
|
||||
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);
|
||||
AssignVariable(key, type, instance, value, PUBLIC_BINDING_FLAGS);
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
public object CreateInstance(EntityRegistry? entityRegistry)
|
||||
{
|
||||
if (entityRegistry is null)
|
||||
return CreateInstance();
|
||||
|
||||
Type type = TypeFactory.GetType(Type);
|
||||
object instance = TypeFactory.Get(type);
|
||||
|
||||
foreach ((string key, object? value) in Private)
|
||||
AssignVariable(key, type, instance, value, PRIVATE_BINDING_FLAGS, entityRegistry);
|
||||
|
||||
foreach ((string key, object? value) in Public)
|
||||
AssignVariable(key, type, instance, value, PUBLIC_BINDING_FLAGS, entityRegistry);
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
private static void AssignVariable(string key, Type type, object instance, object? value, BindingFlags bindingFlags, EntityRegistry entityRegistry)
|
||||
{
|
||||
if (type.GetField(key, bindingFlags) is FieldInfo fieldInfo)
|
||||
{
|
||||
if (typeof(IEntity).IsAssignableFrom(fieldInfo.FieldType))
|
||||
entityRegistry.QueueAssign(value?.ToString() ?? "", (entity) => fieldInfo.SetValue(instance, entity));
|
||||
else
|
||||
fieldInfo.SetValue(instance, value);
|
||||
}
|
||||
else if (type.GetProperty(key, bindingFlags) is PropertyInfo propertyInfo)
|
||||
{
|
||||
if (typeof(IEntity).IsAssignableFrom(propertyInfo.PropertyType))
|
||||
entityRegistry.QueueAssign(value?.ToString() ?? "", (entity) => propertyInfo.SetValue(instance, entity));
|
||||
else
|
||||
propertyInfo.SetValue(instance, value);
|
||||
}
|
||||
}
|
||||
|
||||
private static void AssignVariable(string key, Type type, object instance, object? value, BindingFlags bindingFlags)
|
||||
{
|
||||
if (type.GetField(key, bindingFlags) is FieldInfo fieldInfo)
|
||||
fieldInfo.SetValue(instance, value);
|
||||
else if (type.GetProperty(key, bindingFlags) is PropertyInfo propertyInfo)
|
||||
propertyInfo.SetValue(instance, value);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user