Can an attribute be applied to a static class, method or property in c#? Like:
[MyAttribute]
public static MyMethods(string str) ...
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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
AttributeTargetsenum and hence make it illegal for a specific attribute to be applied to a specific construct.For example the
ParamArrayAttributecan only target parameters while theObsoleteAttributecan target pretty much anything (except assemblies and maybe one other I’m missing)