PC Review


Reply
Thread Tools Rate Thread

Xml Serialization, properties and 'new'

 
 
Craig Vermeer
Guest
Posts: n/a
 
      21st Jul 2006
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;}
}
}


}
 
Reply With Quote
 
 
 
 
Dave Sexton
Guest
Posts: n/a
 
      23rd Jul 2006
Hi Craig,

In this thread from 2003 a Microsoft rep provides an answer:

http://groups.google.com/group/micro...973931d9061f37

--
Dave Sexton

"Craig Vermeer" <vermeecaATsentDOTCOM.@nospam> wrote in message news:(E-Mail Removed)...
> 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;}
> }
> }
>
>
> }



 
Reply With Quote
 
Craig Vermeer
Guest
Posts: n/a
 
      23rd Jul 2006
There we go... thanks!

Dave Sexton wrote:
> Hi Craig,
>
> In this thread from 2003 a Microsoft rep provides an answer:
>
> http://groups.google.com/group/micro...973931d9061f37
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
xml serialization including properties type ginnone@gmail.com Microsoft Dot NET Framework 0 5th Jun 2008 01:20 PM
Serialization of derived List<T> properties swiftalpha@gmail.com Microsoft C# .NET 1 29th May 2008 12:05 PM
Serialization Problem: custom properties of a derived DataSet class not serialized Michael Microsoft ADO .NET 13 28th Nov 2005 03:43 AM
.NET 2.0 (RTM) serialization does not thror exception for missing/changed properties! Tuncay Baskan Microsoft C# .NET 3 2nd Nov 2005 01:17 AM
Re: Handling resetting/serialization of expanding properties in the property window walter leinert Microsoft Dot NET Framework Forms 0 1st Jul 2003 10:04 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:48 PM.