using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class TestAttribute : ValidationAttribute
public TestAttribute() : base("Error message"){}
public override bool IsValid(object o)
ErrorMessage = "{0} is not even, it is " + value;
public int TestProperty { get; set; }
public static void Main(string[] args){
TestModel invalidModel = new TestModel() { TestProperty = 1 };
TestModel validModel = new TestModel() { TestProperty = 2 };
private static void Validate(TestModel model)
ValidationContext vc = new ValidationContext(model);
List<ValidationResult> results = new List<ValidationResult>();
bool isValid = Validator.TryValidateObject(model, vc, results, true);
Console.Out.WriteLine(isValid);
results.ForEach(c => Console.Out.WriteLine(c.ErrorMessage));