c# - Accessing a property from its attribute -
is possible access type of property attribute has been implemented on property?
public class fooattribute : attribute { public string getpropertyname() { // return ?? } } public class bar { [fooattribute] public int baz { get; set; } }
i getpropertyname() return "baz".
sriram sakthivel correct not possible if using .net 4.5 can create workaround using callermembernameattribute
pass caller constructor of attribute, store , return getpropertyname
method:
public class fooattribute : attribute { public string propertyname { get; set; } public fooattribute([callermembername] string propertyname = null) { propertyname = propertyname; } public string getpropertyname() { return propertyname; } }
this pass caller (the property) constructor of attribute.
more details on callermembernameattribute
available on msdn.
Comments
Post a Comment