/// <summary> /// 属性修改时忽略 /// </summary> /// <param name="prop"></param> /// <returns></returns> private static bool PropModifyIgnore(Cache_ModelInfor_Property propinfor, Func <ModelFieldAttribute, bool> func) { if (func == null) { func = (a) => { return(false); }; } ModelFieldAttribute attr = propinfor.PropertyModelAttr; if (attr != null) { if (attr.IdentityKey || attr.UseDatabaseDefaultValue || func(attr)) { return(true); } } return(false); }
/// <summary> /// 装载模型属性 /// </summary> /// <param name="type"></param> /// <param name="funcEach"></param> /// <param name="funcIgnore"></param> private static Cache_ModelInfor LoadModelProperty(Type type, Action <Cache_ModelInfor_Property> funcEach, Func <Cache_ModelInfor_Property, bool> funcIgnore) { if (GLB_CACHE_MODELPROPERTYS.ContainsKey(type)) { Cache_ModelInfor cminfo = GLB_CACHE_MODELPROPERTYS[type]; List <Cache_ModelInfor_Property> t_props = cminfo.PropertyInfoList; foreach (Cache_ModelInfor_Property pi in t_props) { if (!funcIgnore(pi)) { funcEach(pi); } } return(cminfo); } else { List <Cache_ModelInfor_Property> t_props; t_props = new List <Cache_ModelInfor_Property>(); string fieldname, pkname = string.Empty, pksqlexp = string.Empty; bool b_pkint = false; Cache_ModelInfor_Property porpinfor; PropertyInfo prop_idf = null, prop_pk = null; ModelFieldAttribute attr; PropertyInfo[] props = type.GetProperties(); foreach (PropertyInfo p in props) { if (p.CanWrite) { #region 属性处理 object[] attrs = p.GetCustomAttributes(false); if (attrs.Length > 0) { attr = (ModelFieldAttribute)attrs[0]; // 直接忽略 if (attr.Ignore) { continue; } // 获取自定义字段名 if (string.IsNullOrEmpty(attr.FieldName)) { fieldname = p.Name; } else { fieldname = attr.FieldName; } // 获取自增键 if (attr.IdentityKey) { prop_idf = p; } // 获取主键 if (attr.PrimaryKey) { prop_pk = p; pkname = fieldname; // 主键仅识别字符串类型和非字符串类型 if (p.PropertyType == typeof(string)) { pksqlexp = pkname + "='{0}'"; } else { b_pkint = true; pksqlexp = pkname + "={0}"; } } } else { fieldname = p.Name; attr = null; } #endregion porpinfor = new Cache_ModelInfor_Property { FieldName = fieldname, Property = p, PropertyModelAttr = attr }; t_props.Add(porpinfor); try { if (!funcIgnore(porpinfor)) { funcEach(porpinfor); } } catch { } } } string key_tablename = GLB_GetDBTableName(type); Cache_ModelInfor r = new Cache_ModelInfor { TableName = key_tablename, PrimaryKeyProperty = prop_pk, PrimaryKeyFieldName = pkname, PrimaryKeySQLConditionExpress = pksqlexp, IdentityProperty = prop_idf, PropertyInfoList = t_props, IsIntPrimaryKey = b_pkint }; GLB_CACHE_MODELPROPERTYS.Add(type, r); return(r); } }