/// <summary> /// 是否该在数据库对象中定义pd属性指定的字段 /// </summary> /// <param name="pd"></param> /// <param name="b"></param> /// <returns></returns> private static bool FieldIsDefineOnDb(PropertyDescriptor pd, DataModelFieldCustomFilterAttribute b) { if (b != null) { return(b.IsDefineFeild(pd)); } NoneDatabaseFieldAttribute a = pd.Attributes[typeof(NoneDatabaseFieldAttribute)] as NoneDatabaseFieldAttribute; if (a != null) { return(a.IsDefineOnDb); } return(true); }
/// <summary> /// 设置扩展属性:字段集合(Fields,PrimaryKeys,IndexKey,SoftDeleteFieldName) /// </summary> /// <param name="ext"></param> /// <param name="type"></param> internal static void SetExtendProperty_Fields(IExtendProperty ext, Type type) { //自定义字段筛选特性 DataModelFieldCustomFilterAttribute a = null; if (Attribute.IsDefined(type, typeof(DataModelFieldCustomFilterAttribute), true)) { a = type.GetCustomAttributes(typeof(DataModelFieldCustomFilterAttribute), true)[0] as DataModelFieldCustomFilterAttribute; } List <IDataModelFieldIndex> primk = new List <IDataModelFieldIndex>(); List <IDataModelFieldIndex> indexk = new List <IDataModelFieldIndex>(); DataFieldModelCollection fields = new DataFieldModelCollection(); PropertyDescriptorCollection pdcols = TypeDescriptor.GetProperties(type); string softDeleteFieldName = null; foreach (PropertyDescriptor p in pdcols) { if (FieldIsDefineOnDb(p, a)) { IDataFieldModel field = new PropertyDescriptorDataModelField(p); fields.Add(field); var pk = field.IsPrimaryKey(); if (pk != null) { primk.Add(pk); } var ink = field.IsIndexKey(); if (ink != null) { indexk.Add(ink); } if (field.IsSoftDeleteField()) { if (!string.IsNullOrEmpty(softDeleteFieldName)) { throw new DataModelException(string.Format(LocalResource.DataModelException_ExtSFD, softDeleteFieldName, field.Name)); } softDeleteFieldName = field.Name; ext.SetExtendPropertyValue("SoftDeleteFieldName", softDeleteFieldName); } } } ext.SetExtendPropertyValue("Fields", fields); ext.SetExtendPropertyValue("PrimaryKeys", primk.ToArray()); ext.SetExtendPropertyValue("IndexKeys", indexk.ToArray()); object[] o = type.GetCustomAttributes(typeof(DataIdRefenceAttribute), true); if (o != null) { foreach (object item in o) { DataIdRefenceAttribute da = item as DataIdRefenceAttribute; var sfield = fields.FirstOrDefault(f => string.Equals(f.Name, da.SourceFieldName, StringComparison.OrdinalIgnoreCase)); if (sfield != null) { DataIdRefence df = new DataIdRefence() { RefenceIsParent = da.RefenceIsParent, RefenceModalType = da.RefenceModalType, SourceFieldName = da.SourceFieldName }; SetExtendProerty_DataIdRefence(sfield.InnerExtend, df); } } } }