Can an attribute be applied to a static class, method or property in c#? Like:
[MyAttribute]
public static MyMethods(string str) ...
There are really two questions here
Is it possible for attributes in general to be applied to class, method's or properties?
Yes attributes can validly target any of these constructs (and many others)
Is it valid for a specific attribute to do so?
That depends on the specific attribute. Attributes can control which constructs they can be applied to via the AttributeTargets
enum and hence make it illegal for a specific attribute to be applied to a specific construct.
For example the ParamArrayAttribute
can only target parameters while the ObsoleteAttribute
can target pretty much anything (except assemblies and maybe one other I'm missing)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With