Xml Serialization, properties and 'new'

C

Craig Vermeer

Hi all,

I'm hoping that someone has seen this before. I wasn't able to find any
solutions through my usual channels (I.E Google). I also tried the
Microsoft.Public.DotNet.Xml group a couple days ago, with no response.
I figure I'll give it one more shot.

I'm getting the following exception in certain situations when using the
Xml Serializer to serialize a class that has changed the type of a
property defined on its base class by using the 'new' keyword.

Anyone seen this before? I've tried using the XmlElement attribute as
the Exception suggests on the offending property to change the name,
with no impact.

Here's the exception :

System.InvalidOperationException: There was an error reflecting type
'XmlSerializerNewTest.class2'. ---> System.InvalidOperationException:
There was an error reflecting property 'prop'. --->
System.InvalidOperationException: Member class2.prop of type
XmlSerializerNewTest.inner2 hides base class member class1.prop of type
XmlSerializerNewTest.inner1. Use XmlElementAttribute or
XmlAttributeAttribute to specify a new name.
at
System.Xml.Serialization.StructMapping.FindDeclaringMapping(MemberMapping
member, StructMapping& declaringMapping, String parent)
at System.Xml.Serialization.StructMapping.Declares(MemberMapping
member, String parent)
at
<snip>

Here's some code that will reproduce the issue :

using System;
using System.Xml;
using System.Xml.Serialization;

namespace XmlSerializerNewTest
{
public class Class2
{
public static void Main(string[] args)
{
try
{
class2 test = new class2();
test.prop.name = "n";
test.prop.val = "v";
XmlSerializer ser = new XmlSerializer(typeof(class2));
ser.Serialize(Console.Out, test);
}
catch(Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
Console.ReadLine();
}
}

public class class1
{
private inner1 _prop = new inner1();
public inner1 prop
{
get{return _prop;}
set{_prop = value;}
}
}

public class class2 : class1
{
private inner2 _prop = new inner2();
public new inner2 prop
{
get{return _prop;}
set{_prop = value;}
}
}

public class inner1
{
private string _name;
public string name
{
get{return _name;}
set{_name=value;}
}
}

public class inner2 : inner1
{
private string _val;
public string val
{
get{return _val;}
set{_val = value;}
}
}


}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top