|
@@ -21,25 +21,45 @@ namespace InABox.Core
|
|
|
|
|
|
public Action<object, object> Setter { get; set; }
|
|
|
|
|
|
+ public Func<object, object> Getter { get; set; }
|
|
|
+
|
|
|
public SubObject(Type objectType, Type propertyType, string name)
|
|
|
{
|
|
|
PropertyType = propertyType;
|
|
|
Name = name;
|
|
|
Setter = Expressions.Setter(objectType, name);
|
|
|
+ Getter = Expressions.Getter(objectType, name);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private static Dictionary<Type, List<SubObject>> _subObjects { get; } = new Dictionary<Type, List<SubObject>>();
|
|
|
|
|
|
- private static List<SubObject>? GetSubObjects(Type t)
|
|
|
+ private static List<SubObject>? GetSubObjectDefs(Type t)
|
|
|
{
|
|
|
CheckProperties(t);
|
|
|
return _subObjects.GetValueOrDefault(t);
|
|
|
}
|
|
|
|
|
|
+ public static IEnumerable<BaseObject> GetSubObjects(BaseObject obj)
|
|
|
+ {
|
|
|
+ var objs = GetSubObjectDefs(obj.GetType());
|
|
|
+ if(objs is null)
|
|
|
+ {
|
|
|
+ yield break;
|
|
|
+ }
|
|
|
+ foreach (var subObjectDef in objs)
|
|
|
+ {
|
|
|
+ var subObj = subObjectDef.Getter(obj);
|
|
|
+ if(subObj is BaseObject bObj)
|
|
|
+ {
|
|
|
+ yield return bObj;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public static void InitializeSubObjects(BaseObject obj)
|
|
|
{
|
|
|
- var objs = GetSubObjects(obj.GetType());
|
|
|
+ var objs = GetSubObjectDefs(obj.GetType());
|
|
|
if(objs is null)
|
|
|
{
|
|
|
return;
|
|
@@ -252,7 +272,7 @@ namespace InABox.Core
|
|
|
{
|
|
|
var props = _properties.GetValueOrDefault(entityName);
|
|
|
var hasprops = props?.Any(x => x.Value is StandardProperty) == true;
|
|
|
- if (type.IsSubclassOf(typeof(BaseObject)) && !hasprops)
|
|
|
+ if (!hasprops && type.IsSubclassOf(typeof(BaseObject)))
|
|
|
RegisterProperties(type);
|
|
|
}
|
|
|
catch (Exception e)
|
|
@@ -287,9 +307,9 @@ namespace InABox.Core
|
|
|
|
|
|
public static void InitializeObject<TObject>(TObject entity) where TObject : BaseObject
|
|
|
{
|
|
|
- entity.UserProperties.Clear();
|
|
|
- var props = Properties(entity.GetType()).Where(x => x is CustomProperty).ToArray();
|
|
|
- foreach (var field in props) entity.UserProperties[field.Name] = DefaultValue(field.PropertyType);
|
|
|
+ entity.UserProperties.Load(Properties(entity.GetType())
|
|
|
+ .Where(x => x is CustomProperty)
|
|
|
+ .Select(x => new KeyValuePair<string, object?>(x.Name, x.PropertyType)));
|
|
|
}
|
|
|
}
|
|
|
}
|