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 objectPools = new BTree(); public IEnumerable Pools => objectPools.Values; public IEnumerable 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()!=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() != 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; } } } }