title: Validating DataAnnotations with Validator class
body: <p>I'm trying to validate a class decorated with data annotation with the <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.validator(VS.100).aspx" rel="noreferrer">Validator class</a>.</p>
  
  <p>It works fine when the attributes are applied to the same class. But when I try to use a metadata class it doesn't work. Is there anything I should do with the Validator so it uses the metadata class? Here's some code..</p>
  
  <p>this works:</p>
  
  <pre><code>public class Persona
  {
      [Required(AllowEmptyStrings = false, ErrorMessage = "El nombre es obligatorio")]
      public string Nombre { get; set; }
  
      [Range(0, int.MaxValue, ErrorMessage="La edad no puede ser negativa")]
      public int Edad { get; set; }
  }
  </code></pre>
  
  <p>this doesnt work:</p>
  
  <pre><code>[MetadataType(typeof(Persona_Validation))]
  public class Persona
  {
      public string Nombre { get; set; }
      public int Edad { get; set; }
  }
  
  public class Persona_Validation
  {
      [Required(AllowEmptyStrings = false, ErrorMessage = "El nombre es obligatorio")]
      public string Nombre { get; set; }
  
      [Range(0, int.MaxValue, ErrorMessage = "La edad no puede ser negativa")]
      public int Edad { get; set; }
  }
  </code></pre>
  
  <p>this is how I validate the instances:</p>
  
  <pre><code>ValidationContext context = new ValidationContext(p, null, null);
  List&lt;ValidationResult&gt; results = new List&lt;ValidationResult&gt;();
  
  bool valid = Validator.TryValidateObject(p, context, results, true);
  </code></pre>
  
  <p>thanks.</p>