c# - Convert generic object list to non-generic type list -


i trying convert list of system.object objects list of typed objects.

here error getting:

object of type 'system.collections.generic.list`1[system.object]' cannot converted type 'system.collections.generic.list`1[testapp.tsc_mrc_step]'.

the purpose because writing business data layer project have name class , properties same name entities in database , data layer automatically populate referenced tables types declared in class.

the business data layer uses reflection, generics , objects deal of this.

below code tried put list of objects list of known types. thing is, object known type pass object....how convert known type without knowing is?

            bool iscoollection = false;             type t = gettypeinsideofobjectbytypename(o, tablename, out iscoollection);              list<object> objectcoll = new list<object>();              object obj = activator.createinstance(t);             if (obj != null)             {                 propertyinfo[] objectprops = obj.gettype().getproperties();                 foreach (propertyinfo op in objectprops)                 {                     if (hascolumn(reader, op.name))                     {                         op.setvalue(obj, reader[op.name]);                     }                 }                  if (iscoollection)                 {                     objectcoll.add(obj);                 }             }              if (iscoollection)             {                 ienumerable<object> objs = objectcoll.asenumerable();                  setobject(o, objs);             }             else             {                 setobject(o, obj);             } 

here setobject:

            public static void setobject(object parentobject, object newobject)             {                 propertyinfo[] props = parentobject.gettype().getproperties();                 string typename = newobject.gettype().name;                 foreach (propertyinfo pi in props)                 {                     if (pi.propertytype.name.tolower() == typename.tolower())                     {                         pi.setvalue(parentobject, newobject);                     }                     else if (!pi.propertytype.isvaluetype && !pi.propertytype.namespace.tolower().contains("system"))                     {                         setobject(pi.getvalue(parentobject), newobject);                     }                 }             } 

if know of values of required type in list:

list<object> objects; list<cat> cats = objects.cast<cat>().tolist(); 

if not values of type, , want weed out ones aren't:

list<object> objects; list<cat> cats = objects.oftype<cat>().tolist(); 

both require linq.

if don't know type until runtime, have use reflection.

how call generic method through reflection


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -