/// <summary> /// Read Table and Column attributes and create SelectModel with filled data /// </summary> /// <typeparam name="TModel">Model Type</typeparam> /// <param name="model">model</param> /// <returns></returns> public static SelectModel Create <TModel>(TModel model) where TModel : new() { var result = new SelectModel(); var properties = typeof(TModel).GetProperties().Where(prop => prop.IsDefined(typeof(ColumnAttribute), false)); var table = (TableAttribute)typeof(TModel).GetTypeInfo().GetCustomAttribute(typeof(TableAttribute)); var tableName = string.Empty; if (table != null) { result.Table = table.Name; result.Schema = table.Schema; } var list = new List <KeyValuePair <int, string> >(); foreach (var item in properties) { ColumnAttribute attributes = (ColumnAttribute)item.GetCustomAttribute(typeof(ColumnAttribute), false); if (attributes != null) { ColumnPositionAttribute pos = (ColumnPositionAttribute)item.GetCustomAttribute(typeof(ColumnPositionAttribute), false); if (pos != null) { list.Add(new KeyValuePair <int, string>(pos.Position, attributes.Name)); } } } result.Columns = list.OrderBy(p => p.Key).Select(p => p.Value).ToList(); return(result); }
/// <summary> /// Get Column Position from attribute /// </summary> /// <param name="val">Property</param> /// <returns>Column Position</returns> public static int GetColumnPosition(object val) { ColumnPositionAttribute attributes = (ColumnPositionAttribute)val .GetType() .GetTypeInfo() .GetCustomAttribute(typeof(ColumnPositionAttribute), false); if (attributes != null) { return(attributes.Position); } else { throw new NotSupportedException(@"This Property does not have ""ColumnPositionAttribute"""); } }