here is my class to serialize/deserialize.
public class MyDic
{
...
[XmlElement(IsNullable = true)]
public List<WebDefinition> WebDefinitions;
...
}
and it’s a full definition of the struct WebDefinition.
public struct WebDefinition
{
public string Definition;
public string URL;
public WebDefinition(string def, string url)
{
Definition = def;
URL = url;
}
public override string ToString() { return this.Definition; }
}
i expected Dictionary.WebDefinitions can nullable when deserialzation. but it occured a runtime error when
//runtime error : System.InvalidOperationException
XmlSerializer myXml = new XmlSerializer(typeof(Dictionary), "UOC");
why i can’t use XmlElementAttribute.IsNullable?
note1:
when i delete a line [XmlElement(IsNullable = true)], it works properly with no error.(serialization and deserialization).
note2:
exception is System.InvalidOperationException and message is : "error occured in during reflection 'UOC.DicData' type"
thanks.
As I indicated in the comment “and in particular the InnerException”; since you didn’t add this, I ran the sample, and the innermost
InnerExceptionis:That tells you everything you need, I think. Changing it to
WebDefinition?(akaNullable<WebDefinition>does indeed fix it.But key point here: read the exception and the
InnerException.IMO, a better “fix” here is to make it a
class; sinceWebDefinitionis not a “value”, it has no business being astruct(and in particular a struct with public mutable fields is … evil):