28

I want to use DataAnnotations to validate classes that implements some interfaces, and so I'm adding validation attributes to the interface, like this:

public interface IUser
{
    [Required]
    string Name { get; set; }

    [Display(Name = "Email Address")]
    [Required]
    string Email { get; set; }
}

It doesn't work when I try to use Validator.TryValidateObject.

Is there any way to make this instead of having to write a custom TryValidateObject method?

2 Answers 2

45

I'm surprised no one mentioned MetadataTypeAttribute. But yes, this works.

[MetadataType(typeof(ICustomerMetaData))]
public partial class Customer
{
}


public interface ICustomerMetaData
{
  // Apply RequiredAttribute
  [Required(ErrorMessage = "Title is required.")]
  string Title { get; }
}

As for using an Interface directly (using Customer: ICustomerMetaData):

The product team does not want to implement this feature, for two main reasons:

• Consistency with DataAnnotations.Validator

• Consistency with validation behavior in ASP.Net MVC

• Tricky scenario: a class implements two interfaces that have the same property, but with conflicting attributes on them. Which attribute would take precedence?

While MVC automatically registers the MetaData with the TypeDescriptor, you may have to manually add it yourself:

  using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.ComponentModel.DataAnnotations;

  public class Program
  {
     public static void Main()
     {
        var customer = new Customer();

        TypeDescriptor.AddProviderTransparent(
          new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Customer), 
            typeof(ICustomerMetaData)), 
            typeof(Customer));

        var context = new ValidationContext(customer);
        var validationResults = new List<ValidationResult>();

        var isValid = Validator.TryValidateObject(
          customer, context, validationResults, true);
        Console.WriteLine($"is Valid = {isValid}");

        customer.Title = "I has Title";

        isValid = Validator.TryValidateObject(
          customer, context, validationResults, true);
        Console.WriteLine($"is Valid = {isValid}");


        Console.ReadKey();
     }

     [MetadataType(typeof(ICustomerMetaData))]
     public partial class Customer
     {
        public string Title { get; set;  }
     }

     public interface ICustomerMetaData
     {
        // Apply RequiredAttribute
        [Required(ErrorMessage = "Title is required.")]
        string Title { get; }
     }
  }

Output:

is Valid = False

is Valid = True

3
  • It doesn't work!! Not sure why it's upvoted, but it definitely doesn't work.
    – madd
    Oct 25, 2017 at 21:11
  • 1
    doesn't work means data annotations are ignored. That's why it doesn't work.
    – madd
    Dec 7, 2017 at 12:10
  • 2
    No one mentioned it since this works only for partial classes, the question is about interfaces. Please check the documentation on your own link.
    – fotisgpap
    Dec 1, 2020 at 6:13
7

If you use a base class instead of an interface, the attributes will work fine.

1
  • 1
    As classes don't inherit attributes from their interfaces it sounds like your going to have to write a custom validate method.
    – Lee Gunn
    May 7, 2011 at 6:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.