1

Is it possible to prevent an attribute to be applied to a given method (or class), if another attribute has already been specified?

For example, I would like to emit a compile time error or warning if attribute B is applied to a method where the attribute A has already been applied:

public class AttributeA : Attribute{}
public class AttributeB : Attribute{}

[A]
[B] // generates error or warning because of A
public void Foobar(){}

I don't think it's possible but it worth a question.

Heisenbug
  • 38,762
  • 28
  • 132
  • 190

1 Answers1

0

It's not possible. A few months back I was looking to do something similar to what ObsoleteAttribute does by triggering a compiler warning for my custom attribute when I ran across this answer.

Community
  • 1
  • 1
Brandon
  • 4,491
  • 6
  • 38
  • 59