1

can a Required attribute be revoked in overriden method?

Example: Baseclass virtual property A has attribute Required attached assigned to it.

Derived class overrides property A to reasign attributes, but somehow the Required attribute cannot be removed.

I couldn't with override. Only the same property with new worked but this is not what I want. I want to override the base classes property.

Sadly the IsRequired attribute has no Enabled property or similar.

CleanCoder
  • 2,406
  • 1
  • 10
  • 35
  • 1
    Can you post an example code – Rahul May 15 '20 at 20:57
  • 1
    Why do you add the `[Required]` attribute when it shouldn't be required? Wouldn't it be right to remove the attribute? – Progman May 15 '20 at 21:04
  • 2
    Does this answer your question? [Is it possible to override the required attribute on a property in a model?](https://stackoverflow.com/questions/8903838/is-it-possible-to-override-the-required-attribute-on-a-property-in-a-model) – Ruslan Kamev May 15 '20 at 21:06
  • for general purposses in most cases the property in base class is marked as required. For example a username. a subclass with an optional username would need to remove that attribute. – CleanCoder May 15 '20 at 22:27
  • 1
    A subclass would be violating the superclass contract by making a required property optional for any client reading that property. – Aluan Haddad May 16 '20 at 02:38

0 Answers0