using System; using System.Collections; using System.Collections.Generic; using System.Reflection; using Zhaizj.Framework.Reflection; namespace Zhaizj.Framework.ORM { /// /// 实体类某个属性的元数据信息 /// [Serializable] public class EntityPropertyInfo { public EntityPropertyInfo() { this.IsList = false; this.IsAbstractEntity = false; this.IsEntity = false; this.ValidationAttributes = new List(); } /// /// 属性名称 /// public String Name { get; set; } /// /// 对应的数据列名 /// public String ColumnName { get; set; } /// /// 属性的类型,比如是int,还是string等等 /// public Type Type { get; set; } /// /// 属性信息(系统自带的元数据) /// public PropertyInfo Property { get; set; } /// /// 本属性所属的实体类信息,比如Blog有一个属性Title,则Title这个属性的ParentEntityInfo就是Blog /// public EntityInfo ParentEntityInfo { get; set; } /// /// 当本属性是实体属性时,此实体属性的信息。比如Blog的实体属性Category的EntityInfo。如果不是实体属性,则为null /// public EntityInfo EntityInfo { get; set; } /// /// 是否保存到数据库(是否打上了NotSave批注) /// public Boolean SaveToDB { get; set; } /// /// 是否是列表类型,列表类型不会保存到数据库 /// public Boolean IsList { get; set; } /// /// 是否是实体类属性 /// public Boolean IsEntity { get; set; } /// /// 是否是抽象类型实体 /// public Boolean IsAbstractEntity { get; set; } /// /// 当前属性的 ColumnAttribute /// public ColumnAttribute SaveAttribute { get; set; } /// /// 当前属性的 LongTextAttribute /// public LongTextAttribute LongTextAttribute { get; set; } /// /// 当前属性的 MoneyAttribute /// public MoneyAttribute MoneyAttribute { get; set; } /// /// 当前属性的 DecimalAttribute /// public DecimalAttribute DecimalAttribute { get; set; } /// /// 当前属性的 DefaultAttribute /// public DefaultAttribute DefaultAttribute { get; set; } /// /// 当前属性的 ValidationAttribute 的列表 /// public List ValidationAttributes { get; set; } /// /// 当前属性的赋值/取值器,可以避免反射的低效 /// internal IPropertyAccessor PropertyAccessor { get; set; } internal static EntityPropertyInfo Get( PropertyInfo property ) { EntityPropertyInfo ep = new EntityPropertyInfo(); object[] arrAttr = property.GetCustomAttributes( typeof( ValidationAttribute ), true ); foreach (Object at in arrAttr) { ep.ValidationAttributes.Add( at as ValidationAttribute ); } ep.SaveAttribute = ReflectionUtil.GetAttribute( property, typeof( ColumnAttribute ) ) as ColumnAttribute; ep.LongTextAttribute = ReflectionUtil.GetAttribute( property, typeof( LongTextAttribute ) ) as LongTextAttribute; ep.MoneyAttribute = ReflectionUtil.GetAttribute( property, typeof( MoneyAttribute ) ) as MoneyAttribute; ep.DecimalAttribute = ReflectionUtil.GetAttribute( property, typeof( DecimalAttribute ) ) as DecimalAttribute; ep.DefaultAttribute = ReflectionUtil.GetAttribute( property, typeof( DefaultAttribute ) ) as DefaultAttribute; ep.Property = property; ep.Name = property.Name; ep.Type = property.PropertyType; ep.SaveToDB = !property.IsDefined( typeof( NotSaveAttribute ), false ); if (property.PropertyType is IList) { ep.IsList = true; ep.SaveToDB = false; } return ep; } /// /// 获取obj的当前属性的值 /// /// /// public Object GetValue( Object target ) { return PropertyAccessor.Get( target ); } /// /// 给obj的当前属性赋值 /// /// /// public void SetValue( Object target, Object value ) { PropertyAccessor.Set( target, value ); } /// /// 是否是长文本 /// public Boolean IsLongText { get { if (Type != typeof( String )) return false; return LongTextAttribute != null || ((SaveAttribute != null) && (SaveAttribute.Length > 255)); } } /// /// 获取属性的label(用在表单中) /// public String Label { get { if (SaveAttribute == null) return Name; if (strUtil.IsNullOrEmpty( SaveAttribute.Label )) return Name; return SaveAttribute.Label; } } } }