7

How does inheritance of attribute classes work in C#? To clarify, I am talking about the inheritance of the attribute classes themselves (i.e. base classes of System.Attributes), NOT any classes that happen to have them as attribute.

For some reason, I cannot seem to find anything on this (my searches always turn up with the other meaning).

For example, if a have a class AttributeA which extends System.Attribute:

  1. Do I have to separately mark subclasses of AttributeA with [AttributeUsage()]. (Since System.AttributeUsage's inheritance is true, I don't think I will.)

  2. Will AllowMultiple=false on the AttributeUsage of AttributeA prevent me from having multiple subclasses of AttributeA for attributes?

EDIT:

Programmers can only read code.

[AttributeUsage(AttributeTargets.Class, AllowMultiple=false)]
class AttributeA
{ }

class AttributeB : AttributeA
{ }

class AttributeC : AttributeA
{ }

[AttributeB]
[AttributeC]
class Foo
{ }

Does this work?

3
  • 5
    It seems to me that both of these could be checked by trying it... Commented Apr 19, 2013 at 22:45
  • 1
    Yes, I am not able to do that right now...and I wanted to save someone else some time on this too. Even I could just find the relevant documentation, I would be happy... Commented Apr 19, 2013 at 22:47
  • Your basic assumption should be that there is no magic. Commented Jul 19, 2014 at 21:02

3 Answers 3

1

(I was finally able to try it.)

The example code does compile. I still like to have documentation, although I suppose C# is C# and it isn't going to change that. (Right?)

Evidently:

  1. The subclasses inherit AttributeUsage, as expected.
  2. AllowMultiple refers only to attributes of (strictly) the original class, not subclasses.
  3. Also, AttributeUsage can be effectively "re-defined".

With

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
class AttributeA : Attribute
{ }

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
class AttributeB : AttributeA
{ }

class AttributeC : AttributeA
{ }

the following is okay

[AttributeA]
[AttributeB]
[AttributeB]
[AttributeC]
class Foo
{ }

but this isn't

[AttributeA]
[AttributeB]
[AttributeC]
[AttributeC]
class Foo
{ }
Sign up to request clarification or add additional context in comments.

Comments

1

You can control this behavior by using the named parameter Inherited for your AttributeUsageAttribute.

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
class AttributeA
{ }

class AttributeB : AttributeA
{ }

class AttributeC : AttributeA
{ }

Comments

0

The attribute usage is for the classes using the attributes.

For example, if you mark class A with AttributeA and class B is a subclass of A, you don't need to mark class B with AttributeB if AttributeA AttributeUsage's inheritance is true.

4 Comments

You miss the question. I am asking about marking A with AttributeA AND subclass AttributeB.
[AttributeUsageAttribute(AttributeTargets.Class, Inherited=true)] public sealed class AttributeUsageAttribute : Attribute. So it looks like it is inheritable.
And it that allowed if AllowMultiple=false?
If it is inheritable it should inherit everything

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.