In my application, I have three collection objects which store data. The data which populates these collections is downloaded from an XML file on the web.
The three data classes are very simple, the following is a typical example:
[SerializableAttribute()]
[XmlTypeAttribute(AnonymousType = true)]
[XmlRootAttribute(Namespace = "", IsNullable = false, ElementName = "companies")]
public partial class CompanyList
{
private List<Company> itemsField = new List<Company>();
[XmlElement("company", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public List<Company> Items
{
get { return this.itemsField; }
}
}
[SerializableAttribute()]
[XmlTypeAttribute(AnonymousType = true)]
public partial class Company
{
private int companyIdField;
private string companyNameField;
[XmlElement(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, ElementName = "companyid")]
public int CompanyID
{
get { return this.companyIdField; }
set { this.companyIdField = value; }
}
[XmlElement(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, ElementName = "companyname")]
public string CompanyName
{
get { return this.companyNameField; }
set { this.companyNameField = value; }
}
}
In order to download the data for these objects from the web, I have written an asynchronous web client which will take a URI, downloads the data, then fires an event handler with the downloaded data passed as a string within DownloadCompleteEventArgs. When calling the constructor for this web client, I pass the one of the empty objects for the data to be de-serialized into as a object parameter - this is passed between the async methods via a custom class.
This is where I'm running into difficultly. In the event handler, I want to take the string and de-serialize it into the appropriate object. However, although the de-serialization works fine, the original object isn't modified - presumably because I am working on a copy of the object.
I've tried passing refs to the original constructor and between the custom classes, but the compiler won't let me cast my type to "ref object", and I'd like to keep the web download/de-serialization code type-agnostic. In addition, I'm getting a 'feeling' that I'm going way off track, and it's actually my choice of design pattern that's at fault.
So, in summary, what is the best approach for creating a 'helper' class which can populate any one of a variety of objects of different types from a suitable asynchronously downloaded string of xml data?
EDIT: Adding some further context: I need to deserialize an XML string into an object from an asynchronous callback. For example, I might make three calls to DownloadXMLAsync(), which is a method which calls DownloadCompleted(DownloadCompletedEventArgs) when it is done. Each of the three calls return data to populate three different objects. How can I reliably pass a reference to the object through the asynchronous calls so that the DownloadCompleted() method can correctly populate the right object in each case?
I tried defining DownloadXMLAsync(ref object objectToPopulate), then passing the objectToPopulate in the state object within the call to HttpWebRequest.BeginGetResponse(), but I get "cannot convert from 'ref TicketSpaceSiteServer.CompanyList' to 'ref object'".