title: ASP.Net MVC3, DataAnnotations and Dynamic Validation
body: <p>I'm creating an MVC 3 application that needs to establish data validation rules at runtime based on external data (e.g. Required, MinimumLength, MaximumLength).  It seems natural to use Data Annotations in MVC 3, however the property attributes that provide validation metadata are set at compile-time.</p>
  
  <p>Is there a pattern to use Data Annotations with metadata provided at runtime?</p>
  
  <p>Example:</p>
  
  <pre><code>public string Text { get; set; }
  
  public void SetIsRequired(string propertyName, bool required)
  {
      // Somehow find the property 'propertyName' and create/remove a RequiredAttribute
      // on that property
  }
  
  ...
  SetIsRequired("Text", true);
  </code></pre>
  
  <p>I'm aware of <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.typedescriptor.aspx" rel="nofollow">TypeDescriptor</a>, but don't see an option to modify attributes of a property of an instance (only class level attributes on an instance, or property level attributes for a type).</p>