How to validate Data Annotations with a MetaData class
Posted
by Micah
on Stack Overflow
See other posts from Stack Overflow
or by Micah
Published on 2010-01-22T04:38:15Z
Indexed on
2010/03/22
4:01 UTC
Read the original article
Hit count: 341
I'm trying to validate a class using Data Annotations but with a metadata class.
[MetadataType(typeof(TestMetaData))]
public class Test
{
public string Prop { get; set; }
internal class TestMetaData
{
[Required]
public string Prop { get; set; }
}
}
[Test]
[ExpectedException(typeof(ValidationException))]
public void TestIt()
{
var invalidObject = new Test();
var context = new ValidationContext(invalidObject, null, null);
context.MemberName = "Prop";
Validator.ValidateProperty(invalidObject.Prop, context);
}
The test fails. If I ditch the metadata class and just decorated the property on the actual class it works fine. WTH am I doing wrong? This is putting me on the verge of insanity. Please help.
© Stack Overflow or respective owner