/// <summary> /// 获取字段,并指定是否为基本查询字段(包函虚拟字段) /// </summary> /// <param name="type"></param> /// <param name="onlyField">是否为基本查询字段</param> /// <returns></returns> public static IgnoreCaseDictionary <Attribute.FieldAttribute> GetProperties(Type type, bool onlyField) { var table = GetTable(type); var list = new IgnoreCaseDictionary <Attribute.FieldAttribute>(); foreach (var item in table.Fields) { if (onlyField) { if (item.FieldType != Attribute.FieldType.数据库字段 && item.FieldType != Attribute.FieldType.虚拟字段) { continue; } } list.Add(item.Name, item.Clone()); } return(list); }
static void SetProperties(Attribute.TableAttribute table) { if (table.Fields.Count > 0) { return; } var type = table.Type; List <Attribute.FieldAttribute> list = new List <CRL.Attribute.FieldAttribute>(); var fieldDic = new IgnoreCaseDictionary <Attribute.FieldAttribute>(); //string fieldPat = @"^([A-Z][a-z|\d]+)+$"; int n = 0; Attribute.FieldAttribute keyField = null; #region 读取 var typeArry = table.Type.GetProperties().ToList(); //移除重复的 var dic = new Dictionary <string, PropertyInfo>(); foreach (PropertyInfo info in typeArry) { if (!dic.ContainsKey(info.Name)) { dic.Add(info.Name, info); } } foreach (PropertyInfo info in dic.Values) { //if (!System.Text.RegularExpressions.Regex.IsMatch(info.Name, fieldPat)) //{ // throw new CRLException(string.Format("属性名:{0} 不符合规则:{1}", info.Name, fieldPat)); //} //排除没有SET方法的属性 if (info.GetSetMethod() == null) { continue; } Type propertyType = info.PropertyType; Attribute.FieldAttribute f = new CRL.Attribute.FieldAttribute(); //排除集合类型 if (propertyType.FullName.IndexOf("System.Collections") > -1) { continue; } object[] attrs = info.GetCustomAttributes(typeof(Attribute.FieldAttribute), true); if (attrs != null && attrs.Length > 0) { f = attrs[0] as Attribute.FieldAttribute; } f.SetPropertyInfo(info); f.PropertyType = propertyType; f.MemberName = info.Name; f.TableName = table.TableName; f.ModelType = table.Type; //if (!string.IsNullOrEmpty(f.VirtualField)) //{ // if (SettingConfig.StringFormat != null) // { // f.VirtualField = SettingConfig.StringFormat(f.VirtualField); // } // f.VirtualField = f.VirtualField.Replace("$", "{" + type.FullName + "}");//虚拟字段使用Type名 //} //排除不映射字段 if (!f.MapingField) { continue; } if (propertyType == typeof(System.String)) { if (f.Length == 0) { f.Length = 30; } } if (f.IsPrimaryKey)//保存主键 { table.PrimaryKey = f; f.FieldIndexType = Attribute.FieldIndexType.非聚集唯一; keyField = f; n += 1; } //if (f.FieldType != Attribute.FieldType.关联字段) //{ fieldDic.Add(f.MemberName, f); //} f.MapingNameFormat = string.Format("[{0}]", f.MapingName); list.Add(f); } if (n == 0) { //throw new CRLException(string.Format("对象{0}未设置任何主键", type.Name)); } else if (n > 1) { throw new CRLException(string.Format("对象{0}设置的主键字段太多 {1}", type.Name, n)); } #endregion //主键排前面 if (keyField != null) { list.Remove(keyField); list.Insert(0, keyField); } table.Fields = list; table.FieldsDic = fieldDic; }