XmlInclude: lista e matriz
Eu tenho um objeto que tem variáveis comoobject
e quero serializá-lo em XML.
Para fazer isso, adicionei algunsXmlInclude
atributos para gerenciar todos os tipos que podem ser usados.
[Serializable]
[XmlInclude(typeof(short[]))]
[XmlInclude(typeof(ushort[]))]
[XmlInclude(typeof(int[]))]
[XmlInclude(typeof(uint[]))]
[XmlInclude(typeof(ulong[]))]
[XmlInclude(typeof(long[]))]
[XmlInclude(typeof(byte[]))]
[XmlInclude(typeof(decimal[]))]
[XmlInclude(typeof(float[]))]
[XmlInclude(typeof(double[]))]
[XmlInclude(typeof(string[]))]
[XmlInclude(typeof(List<short>))]
[XmlInclude(typeof(List<ushort>))]
[XmlInclude(typeof(List<int>))]
[XmlInclude(typeof(List<uint>))]
[XmlInclude(typeof(List<long>))]
[XmlInclude(typeof(List<ulong>))]
[XmlInclude(typeof(List<byte>))]
[XmlInclude(typeof(List<decimal>))]
[XmlInclude(typeof(List<float>))]
[XmlInclude(typeof(List<double>))]
[XmlInclude(typeof(List<string>))]
[XmlInclude(typeof(MyObject))]
[XmlInclude(typeof(TimeSpan))]
[XmlInclude(typeof(OtherObject))]
[XmlInclude(typeof(MySubObject1))]
[XmlInclude(typeof(MySubObject2))]
[XmlRoot(ElementName = "mc")]
public class MyClass: IComparable
{
[XmlElement("fm")]
public object FirstMember;
[XmlElement("sm")]
public object SecondMember;
[XmlElement("tm")]
public object ThirdMember;
}
Meu problema é que as declarações de matriz e lista não coexistem.E o mais estranho é que, se os atributos da matriz forem colocados primeiro, os membros da matriz serão serializados corretamente, mas não os da lista. E vice versa.
As classes personalizadas e derivadas funcionam bem, masList
eArray
não. Só consigo encontrar exemplos com classes, mas uso tipos primitivos.
Alguém tem alguma ideia ?
P.S .: Eu sei que minha postagem é semelhante aeste, mas não tem resposta desde 2011.