c# - Cant evaluate types of IList when looping properties -
i have class properties on it. call testmecommand (see below). class has list on it. need loop on properties of class, , identify list. has built generically because code validation, same code might need identify list<int>
or list<string>
, or else.
public class testmecommand { [range(1, int32.maxvalue)] public int theint { get; set; } [required] [stringlength(50)] public string thestring { get; set; } [listnotemptyvalidator] public list<testlistitem> mylist { get; set; } public class testlistitem { [range(1, int32.maxvalue)] public int listint { get; set; } } }
now problem have code looks this:
foreach (var prop in this.gettype().getproperties()) { if (prop.propertytype.fullname.startswith("system.collections.generic.list")) { ilist list = prop.getgetmethod().invoke(this, null) ilist; } }
i dont want put string in there, if prop.propertytype ilist never evaluates true. how fix it?
i might use:
if(typeof(ilist).isassignablefrom(prop.propertytype)) {...}
which covers implementing ilist
.
the reason prop.propertytype ilist
never evaluates true asking "does type
object implement ilist
?", rather "does type represented type
object implement ilist
?".
Comments
Post a Comment