Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

A man and a dog have an average of three legs.

Beaware of Statistics.

  • *Experts*
Posted

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
}

Posted
Hmm, I use only [XmlElement] for properties. It has worked fine until inheritance...

A man and a dog have an average of three legs.

Beaware of Statistics.

Posted
obj.GetType() - I got errors when I used this function. I think it shoud be overriden.

A man and a dog have an average of three legs.

Beaware of Statistics.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...