EDN Admin
Well-known member
I recently received a requirement to take a class which has been binary serialized, and in one case serialize it as XML, while retaining the current binary serialization. The current object structure is as follows (I cannot change these classes)<br/>
<pre style="font-family:Consolas; font-size:13; color:black; background:white [Serializable]
<span style="color:blue public <span style="color:blue class <span style="color:#2b91af BaseClass
{
<span style="color:blue public IDictionary<<span style="color:blue string, SomeType> BadProperty { <span style="color:blue get; <span style="color:blue set; }
}
[Serializable]
<span style="color:blue public <span style="color:blue class <span style="color:#2b91af ImplementationClass : BaseClass
{
}[/code]
<br/>
As you can see, the base class contains a property that cannot be XML Serialized. My solution is to create a new class which inherits from the ImplementationClass, and is more XML friendly. In this class I can hide properties I do not need, and
wrap properties I do need with types that cannot be XML serialized<br/>
<pre style="font-family:Consolas; font-size:13; color:black; background:white
[Serializable]
<span style="color:blue public <span style="color:blue class <span style="color:#2b91af SerializedClass : ImplementationClass
{
[XmlIgnore]
<span style="color:blue public <span style="color:blue new IDictionary<<span style="color:blue string, SomeType> BadProperty {get; set;}
}[/code]
As you can see with this example, I am hiding the property with the new keyword, and using the XmlIgnore attribute to prevent it from being serialized.
However, when I instantiate the XmlSerializer with
<pre style="font-family:Consolas; font-size:13; color:black; background:white <span style="color:blue var s = <span style="color:blue new XmlSerializer(typeof(SerializedClass));<br/>
An exception is thrown: Cannot serialize member BaseClass.BadProperty of type... It shouldnt even be trying to serialize BadProperty. <br/> [/code]
View the full article
<pre style="font-family:Consolas; font-size:13; color:black; background:white [Serializable]
<span style="color:blue public <span style="color:blue class <span style="color:#2b91af BaseClass
{
<span style="color:blue public IDictionary<<span style="color:blue string, SomeType> BadProperty { <span style="color:blue get; <span style="color:blue set; }
}
[Serializable]
<span style="color:blue public <span style="color:blue class <span style="color:#2b91af ImplementationClass : BaseClass
{
}[/code]
<br/>
As you can see, the base class contains a property that cannot be XML Serialized. My solution is to create a new class which inherits from the ImplementationClass, and is more XML friendly. In this class I can hide properties I do not need, and
wrap properties I do need with types that cannot be XML serialized<br/>
<pre style="font-family:Consolas; font-size:13; color:black; background:white
[Serializable]
<span style="color:blue public <span style="color:blue class <span style="color:#2b91af SerializedClass : ImplementationClass
{
[XmlIgnore]
<span style="color:blue public <span style="color:blue new IDictionary<<span style="color:blue string, SomeType> BadProperty {get; set;}
}[/code]
As you can see with this example, I am hiding the property with the new keyword, and using the XmlIgnore attribute to prevent it from being serialized.
However, when I instantiate the XmlSerializer with
<pre style="font-family:Consolas; font-size:13; color:black; background:white <span style="color:blue var s = <span style="color:blue new XmlSerializer(typeof(SerializedClass));<br/>
An exception is thrown: Cannot serialize member BaseClass.BadProperty of type... It shouldnt even be trying to serialize BadProperty. <br/> [/code]
View the full article