This will work for all your classes if the hierarchy is one, two ,three, four levels etc too. Only thing missing is OrderBy. I believe that piece can be done by you once the listing is got.
public static List<PropertyInfo> GetSearchProperties(Type type)
{
if (type == null) throw new ArgumentNullException("type");
List<PropertyInfo> propL = new List<PropertyInfo>();
foreach (var item in type.GetProperties())
{
object[] obj = item.GetCustomAttributes(typeof(Searchable), true);
if (obj.Count() > 0)
{
propL.Add(item);
}
else
{
propL.AddRange(GetSearchPropertiesWithOrderBy(item.PropertyType));
}
}
return propL;
}
[System.AttributeUsage(System.AttributeTargets.Property)]
public class Searchable : System.Attribute
{
}
public class LookUpData
{
[Searchable]
public string ManufactureName { get; set; }
}
public class Car
{
public int ID { get; set; }
[Searchable]
public string Model { get; set; }
public int ManufactureId { get; set; }
public LookUpData LookUpData { get; set; }
}
With hierarchy it means if your LookUpData property was not declared directly in Car class For example, then too this code will work perfectly fine.
public class MyDataModels
{
LookUpData MysetOfData { get; set; }
int MyVal { get; set; }
}
public class MyCar
{
public int ID { get; set; }
[Searchable]
public string Model { get; set; }
public int ManufactureId { get; set; }
public MyDataModels MyModel { get; set; }
}