how to get data annotation attributes from properties
I am trying to build a grid, using class info and its properties along with the supported dataannotion attributes.
Class and annotations might be defined in different classes, like (Product and ProductMetaData classes) and tied togather with the metadatatype attribute, (might also be defined in a single class).
Would this make the DataAnnotaion attributes available in the base class itself or are there other means of getting the dat开发者_JAVA技巧aannotation attributes?
Any sample code that you can link, would also be very helpful.
thanks.
To read model metadata from a class you should generally create an instance of the DataAnnotationsModelMetadataProvider (in System.ComponentModel).
This class generates a ModelMetadata object which is a summary of the meta data on the class.
One of the main advantages of this approach is that it automatically respects metadata on buddy metadata classes.
By using ModelMetadata you also introduce a nice abstraction between the specific metadata attributes and the interpretation of their meaning. For example if you create some of your own metadata attributes, or you want to enforce other sources of metadata (e.g. treat all properties ending in Date as if they had DataType.Date applied even if they don't) then you can create your own DataAnnotationsModelMetadataProvider, add these extra rules, and all your other code works unchanged because it reads from a ModelMetadata class - not directly from knowledge of specific attributes.
精彩评论