I’d like to implement dynamic WebGrid Creation from Model.
The Idea is to Create a Grid from Model “Description” by annotating the Model Properties with attributes.
class Model
{
public List<Product> Products {get;set;}
}
class Product
{
[GridColumn]
public String Name {get;set;}
....
}
Then I’d like to Get By Reflection All properties marked by this Attribute.
public WebGridColumns[] ColumnsFromModel(object model)
{
// Here model is List<T> so how get all custom attributes of List<T> ?
}
You can create a simple extension method that will get the attributes you want from an implementation of the
ICustomAttributeProviderinterface (which is implemented by any representation of a .NET construct that can have an attribute on it):From there, it’s a call over all the
PropertyInfoinstances on a type, like so:From there, you can call the
GetTypemethod on any object instance and run it through the above query to get thePropertyInfoinstance and the attribute that is applied to it.