title: How to get property name by one of it's attributes' value using reflection or how to get property info of data property which is currently validating?
body: <p>I want to write custom validation attribute and add additional member names which have validation errors to validation result. The thing is I want to generate member name dynamically based on property name and invalid match property index or key (I want to validate IEnumerables or IDictionaries) like <code>Names[0]</code>, <code>Names[1]</code>, <code>Names[key]</code> etc. For example:</p>
  
  <p><strong>Model:</strong></p>
  
  <pre><code>public class ModelClass
  {
      [ItemMaxLength(10)]
      [Display(ResourceType = typeof(CategoriesRename), Name = "CategoryNamesFieldName")]
      public IDictionary&lt;string, string&gt; Names { get; set; }
  }
  </code></pre>
  
  <p><strong>Attribute:</strong></p>
  
  <pre><code>public class ItemMaxLengthAttribute : ValidationAttribute
  {
      private readonly int _maxLength = int.MaxValue;
  
      public ItemMaxLengthAttribute(int maxLength)
      {
          _maxLength = maxLength;
      }
  
      protected override ValidationResult IsValid(object value, ValidationContext validationContext)
      {
          ...
          // I can get instance and it's type from validation context
          var instance = validationContext.ObjectInstance; // which is instance of ModelClass
          var instanceType = validationContext.ObjectType; //which is typeof(ModelClass)
          var dispayName = validationContext.DisplayName; //which is value of Display attribute
          ...
      }
  }
  </code></pre>
  
  <p>So the main idea is (I don't like it ether) get current property been validated by it's <code>DysplayName</code> attribute value (<code>dispayName</code>). I'm kind'a stuck here for a while. Maybe is there some other way to get property info of the property which is validating?</p>
  
  <p><strong>P.S.</strong> I've already tried <code>MemberName</code> property, as <strong>Alexandre Rondeau</strong> suggested, but the problem is that <code>validationContext.MemberName = null</code> so it can't be used. Also <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.validationcontext.membername.aspx" rel="nofollow">MSDN</a> says that <em>this property represents an entity member name, not the name of a corresponding data field</em> and I need <strong>the name of a corresponding data field</strong>.</p>