How nullable adapter properties work
masterSince XmlSerializer does not natively support .NET nullable types for value types, the generator can create an 'adapter' pattern to signal the presence or absence of a value.
When --nullable is enabled, the generator produces:
- A raw value property (e.g.,
int IdValue) decorated with[XmlIgnore]. - A
Specifiedboolean property (e.g.,bool IdValueSpecified) to signal serialization. - A nullable wrapper property (e.g.,
int? Id) that acts as an interface for the user, internally managing theIdValueandIdValueSpecifiedproperties.
Example Output:
[System.Xml.Serialization.XmlAttributeAttribute("id", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, DataType="int")]
public int IdValue { get; set; }
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool IdValueSpecified { get; set; }
[System.Xml.Serialization.XmlIgnoreAttribute()]
public System.Nullable<int> Id
{
get { return this.IdValueSpecified ? this.IdValue : (int?)null; }
set
{
this.IdValue = value.GetValueOrDefault();
this.IdValueSpecified = value.HasValue;
}
}// Example of the generated pattern
[System.Xml.Serialization.XmlAttributeAttribute("id", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, DataType="int")]
public int IdValue { get; set; }
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool IdValueSpecified { get; set; }
[System.Xml.Serialization.XmlIgnoreAttribute()]
public System.Nullable<int> Id
{
get { return this.IdValueSpecified ? this.IdValue : (int?)null; }
set
{
this.IdValue = value.GetValueOrDefault();
this.IdValueSpecified = value.HasValue;
}
}