ln.types/reflection/ObjectPoolContainer.cs

55 lines
1.9 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 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)
{
}
}
}