473,396 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Creating a strongly typed object from a basic serialized class

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.

Nov 7 '06 #1
3 2018
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:
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.
Nov 7 '06 #2
Martin,

Thanks for the reply, do you mean implement some sort of CopyTo() method?

This must be possible as Microsoft has done it in CRM 3.0 SDK.

Regards
Simon.

"Martin Z" wrote:
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:
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.

Nov 7 '06 #3
I haven't touched CRM 3.0, but I imagine that it must be done manually
(or from some hefty code-generation). Personally, I find the
polymorphic solution using the native functionality of the
XmlSerializer to be better.

Thought of another, non-polymorphic way to do it that's a little less
boilerplate - skipping how you'd determine the type to construct, since
that's really up to you (one app I worked on just had the full
type/assembly reference included as a member). The approach works like
this: you make all your specific classes wrappers for the basic,
general object. The specific classes have no fields, they read
everything from the base. Then you only have to write one copy
constructor - the base object. You just write properties for all the
members of the specific classes that redirect into the collections of
the base object. Probably it'd be best to include code in the base
class that lazily constructs a dictionary out of the properties array
to make those specific-class-properties less painful.

Simon Hart wrote:
Martin,

Thanks for the reply, do you mean implement some sort of CopyTo() method?

This must be possible as Microsoft has done it in CRM 3.0 SDK.

Regards
Simon.

"Martin Z" wrote:
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:
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.
Nov 7 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mark | last post by:
Just wanted to confirm that my understanding of a strongly typed language is correct: 1. .NET is a strongly typed language because each variable / field must be declared a specific type (String,...
3
by: MC D | last post by:
I have a situation in which I have a strongly typed dataset based on the schema of a xml document. This works great, and OMG I love strongly typed datasets... ;o) However, The situation I have...
2
by: Mark | last post by:
Assume you have a strongly typed collection of a class called Person. The strongly typed collection implements IEnumerable so it can be databound to a server control like a DataGrid. The Person...
1
by: Andre Ranieri | last post by:
I'm designing an extranet web site that customers will use to log in and see their account history, make payments against their balance, etc. I've declared a strongly typed DataSet as a public...
4
by: Michael K. Walter | last post by:
I'd like to create a strongly-typed collection of objects that are indexed by a string value (key is a string). I'd also like to allow users to iterate over the collection using a For-each loop...
0
by: Jordan Bowness | last post by:
I make a similar post in another newsgroup, but this example is simplified somewhat. I have a component (cmpMyComponent) with 2 properties. The 1st property is a string value (Description) and...
20
by: Dennis | last post by:
I use the following code for a strongly typed arraylist and it works great. However, I was wondering if this is the proper way to do it. I realize that if I want to implement sorting of the...
5
by: Harold Howe | last post by:
I am having a problem deserializing objects from a library when the following conditions exist: 1- The library is strongly named 2- The serialized file was created with version 1.0 of the...
4
by: Rachana | last post by:
Hi, I have understood Data Sets but what is meant by typed/untyped/ strongly typed datasets. Can any one explain me or suggest any site/ article, to get these concepts (and their ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.