20

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

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

And

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")]
    [DataType(DataType.Password)]
    public virtual string Password { get; set; }
}

And

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?

Mohayemin
  • 3,841
  • 4
  • 25
  • 54
  • http://stackoverflow.com/questions/540749/can-a-c-sharp-class-inherit-attributes-from-its-interface – KyorCode Aug 24 '12 at 09:28
  • http://bradwilson.typepad.com/blog/2011/08/interface-attributes-class-attributes.html also usefull – KyorCode Aug 24 '12 at 09:29
  • @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. – Mohayemin Aug 24 '12 at 09:54

1 Answers1

29

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

Found an answer from 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?
Habib
  • 219,104
  • 29
  • 407
  • 436
  • 3
    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 Jul 30 '14 at 08:57
  • 11
    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 Jul 30 '14 at 09:01
  • 9
    @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. – Dbl May 12 '15 at 14:44
  • 1
    @AndreasMüller (respectfully) disagree - Attributes define a behaviour pattern, just not the implementation of the behaviour. – CAD bloke Mar 11 '16 at 19:23
  • 1
    @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) – Dbl Mar 11 '16 at 20:12
  • You are totally right. That will teach me to post on SO after 4 hours sleep. Also, the whole purpose of an Interface is to describe patterns to be implemented later When I'm wrong, I'm totally wrong. Cheers for the wake-up. – CAD bloke Mar 11 '16 at 20:17
  • While I understand the sentiment of the last point ,is it so hard to do it like other languages? It either uses the first or last one specified (the convention needs to be defined) essentially, specifying the order of importance. This gets hairy when you have multiple inherited same attributes, enforce the same rule, and if the user needs some weird scenario the onus is on the developer to improve their solution. – Adam Hess Oct 20 '16 at 14:12
  • If you need to get the attributes inherited from interfaces use `typeof(T).GetInterfaces().Select(x => x.GetCustomAttributes());` – Wouter Jun 21 '21 at 15:21