/// <summary> /// 获得实体属性列表 /// </summary> /// <param name="type"></param> /// <param name="ignoreKey">是否忽略 KEY主键</param> /// <returns></returns> public static IEnumerable <PropertyInfo> GetPropertyInfos(Type type, bool ignoreKey = true, bool ignore = false) { string key = string.Format(Common.EntityPropertyListCache, type.FullName); var instance = CacheHelper <IEnumerable <PropertyInfo> > .GetInstance(); IEnumerable <PropertyInfo> ps = instance.Get(key); if (ps != null && ps.Count() > 0) { return(ps); } var props = type.GetProperties(); if (props == null || props.Length < 1) { return(null); } SqlTableEntity table = GetTable(type); ps = props.Where(p => { SqlColumnEntity column = GetColumn(table, type, p.Name); if (column != null) { if (ignore && column.Ignore) { return(false); } if ((ignoreKey && column.Key) || column.Increment) { return(false); } if (column.ActionType == ActionType.Undo) { return(false); } } //var attrs = p.GetCustomAttributes(true); //if (ignore)//查询 不管 //{ // var ignoreAttr = attrs.OfType<IgnoreAttribute>().FirstOrDefault() as IgnoreAttribute; // if (ignoreAttr != null) return false; //} //if (ignoreKey) //{ // var keyAttr = attrs.OfType<KeyAttribute>().FirstOrDefault() as KeyAttribute; // if (keyAttr != null) return false; //} ////操作 可读,不能操作 忽略 //var actionAttr = attrs.OfType<ActionAttribute>().FirstOrDefault() as ActionAttribute; //if (actionAttr != null) //{ // if (actionAttr.ActionType == ActionType.Undo) return false; //} return(IsSimpleType(p.PropertyType)); }); instance.Add(key, ps); return(ps); }
public static SqlColumnEntity GetColumn(SqlTableEntity table, Type type, string columnName) { SqlColumnEntity column = null; if (table != null) { column = table.GetColumn(columnName); } if (column == null) { var p = type.GetProperty(columnName); if (p == null) { return(column); } var attrs = p.GetCustomAttributes(true); if (attrs == null || attrs.Length < 1) { return(column); } column = new SqlColumnEntity(columnName); int columnType = 1; var cattr = attrs.OfType <ColumnAttribute>().FirstOrDefault(); if (cattr != null) { column.FieldName = cattr.Name; columnType = 2; } var iattr = attrs.OfType <IgnoreAttribute>().FirstOrDefault(); if (iattr != null) { column.Ignore = true; columnType = 2; } var aattr = attrs.OfType <ActionAttribute>().FirstOrDefault(); if (aattr != null) { column.ActionType = aattr.ActionType; columnType = 2; } var kattr = attrs.OfType <KeyAttribute>().FirstOrDefault(); if (kattr != null) { column.Key = true; column.Increment = kattr.Increment; columnType = 2; } column.Type = columnType; table.AddColumn(column); } return(column); }
public void AddColumn(SqlColumnEntity column) { if (column == null) { return; } if (this.Columns == null) { this.Columns = new List <SqlColumnEntity>(); } var columns = this.Columns; if (columns.Count > 0) { var c = columns.Find(m => m.Type == column.Type && m.Name.Equals(column.Name, StringComparison.CurrentCultureIgnoreCase)); if (c != null) { columns.Remove(c); } } columns.Add(column); this.Columns = columns.OrderBy(m => m.Type).ToList(); }