Reputation: 925
Is there a way to set a property value using attribute? In View Model i have a propety:
public bool HasAccess{get;set;}
and would like to do something like this:
[MyAttribute]
public bool HasAccess{get;set;}
And MyAttribute should set the value of HasAccess. My question is how to build MyAttribute to where I will Set the value of HasAccess to what ever value is given to it inside the attribute.
Upvotes: 0
Views: 1316
Reputation: 941287
Attributes are associated with a type. You find this one through Type.GetProperty() and PropertyInfo.GetCustomAttributes(). That doesn't help you set the property value of an object. Unless HasAccess is static, it doesn't look it. Or in other words, you won't be able to supply the value for the first argument of PropertyInfo.SetValue().
If you already have the object reference then it isn't a problem, use its GetType() method and pass the object to SetValue. But that's code elsewhere, not in the attribute class.
Upvotes: 2