I have types provided in a library I cant modify, such as this:
namespace BaseNamespace
{
public class A
{
public string Foo { get; set; }
}
}
I also have a class named SomeOtherNamespace.A" that derives from BaseNamespace.A like this:
namespace SomeOtherNamespace
{
public class A : BaseNamespace.A
{
public string DoSomething() {}
}
}
From a web service I receive an XML payload with in it.
I want to deserialize the XML so that I end up with a SomeOtherNamespace.A object. However when I run the following code
string xml = "<A Foo=\"test\"></A>";
XmlSerializer serializer = new XmlSerializer(typeof(A));
StringReader reader = new StringReader(xml);
A obj = serializer.Deserialize(reader) as A;
I get an error:
Types 'BaseNamespace.A' and 'SomeOtherNamespace.A' both use the XML type name, 'A', from namespace ''. Use XML attributes to specify a unique XML name and/or namespace for the type.
Question: Without modification of the class BaseNamespace.A how can I force deserialization to my derived type SomeOtherNamespace.A?