0

I want to iterate through my class properties (may be metaData) to determine which validation ([Required] and [MaxLength] for example) or other attributes have been applied to those properties.

// model is my class

var modelTypeTemp = model.GetType();
var metaData = ModelMetadataProviders.Current.GetMetadataForType(null, modelTypeTemp);
var behaviourAttributes= metaData.GetBehaviourDataAttributes();
var temp2=modelTypeTemp.GetCustomAttributesData();
object[] attrs = modelTypeTemp.GetCustomAttributes(true);

foreach (Attribute attr in attrs)
{
    var temp = attr;
}

I have tried different combinations of the code above (Custom and Behaviour attributes) but still can't see the applied attributes.

I want to do this for debugging purposes.

David Tansey
  • 5,813
  • 4
  • 35
  • 51
  • Take a look at the accepted answer in the following post: http://stackoverflow.com/questions/2051065/check-if-property-has-attribute?noredirect=1&lq=1 – David Tansey Apr 25 '17 at 15:45
  • 2
    Possible duplicate of [Check if property has attribute](http://stackoverflow.com/questions/2051065/check-if-property-has-attribute) – T.S. Apr 25 '17 at 15:55
  • Do you have access to the HtmlHelper (the @Html object used on views) object where you are trying to debug? – Cat_Clan Apr 26 '17 at 00:29

0 Answers0