67 lines
2.4 KiB
C#
67 lines
2.4 KiB
C#
using System;
|
|
using ln.types.btree;
|
|
using System.Reflection;
|
|
using ln.types.attributes;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
namespace ln.types.reflection
|
|
{
|
|
public class ObjectPoolContainer
|
|
{
|
|
BTree<string, ObjectPool> objectPools = new BTree<string, ObjectPool>();
|
|
public IEnumerable<ObjectPool> Pools => objectPools.Values;
|
|
public IEnumerable<Type> Types => objectPools.Values.Select((pool) => pool.ObjectType);
|
|
|
|
public ObjectPoolContainer()
|
|
{
|
|
}
|
|
|
|
public ObjectPool this[string typeName] => objectPools[typeName];
|
|
public ObjectPool this[Type type] => objectPools[type.Name];
|
|
|
|
public bool KnowsType(Type type) => objectPools.ContainsKey(type.Name);
|
|
|
|
public ObjectPoolContainer AddType(Type type) => AddType(type, null);
|
|
public ObjectPoolContainer AddType(Type type,String identityAttribute)
|
|
{
|
|
foreach (FieldInfo fieldInfo in type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
|
|
{
|
|
if (((identityAttribute == null) && (fieldInfo.GetCustomAttribute<IdentityAttribute>()!=null)) || fieldInfo.Name.Equals(identityAttribute))
|
|
{
|
|
objectPools.Add(type.Name, new ObjectPool(type, fieldInfo.FieldType, fieldInfo.GetValue));
|
|
}
|
|
}
|
|
foreach (PropertyInfo propertyInfo in type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
|
|
{
|
|
if (((identityAttribute == null) && (propertyInfo.GetCustomAttribute<IdentityAttribute>() != null)) || propertyInfo.Name.Equals(identityAttribute))
|
|
{
|
|
objectPools.Add(type.Name, new ObjectPool(type, propertyInfo.PropertyType, propertyInfo.GetValue));
|
|
}
|
|
}
|
|
|
|
SetupDerivedTypes(type);
|
|
|
|
return this;
|
|
}
|
|
|
|
public void SetupDerivedTypes(Type type)
|
|
{
|
|
Type current = type.BaseType;
|
|
while (!object.Equals(current, typeof(object)))
|
|
{
|
|
if (objectPools.ContainsKey(current.Name))
|
|
{
|
|
objectPools[current.Name].AddDerivedTypePool(objectPools[type.Name]);
|
|
break;
|
|
}
|
|
|
|
current = current.BaseType;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|