Example:
[OnlyShowIfValueIsNonZero]
public int Foo { get; set; }
In the code for OnlyShowIfValueIsNonZero, I need to be able to query the value of Foo. Is this possible? If yes, how?
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.
An attribute is not aware of the member it is assigned to, and for an attribute an instance will only be created if you access it via reflection. You cannot let an attribute interact with code directly – there are some frameworks like PostSharp though that inject code at compile time using attributes.
You can however from reflection supply the member it is assigned to, and since you already need the member to access its attributes, you could create a method in that attribute that accepts the member as parameter.