From: Marouane B. J. <mar...@ya...> - 2010-06-07 07:56:56
|
Ok My example was not the reality, here a better description of the case: I have a c++ method: Object getObject(int id); I call for C# this method using the id of a Root. So I get my Object in C#. And then I do the Cast: here is the C# code: Object obj = FROMCPP.getObject(1); Container container = (Container)obj; !!! THIS CAST CRASH: InvalidCastException Regards, Marouane --- En date de : Ven 4.6.10, David Piepgrass <dpi...@me...> a écrit : > De: David Piepgrass <dpi...@me...> > Objet: RE: [Swig-user] Casting in C# objects created in C++ > À: "Marouane BEN JELLOUL" <mar...@ya...> > Date: Vendredi 4 juin 2010, 18h28 > > Here is the class Hierarchy > > Abstract Object > > Container : Object > > Container3D<T> : Container > > Root : Container3D<BLABLA> > > > ... > > > > Root root = new Root(); > > Object obj = root; > > Container container = (Container)obj; > > !!! THIS CAST CRASH: InvalidCastException > > You claim that Root is derived from Container3DBLABLA and > Container3DBLABLA is derived from Container. If that were > true then you wouldn't NEED a cast from Root to Container. > You should look at the code generated by SWIG to find out > what it "thinks" the class hierarchy is. > > I don't think %rename is a problem. %template is a bit > tricky, you must use it in the right place. Make sure that > the line > > %template(Container3DBLABLA) > MyNameSpace::Container3D<MyNameSpace::BLABLA>; > > comes AFTER the definition of > MyNameSpace::Container3D<T>, MyNameSpace::BLABLA, > Container and Object but BEFORE the definition of Root. > > |