Attribute Inheritance on Interfaces #8494
Unanswered
BN-KS
asked this question in
Language Ideas
Replies: 1 comment 1 reply
-
What are some use cases for wanting this kind of inheritance, and why would it need such fine-grained control? The runtime already provides a facility for attributes to be inherited by derived types and overriding members through attribute inheritance, which is specified on the attribute by the |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Proposal: Attribute Inheritance on Interfaces with Fine-Grained Control
This proposal introduces a new language feature in C# allowing developers to specify how attributes defined on interfaces are inherited by implementing classes. The feature uses keywords such as :inherit, :virtual, :override, and :none to provide control over attribute propagation, making codebases more flexible, readable, and compatible with existing patterns.
Keywords
:inherit: Inherit this attribute unconditionally, regardless of whether the property is explicitly overridden in the implementing class.
:virtual: Inherit this attribute only if the property is not explicitly implemented or overridden in the class.
:override: Apply this attribute only if the class explicitly overrides the property.
:none: Prevent this attribute from being inherited, even if it's defined on the interface.
Syntax Proposal for Attribute Inheritance
Interface-Level Attribute Declaration
In this syntax, attributes are applied to properties in interfaces with specific inheritance rules using :inherit, :virtual, :override, and :none.
Class-Level Attribute Inheritance Control
Classes implementing interfaces can declare their intent to inherit all attributes from interface properties or customize which interface attributes to inherit.
Compact Syntax for Attribute Application
To maintain clarity and compactness, this syntax allows for marking class declarations or individual interface property overrides for inheritance. By default, inheritance is not assumed unless explicitly specified.
Summary of Syntax
Interface-Level: Mark attributes in interface properties with :inherit, :virtual, :override, or :none to control inheritance behavior.
Class-Level: Use [inherit] or [inheritAttributesFrom] to globally or selectively inherit attributes from interfaces in class implementations.
Compact Property Syntax: Attributes can be inherited on a per-property basis using :inherit, :virtual, :override, or :none keywords directly on class properties.
This approach introduces flexibility to define attribute inheritance, offering developers a powerful tool to streamline attribute handling in interface-based design patterns while keeping C# syntax compact and readable.
Looking for feedback or potentially alternatives to the proposed syntax.
For example we could do [.inherit] before a property to apply to all the properties
Beta Was this translation helpful? Give feedback.
All reactions