I've gone through the whole process recently... pretty much the only
way to do it the way you have in mind is to use the copy constructor
and do it manually - that is, the basic object provides methods to
convert the object into a number of other object types, and you just do
conditional code based on whichever of those is valid.... or, for a
more clean, internally polymorphic implementation, you move the
conditional logic inside the instances.
Alternately, you can let the XmlSerializer do the polymorphism for you,
but then the methodology is a little different. Define your specific
implementations as subclasses of the general (possibly abstract) base
class. Then use the XmlIncludeAttribute on the base class to define
the allowed subclasses. These subclasses are then referenced using the
xsi:type="" element... so the type isn't inferred, as in your case, but
explicitly mentioned.
Simon Hart wrote:
Quote:
Hi,
>
I am trying to implement some functionality as seen in MS CRM 3.0 whereby a
basic Xml is deserialized into an object which contains properties. What I
want to do from here is; cast the basic object into a more strongly typed
class which knows what properties to expect.
>
The basic top level class is as follows:
>
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Collections;
>
namespace MessageOutgoing
{
[XmlRoot(Namespace="")]
/// <summary>
/// Describes the Message object.
/// </summary>
public class Message
{
private MessageType _type;
private MessageSubType _subType;
private ArrayList properties = new ArrayList();
>
/// <summary>
/// Gets or sets the MessageType enumeration.
/// </summary>
[XmlAttribute("Type")]
public MessageType MessageType
{
get
{
return _type;
}
set
{
_type = value;
}
}
>
/// <summary>
/// Gets or sets the type of message ie: ID01 - Reset request.
/// </summary>
[XmlAttribute("SubType")]
public MessageSubType MessageSubType
{
get
{
return _subType;
}
set
{
_subType = value;
}
}
>
/// <summary>
/// Gets or sets the properties collection. A properties collection
/// is a collection of property objects that make up a record.
/// </summary>
[XmlElement("Properties")]
public Properties[] Properties
{
get
{
Properties[] propertiesObj = new Properties[properties.Count];
properties.CopyTo(propertiesObj);
return propertiesObj;
}
set
{
if (value == null)
return;
>
Properties[] propertiesObj = (Properties[])value;
properties.Clear();
foreach (Properties property in propertiesObj)
properties.Add(property);
}
}
}
>
/// <summary>
/// Describes a collection of property objects.
/// </summary>
public class Properties
{
private ArrayList properties = new ArrayList();
>
/// <summary>
/// Gets or sets the list of property objects for this Properties
object.
/// </summary>
[XmlElement("Property")]
public Property[] Property
{
get
{
Property[] propertiesObj = new Property[properties.Count];
properties.CopyTo(propertiesObj);
return propertiesObj;
}
set
{
if (value == null)
return;
>
Property[] propertiesObj = (Property[])value;
properties.Clear();
foreach (Property property in propertiesObj)
properties.Add(property);
}
}
}
>
/// <summary>
/// Describes a property object.
/// </summary>
public class Property
{
private string _name = string.Empty;
private string _value = string.Empty;
>
/// <summary>
/// Gets or sets the name of the property.
/// </summary>
[XmlAttribute("Name")]
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
>
/// <summary>
/// Gets or sets the value of the property.
/// </summary>
[XmlAttribute("Value")]
public string Value
{
get
{
return _value;
}
set
{
_value = value;
}
}
}
}
>
This looks like this when serialied into Xml:
>
>
<Message Type="System" SubType="ID01">
<Properties>
<Property Name="PhoneNumber" Value="0013343222>
</Property>
</Properties>
</Message>
>
What I want in the above case is I know ID01 can be deserialied into ID01
class so this class looks for a property name of "PhoneNumber" and sets the
PhoneNumber property to this value.
>
Does anyone know how to achieve this?
>
Regards
Simon.