public static void FetchREFEntity(object objEntity, REFAttribute refAtt, DataTable table) { int rindex = -1;//根据objEntity的Id属性,在table中找到对应的行数 for (int i = 0; i < table.Rows.Count; i++) { if (((BaseEntity)objEntity).Id == Convert.ToInt64(table.Rows[i]["E_" + objEntity.GetType().Name + "Id"])) { rindex = i; break; } } //对象的该引用属性为空值 if (table.Rows[rindex]["E_" + refAtt.REFEntityName + "Id"] == DBNull.Value) { return; } object objRef = DLLAnalysis.GetEntityInstance(refAtt.REFEntityName); ((BaseEntity)objRef).Id = Convert.ToInt64(table.Rows[rindex]["E_" + refAtt.REFEntityName + "Id"]); EntityInfo refeinfo = DLLAnalysis.GetEntityInfoByType(objRef.GetType()); foreach (FieldAttribute fieldAtt in refeinfo.Properties) { FetchPropertyValue(objRef, fieldAtt, table, rindex); } objEntity.GetType().GetProperty(refAtt.PropertyName).SetValue(objEntity, objRef, null); }
/// <summary> /// 根据实体的Type获得实体的类媒数据信息 /// </summary> /// <param name="type">实体类的Type</param> /// <returns>实体的媒数据信息</returns> public static EntityInfo GetEntityInfoByType(Type type) { if (!type.IsSubclassOf(typeof(BaseEntity))) { return(null); } EntityInfo ei = new EntityInfo(); ei.EntityName = type.Name;//获得实体的名称 object[] atts = type.GetCustomAttributes(typeof(EntityAttribute), false); if (atts != null && atts.Length > 0) { ei.EntityDisplayName = ((EntityAttribute)atts[0]).EntityDisplayName;//获得实体的显示名称 } PropertyInfo[] properties = type.GetProperties(); List <FieldAttribute> pros = new List <FieldAttribute>(); List <REFAttribute> refs = new List <REFAttribute>(); List <SETAttribute> sets = new List <SETAttribute>(); foreach (PropertyInfo pi in properties) { object[] patts = pi.GetCustomAttributes(false); if (patts != null && patts.Length > 0) { object objAtt = patts[0]; if (objAtt is FieldAttribute) { ((FieldAttribute)objAtt).FieldName = pi.Name; pros.Add((FieldAttribute)objAtt); } else if (objAtt is REFAttribute) { REFAttribute refAtt = (REFAttribute)objAtt; refAtt.EntityName = ei.EntityName; refAtt.PropertyName = pi.Name; refAtt.REFFieldName = refAtt.REFEntityName + "Id"; refs.Add(refAtt); } else if (objAtt is SETAttribute) { SETAttribute setAtt = (SETAttribute)objAtt; setAtt.EntityName = ei.EntityName; setAtt.PropertyName = pi.Name; if (setAtt.IsMidTable) { setAtt.MidTableName = type.Name + "_" + setAtt.ElementEntityName; setAtt.OwnerFieldName = type.Name + "Id"; setAtt.ElementEntityId = setAtt.ElementEntityName + "Id"; } else { setAtt.ElementEntityId = ei.EntityName + "Id"; } sets.Add(setAtt); } //如果有引用自生的属性,则将REFSelf置为true; else if (objAtt is REFSelfAttribute) { ei.REFSelf = true; } } } ei.Properties = pros; ei.References = refs; ei.Sets = sets; return(ei); }