public static List <Campo> GerarCampos(object objeto) { if (objeto == null) { return(null); } List <Campo> campos = new List <Campo>(); var props = objeto.GetType().GetProperties(); foreach (var prop in props) { var display = prop.GetCustomAttributes(typeof(DisplayAttribute), true).FirstOrDefault() as DisplayAttribute; if (display == null) { continue; } Type type = prop.PropertyType; bool tipoValor = type.IsPrimitive; bool tipoString = type == typeof(string); bool tipoGeneric = type.IsGenericType; bool genericValor = type.GetGenericArguments().Count() > 0 && type.GetGenericArguments().FirstOrDefault().IsPrimitive; if (!tipoValor && !tipoString && !(tipoGeneric && genericValor)) { continue; } Campo c = new Campo(); c.Alias = string.IsNullOrEmpty(display.Name) ? prop.Name : display.Name; object o = prop.GetValue(objeto, null); c.Valor = o != null?o.ToString() : ""; c.Ordem = display.GetOrder() ?? 0; campos.Add(c); } campos.Sort((x, y) => x.Ordem - y.Ordem); return(campos); }
public Campo ObterCampo(string alias) { Campo campo = Campos.SingleOrDefault(x => x.Alias == alias); return(campo); }