How can I get object type?

hrabia

Centurion
Joined
May 21, 2003
Messages
107
Location
Hamburg, Germany
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?
 
Are you making sure to set the [Serializable()] attribute of both the base and the inherited class?

i.e.
C#:
[Serializable()]
public class Base {
  // etc
}

[Serializable()]
public class Second : Base {
  // etc
}
 
Back
Top