/// <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 static void AddTable(SqlTableEntity table) { var t = tables.Find(m => m.Type == table.Type && m.Name.Equals(table.Name, StringComparison.CurrentCultureIgnoreCase)); if (t != null) { tables.Remove(t); } tables.Add(table); tables = tables.OrderBy(m => m.Type).ToList(); }
public static SqlTableEntity GetTable(Type type) { if (type == null) { return(null); } SqlTableEntity table = GlobalConfig.GetTable(type.Name); if (table == null) { table = new SqlTableEntity(type); table.Columns = new List <SqlColumnEntity>(); var attr = type.GetCustomAttributes(true).OfType <TableAttribute>().FirstOrDefault(); if (attr != null) { table.Type = 2; table.TableName = attr.Name; } GlobalConfig.AddTable(table); } return(table); }