473,216 Members | 1,302 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,216 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 2008
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 ...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...

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.