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

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -