mardi 12 janvier 2016

Cast array of unknowClass to array of otherClass in c#

    public abstract class tObject
    {
       public tObject[] someMthode(){;}
    }
    public  class myClass : tObject
    {
       public string Oth0 { get; set; }
       public string Oth1 { get; set; }
       public string Oth2 { get; set; }
    }

I want

 myClass mc=new myClass();
 tObject to=mc;
 myClass[] mcArray=(myClass[])mc.someMthode();//System.InvalidCastException
 //Unable to cast object of type 'tObject[]' to type 'myClass[]'

but when check any element of mcArray is correct

if (mcArray[0] is myClass)
{
   //return true and run this ;
}

In fact I want cast when a method return array of tObject according to the base class :

 subClass[] mcArray=(subClass[])instanceOfsubClass.someMthode()





Aucun commentaire:

Enregistrer un commentaire