Inclass conversion from base class to derived class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • brboLikus
    New Member
    • Mar 2010
    • 5

    #1

    Inclass conversion from base class to derived class

    Hello everybody!

    My problem is somewhat strange since I can't think of any normal situation where one would like to do what I need, but here it goes. I need to somehow cast a base class to a derived class, but from the base-class code. I would need to do something like this:

    Code:
    class Base
    {
        public Base() { }
        public void Convert()
        {
            this = new Derived(); 
            // this, of course, doesn't work
        }
    }
    class Derived : Base
    {
        public Derived() { }
    }
    Now, the reason I need to do this is because I need to serialize a list that contains the Base class type objects, but I don't know what do the derived classes look like. All I know is that they have a parameter-less constructor, their full names and the DLL they are in. This gives me the possibility to use the Activator function and fill my list up. But I cannot write the functions for the base class that the IXmlSerializabl e interface demands because to do so I must initialize the base class as a derived class of a specific type, but within the base class itself. I do know that I can use wrappers for the data, and then fill the list using the Activator class, but since I use the base class extensively throughout my project, it would be a tough thing to do..

    So, any suggestions are more than welcome.
Working...