convert dynamically object to an array of dynamic type in c# -


i have 2 types :

    public class type1     {         public string name { get; set; }     }      public class type2     {         public string name { get; set; }     } 

i have list of elements (each element object type). elements array. (an array type1[] or type2[])

my goal :
1-iterate on list of elements
2-determine type1[]array pr type2[] array
3-get name value property element of previous array

this have done :

    foreach (var myobject in mylist)     {         if (myobject.gettype().isarray)         {             var elementtype = myobject.gettype().getelementtype()// should me return element type, ie type1 or type2              //this stuck, know object array cannot cast if in type1[] or type2[] array using elementtype             //the following not working             elementtype[] myarrat = (elementtype[])myobject;              // , don't want hardwrite each case each possible type :             type1[] myarrat = (type1[])myobject;             type2[] myarrat = (type2[])myobject;             // want use elementtype got          }     } 

thanks in advance help.

you can't trying do. , quite frankly, don't need either. if expecting different types means going different things each type. can change type1 , type2 extend same base class , use base class instead:

public class typebase  {    public virtual string name { get; set; } }  public class type1 : typebase { }  public class type2 : typebase { }   foreach (var myobject in mylist) {     if (myobject.gettype().isarray)     {         object[] array = (object[]) myobject;         typebase[] yourarray = array.cast<typebase>();         //use properties , methods of typebase instead of type1 , type2         //mark methods , properties in typebase virtual ,         //override them on type1 , type2 if needed     } } 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -