1

I have a chain of inheritance and want to get a base class instance. The code looks like this:

[Serializable()]
class MyBaseClass {
     public MyBaseClass() { /* Ctor implementation */}
     public MyBaseClass( someVar ) { /* Ctor implementation */}
     public MyBaseClass GetBaseInstance{ return( (MyBaseClass)this ); }
     // Base class methods & properties
}

class Level1Class : MyBaseClass {
    // Some methods
    // Some properties
    // Some objects
}

class Level2Class : Level1Class {
    // Some more methods
    // Some more properties
    // Some more objects
}

At some point in my code I have access to an instance of Level2Class but I need to get an instance from MyBaseClass. I need a strict MyBaseClass instance since I (try to) feed that instance to an XML serializer. The XML serialization is defined for MyBaseClass but not for Level1Class and Level2Class.

I tried a couple of things:

var TheInstance = Level2ClassInstance as MyBaseClass; // return a Level2Class instance
var TheInstance = Level2ClassInstance.GetBase();      // return a Level2Class instance

// The serializer code
FileInfo fobject = new FileInfo( "MyXMLFile.xml" );
XmlSerializer MySerializer = new XmlSerializer( TheInstance.GetType() );
using( StreamWriter xmlStream = new StreamWriter( fobject.FullName ) ) {
    MySerializer.Serialize( xmlStream, TheInstance );
}

I guess I am missing out on something obvious... In this situation how can I get an instance of MyBaseClass? Is it required to implement copy constructors for every class?

Any pointers would be greatly appreciated...

Edit 1: I added the serializer code. I also found out that in Level2Class and Level1Class some types of objects give problems. In particular Generic dictionaries. I found a work around by making Level1Class & Level2Class serialisable (using [Serializable()]) and then explicitely ignore the dictionaries (using [XmlIgnore]). This works but has a downsides: The XML file now contains the class type Level2Class (with the content of MyBaseClass).

TocToc
  • 4,739
  • 4
  • 29
  • 34
  • 1
    Derived classes _are_ instances of their base classes. You're misunderstanding inheritance; this is impossible and unnecessary. – SLaks Mar 19 '14 at 22:00
  • 1
    Once created, an object cannot change its type. Even though you can treat it as if it were an instance of `MyBaseClass`, It will always be a `Level2ClassInstance`. – dcastro Mar 19 '14 at 22:00
  • Your question doesn't really make sense. It is an instance of the class.. if you want _just_ the base.. then yes, you'll have to define a conversion method/operator overload. – Simon Whitehead Mar 19 '14 at 22:01
  • 1
    Have you tried feeding that object to the XML serializer? It should work fine, but if for some reason it doesn't, then yes, you'll have to [clone](http://stackoverflow.com/q/78536/857807) the object. – dcastro Mar 19 '14 at 22:01
  • You have to clone the object. Any object is of the base type too, so just re-assignment does not change its type... or, just implement ISerializable interface by the base type only (not tested this one). – Tengiz Mar 19 '14 at 22:02
  • @dcastro: Yes actually, feeding is not ok so I was trying to pass the proper instance to it. – TocToc Mar 19 '14 at 22:11
  • @Tengiz: Cloning is of course an option but I am trying to search alternative ways. – TocToc Mar 19 '14 at 22:11
  • @SimonWhitehead: That is pretty much what I was trying to avoid since the classes are quite complexe. – TocToc Mar 19 '14 at 22:12
  • @TocToc Out of curiosity, would you mind showing us the call to the serializer? – dcastro Mar 19 '14 at 22:13
  • Perhaps a library like Automapper or ValueInjecter could be of use to you. – Simon Whitehead Mar 19 '14 at 22:13
  • @SimonWhitehead: I'll give try to Automapper, it seems like it would be suited here. – TocToc Mar 20 '14 at 08:22

0 Answers0