Reputation: 1809
I have the following class:
public partial class ct_ServiceProductInfoWireless {
private object[] itemsField;
private bool directFulfillmentField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("NewWirelessLines", typeof(ct_WirelessLines))]
[System.Xml.Serialization.XmlElementAttribute("WirelessLine", typeof(ct_ServiceProductInfoWirelessWirelessLine))]
public object[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
}
ct_WirelessLines Class
public partial class ct_WirelessLines {
private ct_NewWirelessLine[] wirelessLineField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("WirelessLine")]
public ct_NewWirelessLine[] WirelessLine {
get {
return this.wirelessLineField;
}
set {
this.wirelessLineField = value;
}
}
}
The problem is when I serialize my ct_ServiceProductInfoWireless Object I get following Exception:
The type ct_WirelessLines[] may not be used in this context.
This is the serialization code:
var stringWriter = new System.IO.StringWriter();
var serializer = new XmlSerializer(objectToSerialize.GetType());
serializer.Serialize(stringWriter, objectToSerialize); //Getting exception here
return stringWriter.ToString();
What is wrong?
ct_NewWirelessLine class
public partial class ct_NewWirelessLine {
private ct_NewRatePlan ratePlanField;
private ct_DataPlan dataPlanField;
private ct_OrderDevice deviceField;
private ct_PhoneNumber telephoneNumberField;
private string lineNumberField;
private bool isPrimaryLineField;
}
ct_ServiceProductInfoWirelessWirelessLine class
public partial class ct_ServiceProductInfoWirelessWirelessLine {
private ct_Device deviceField;
private ct_RatePlan ratePlanField;
private ct_InstallmentPlan installmentPlanField;
private string paymentArrangementField;
private ct_OptionalFeature[] optionalPackagesField;
private ct_WirelessFeature[] optionalFeaturesField;
private string lineNumberField;
private string primarySQNumberField;
private string secondarySQNumberField;
private st_CustomerType customerTypeField;
private bool customerTypeFieldSpecified;
private string ppuZipField;
private string wtnField;
private bool directFulfillmentField;
private bool directFulfillmentFieldSpecified;
}
Upvotes: 4
Views: 1674
Reputation: 116751
I was able to reproduce the error message
System.InvalidOperationException: The type ct_WirelessLines[] may not be used in this context.
by assigning ct_ServiceProductInfoWireless.Items
to be an array of array of ct_WirelessLines
instead of a 1-dimensional array:
Items = new [] { new [] { new ct_WirelessLines() } }`
This code compiles because Items
is just an array of objects, but won't serialize because XmlSerializer
expects only objects of the two types declared in XmlElementAttribute.Type
attributes to be in the array, and not some nested array. Demo fiddle #1 here.
The solution would be to not do that and use a flat 1d array:
Items = new [] { new ct_WirelessLines() },
Demo fiddle #2 here.
If you have two collections, one of ct_WirelessLines
and one of ct_ServiceProductInfoWirelessWirelessLine
, you can combine them into the final Items
array by using upcasting the collection contents to object
, then using Concat()
and ToArray()
:
var wirelessLines = GetWirelessLines(); // Some IEnumerable<ct_WirelessLines>
var productInfo = GetProductInfoWirelessWirelessLines(); // Some IEnumerable<ct_ServiceProductInfoWirelessWirelessLine>
var objectToSerialize = new ct_ServiceProductInfoWireless
{
Items = wirelessLines.Cast<object>().Concat(productInfo).ToArray(),
};
Demo fiddle #3 here.
As an aside, it looks as though this code was auto-generated by xsd.exe
. This tool generally will not automatically generate a class hierarchy for sequences of choice elements. However, if you can manually modify the generated code, you could introduce an abstract base class for your Items
which will prevent the sort of error you encountered at compile time:
public partial class ct_ServiceProductInfoWireless
{
private WirelessItemBase[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("NewWirelessLines", typeof(ct_WirelessLines))]
[System.Xml.Serialization.XmlElementAttribute("WirelessLine", typeof(ct_ServiceProductInfoWirelessWirelessLine))]
public WirelessItemBase[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
}
public abstract class WirelessItemBase
{
}
public partial class ct_WirelessLines : WirelessItemBase
{
}
public partial class ct_ServiceProductInfoWirelessWirelessLine : WirelessItemBase
{
}
Now the following won't even compile:
Items = new [] { new [] { new ct_WirelessLines() } }, //Compilation error (line 72, col 13): Cannot implicitly convert type 'ct_WirelessLines[][]' to 'WirelessItemBase[]'
Demo fiddle #4 here.
Upvotes: 2