Search code examples
c#reflectioncloneobject-object-mapping

Cloning an instance of object to its base type


I have the following class:

public class AddCouponInfoRequest : namespace.Request 
{

}

I have an instance of AddCouponInfoRequest in my hand and I want to get an instance of namespace.Request with the same values.

This doesn't work fine:

namespace.Request req = (namespace.Request)request;
string xml = req.SerializeToXml();

The value of xml after serialization is:

<AddCouponInfoRequest xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\r\n \r\n \r\n ...

I need a pure namespace.Request object. What is the best way to do this?

Thanks in advance,


Solution

  • SerializeToXml is a virtual method so it is logical it always calls the overriden method.

    You can, for example, create a new method for AddCouponInfoRequest

    string SerializeToXmlAsParent()
    {
        return base.SerializeToXml();
    }