c# - Auto detect the type of reference property of a given class object -
given class this
public class employee { public employee() { children = new list<child>(); } public virtual string firstname { get; set; } public virtual string lastname { get; set; } public virtual employeecard employeecard { get; set; } public virtual ilist<child> children { get; protected set; } }
if have object of above class, how determine if employeecard
property object or list of object @ run time? possible?
check see if type of property implements ienumerable
:
bool iscollection = typeof(employee).getproperty("employeecard") .propertytype .getinterface("ienumerable") != null;
this should work generic , non-generic collection types, including ilist<t>
Comments
Post a Comment