Jacob Eggers
Jacob Eggers

Reputation: 9322

Extend an Attribute

Here is my situations, I use this attribute MyAttrAttribute all over my codebase, I would now like to give it the property such that any place that I use [MyAttr], it also applies a third party attribute [TheirAttr].

Of course, I could do a find a replace across my code to extend the attribute, but is there a way to modify MyAttrAttribute to apply the third party attribute as well?

Upvotes: 1

Views: 1099

Answers (1)

AlexD
AlexD

Reputation: 32576

You could derive MyAttrAttribute from TheirAttrAttribute, and then Attribute.GetCustomAttribute method should work with both types:

public static Attribute GetCustomAttribute(
    Assembly element,
    Type attributeType
)

....
attributeType
    Type: System.Type
    The type, or a base type, of the custom attribute to search for.

Upvotes: 1

Related Questions