using Ant.Common; using Ant.Mapper; using Ant.Utility; using System; using System.Collections.Generic; using System.Data; using System.Reflection; namespace Ant.Descriptors { public class EntityConstructorDescriptor { EntityMemberMapper _mapper = null; EntityConstructor _entityConstructor = null; EntityConstructorDescriptor(ConstructorInfo constructorInfo) { this.ConstructorInfo = constructorInfo; this.Init(); } void Init() { ConstructorInfo constructor = this.ConstructorInfo; Type type = constructor.DeclaringType; if (AntUtils.IsAnonymousType(type)) { ParameterInfo[] parameters = constructor.GetParameters(); this.MemberParameterMap = new Dictionary(parameters.Length); foreach (ParameterInfo parameter in parameters) { PropertyInfo prop = type.GetProperty(parameter.Name); this.MemberParameterMap.Add(prop, parameter); } } else this.MemberParameterMap = new Dictionary(0); } public ConstructorInfo ConstructorInfo { get; private set; } public Dictionary MemberParameterMap { get; private set; } public Func GetInstanceCreator() { EntityConstructor entityConstructor = null; if (null == this._entityConstructor) { this._entityConstructor = EntityConstructor.GetInstance(this.ConstructorInfo); } entityConstructor = this._entityConstructor; return entityConstructor.InstanceCreator; } /// /// 获取实体映射关系 /// /// public EntityMemberMapper GetEntityMemberMapper() { EntityMemberMapper mapper = null; if (null == this._mapper) { this._mapper = EntityMemberMapper.GetInstance(this.ConstructorInfo.DeclaringType); } mapper = this._mapper; return mapper; } static readonly System.Collections.Concurrent.ConcurrentDictionary InstanceCache = new System.Collections.Concurrent.ConcurrentDictionary(); /// /// /// /// /// public static EntityConstructorDescriptor GetInstance(ConstructorInfo constructorInfo) { EntityConstructorDescriptor instance; if (!InstanceCache.TryGetValue(constructorInfo, out instance)) { lock (constructorInfo) { if (!InstanceCache.TryGetValue(constructorInfo, out instance)) { instance = new EntityConstructorDescriptor(constructorInfo); InstanceCache.GetOrAdd(constructorInfo, instance); } } } return instance; } } }