hrabia Posted July 15, 2003 Posted July 15, 2003 I have one base class. This class is used as parameter for method that serializes an object of that class to xml (using XmlSerializer). class Base {.......} class X { public void Serialize(Base obj) { XmlSerializer _o = new XmlSerializer(typeof(Base)); ................... } } I have second class that derives from base class. class Second : Base {.......} When I use object of second class as parameter for my method I need a type of my object for constructor of XmlSerializer(typeof(Second)). Serialize(new Second()); But my method know only type of base class, and only XmlElemnts of base class are serialized. How can get this type dynamicaly? Quote A man and a dog have an average of three legs. Beaware of Statistics.
*Experts* Volte Posted July 15, 2003 *Experts* Posted July 15, 2003 Are you making sure to set the [serializable()] attribute of both the base and the inherited class? i.e. [serializable()] public class Base { // etc } [serializable()] public class Second : Base { // etc } Quote
hrabia Posted July 16, 2003 Author Posted July 16, 2003 Hmm, I use only [XmlElement] for properties. It has worked fine until inheritance... Quote A man and a dog have an average of three legs. Beaware of Statistics.
_SBradley_ Posted July 16, 2003 Posted July 16, 2003 Instead of using typeof(Second), just call obj.GetType(). :) Quote
hrabia Posted July 16, 2003 Author Posted July 16, 2003 obj.GetType() - I got errors when I used this function. I think it shoud be overriden. Quote A man and a dog have an average of three legs. Beaware of Statistics.
_SBradley_ Posted July 16, 2003 Posted July 16, 2003 What sort of errors? GetType() should never cause any errors. And it cannot be overriden, because it is not virtual in Object. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.