El atributo en los miembros de la interfaz no funciona

In my application several models need Password properties (eg, Registration y ChangePassword models). The Password property has attribute like DataType y Required. Therefore to ensure of re-usability an consistency, I created :

interface IPasswordContainer{
    [Required(ErrorMessage = "Please specify your password")]
    string Password { get; set; }


class RegistrationModel : IPasswordContainer {
    public string Password { get; set; }

Unfortunately, the attributes does not work.

Then I tried changing the interface to a class:

public class PasswordContainer {
    [Required(ErrorMessage = "Please specify your password")]
    public virtual string Password { get; set; }


public class RegistrationModel : PasswordContainer {
    public override string Password { get; set; }

Now it is working. Why it is so?

Why the attributes are working when inherited from class but not working when inherited from interface?

preguntado el 24 de agosto de 12 a las 10:08

@KyorCode: I could not find the question you posted in first comment while searched. Even not in the list of related questions. I have marked this question as exact duplicate of the question you refereed. -

1 Respuestas

Attributes on interface properties doesn't get inherited to the class, you may make your interface an Abstract Class.

Encontré un respuesta de Microsoft:

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?

Respondido 24 ago 12, 10:08

Though partly understood, it is pretty disappointing. What does not make sense to me: Why should multiple inheritance be an issue with property attributes but not with method attributes? And why should language design be influenced by a certain framework such as ASP.Net MVC that is based on the language? - chiccodoro

It does, in turn, make kind of sense because attributes add behavior, and interfaces are not supposed to define behavior. A class implementing an interface is supposed to do so. - chiccodoro

@chiccodoro they don't define behavior. they provide meta information. an attribute by itself will have no effect without additional code and thus does not define behavior. let's not make excuses for this feature not existing. - Doble

@AndreasMüller (respectfully) disagree - Attributes define a behaviour pattern, just not the implementation of the behaviour. - Tipo CAD

@CADbloke not necessairily no. lets take DebuggerDisplayAttribute for an example. its purpose is nothing else other than providing metadata to the consumer of that attribute. without the consumer there is no behavior at all. in fact you could consume it yourself and do something completely different. thus attributes do not define behavior - it's the consumer of the attribute most of the time (depending on how you solve your issues) - Doble

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.