Inheriting C # attribute classes

How does attribute class inheritance work in C #? To clarify, I'm talking about the inheritance of the attribute classes themselves (i.e., Base classes System.Attributes), and not about any classes that have them as an attribute.

For some reason I cannot find anything on this (my searches always appear with a different meaning).

For example, if a has a class AttributeAthat extends System.Attribute:

  • Do I need to mark subclasses separately AttributeAwith [AttributeUsage()]. (Since System.AttributeUsageinheritance true, I don’t think I will.)

  • Will AllowMultiple=falsein AttributeUsageof AttributeAprevent me from having multiple subclasses AttributeAfor attributes?

EDIT:

Programmers can only read code.

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

class AttributeB : AttributeA
{ }

class AttributeC : AttributeA
{ }

[AttributeB]
[AttributeC]
class Foo
{ }

It works?

+5
3

(, .)

. , , # - #, . (?)

:

  • AttributeUsage, .
  • AllowMultiple () , .
  • , AttributeUsage "".

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

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

class AttributeC : AttributeA
{ }

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

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

, Inherited AttributeUsageAttribute.

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

class AttributeB : AttributeA
{ }

class AttributeC : AttributeA
{ }
+1

Using Attributes for Classes Using Attributes.

For example, if you mark class A with attribute A and class B is a subclass of A, you do not need to mark class B with attribute B if AttributeA AttributeUsage is true.

0
source

All Articles