reflection - How do I retrieve attributes on properties in C#? -
i trying implement simple api user can dictate sorting of object properties property attribute.
something like:
[sorting(sortorder=0)] public string id { get; set; } in basic tostring() method, use reflection pull properties object.
type currenttype = this.gettype(); propertyinfo[] propertyinfoarray = currenttype.getproperties(bindingflags.public); array.sort(propertyinfoarray, this.comparer); i have written custom class using icomparer interface array.sort, once i'm in there stuck trying retrieve [sorting] attribute. have looks this:
propertyinfo xinfo = (propertyinfo)x; propertyinfo yinfo = (propertyinfo)y; i thought use xinfo.attributes, propertyattributes class not need do. have guidance how retrieve [sorting] attribute? have looked around lot, how overloaded word attribute in programming keep getting lot of false leads , dead ends.
use memberinfo.getcustomattributes
system.reflection.memberinfo info = typeof(student).getmembers() .first(p => p.name== "id"); object[] attributes = info.getcustomattributes(true); edit:
to value itself, have @ this answer.
good luck!
Comments
Post a Comment