asp.net - Cannot get attribute from PropertyInfo c# -
i have class , interface set this:
public partial interface inav_item { [fieldid("df918977-369c-4a06-ac38-adb8741b5f75")] string title {get; set;} } public partial class nav_item : inav_item { [fieldid("df918977-369c-4a06-ac38-adb8741b5f75")] public virtual string title {get; set;} } and have class inherited:
public class menuitem : nav_item { public virtual ienumerable<menuitem> children { get; set; } //some other properties } i'm trying instantiate object of type menuitem, , trying attributes inherited class (i can't instantiate menuitem directly, because type being passed in other classes)
object obj = activator.createinstance(type); foreach (propertyinfo propinfo in type.getproperties()) { fieldattribute sfi =(fieldattribute)propinfo.propertytype.getcustomattribute(typeof(fieldattribute)); } but giving me sfi null. i've debugged try getting attributes:
propinfo.propertytype.getcustomattributes() .. giving me system attributes (type , else), own attributes not there? because class inherited? how can attribute value?
edit:
the attribute class defined this:
public class fieldidattribute : attribute { private string _id; public fieldattribute(string id) { _id = id; } public id thefieldid { { return new id(_id); } } }
no, not inheritance problem. need change this:
fieldattribute sfi =(fieldattribute)propinfo.propertytype .getcustomattribute(typeof(fieldattribute)); to this:
var sfi = propinfo.getcustomattribute(typeof(fieldidattribute)) fieldidattribute; this because propertytype returns type of property, i.e. string in case. , type string not have custom attribute.
also edit of fieldidattribute class not correct. it's constructor not match class name , features undeclared & incorrect id type.
Comments
Post a Comment