XmlSerializer possui erro com Array de List

Um erro desses deve afetar poucas pessoas, mas na versão atual do .NET Framework 4.5.1 ocorre um erro de object reference ao executar o codigo a seguir:

       XmlSerializer serializaer = new XmlSerializer(typeof(List<string>[]));

Ao executar esse codigo ocorre um erro do tipo System.NullReferenceException, seguindo o Stack ocorre no metodo System.Xml.Serialization.CodeGenerator.GetVariableType. O Stack Completo do erro é assim:

 

at System.Xml.Serialization.CodeGenerator.GetVariableType(Object var)
at System.Xml.Serialization.SourceInfo.InternalLoad(Type elementType, Boolean asAddress)
at System.Xml.Serialization.XmlSerializationILGen.ILGenLoad(String source, Type type)
at System.Xml.Serialization.XmlSerializationReaderILGen.WriteMemberBegin(Member[] members)
at System.Xml.Serialization.XmlSerializationReaderILGen.WriteArray(String source, String arrayName, ArrayMapping arrayMapping, Boolean readOnly, Boolean isNullable, Int32 fixupIndex, Int32 elementIndex)
at System.Xml.Serialization.XmlSerializationReaderILGen.WriteElement(String source, String arrayName, String choiceSource, ElementAccessor element, ChoiceIdentifierAccessor choice, String checkSpecified, Boolean checkForNull, Boolean readOnly, Int32 fixupIndex, Int32 elementIndex)
at System.Xml.Serialization.XmlSerializationReaderILGen.WriteMemberElementsIf(Member[] members, Member anyElement, String elementElseString)
at System.Xml.Serialization.XmlSerializationReaderILGen.WriteMemberElements(Member[] members, String elementElseString, String elseString, Member anyElement, Member anyText)
at System.Xml.Serialization.XmlSerializationReaderILGen.WriteArray(String source, String arrayName, ArrayMapping arrayMapping, Boolean readOnly, Boolean isNullable, Int32 fixupIndex, Int32 elementIndex)
at System.Xml.Serialization.XmlSerializationReaderILGen.WriteElement(String source, String arrayName, String choiceSource, ElementAccessor element, ChoiceIdentifierAccessor choice, String checkSpecified, Boolean checkForNull, Boolean readOnly, Int32 fixupIndex, Int32 elementIndex)
at System.Xml.Serialization.XmlSerializationReaderILGen.WriteMemberElementsIf(Member[] members, Member anyElement, String elementElseString)
at System.Xml.Serialization.XmlSerializationReaderILGen.WriteMemberElements(Member[] members, String elementElseString, String elseString, Member anyElement, Member anyText)
at System.Xml.Serialization.XmlSerializationReaderILGen.GenerateTypeElement(XmlTypeMapping xmlTypeMapping)
at System.Xml.Serialization.XmlSerializationReaderILGen.GenerateElement(XmlMapping xmlMapping)
at System.Xml.Serialization.TempAssembly.GenerateRefEmitAssembly(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, Evidence evidence)
at System.Xml.Serialization.TempAssembly..ctor(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, String location, Evidence evidence)
at System.Xml.Serialization.XmlSerializer.GenerateTempAssembly(XmlMapping xmlMapping, Type type, String defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type)
at MeuPrograma.Program.Main(String[] args) in d:\MeuPrograma\Program.cs:line 47
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

Porém encontrar o erro assim é muito simples, mas a serialização de XML como mostrada é utilizada em diversos outros lugares como WebAPI e WebServices(Asmx). No meu caso encontrei esse erro em um Asmx graças a esse link(que está em Frances) consegui identificar que um metodo estava retornar um objeto com Array de Lista e foi identificado o problema. Não é permitido utilizar array de List como parametro ou retorno de metodos ASMX pois pode ocrrer esse problema na serialização do cliente.

Para resolver esse problema a unica possibilidade é transformar este Array de List em um Objeto, ficando até mais organizado, ou se quiser deixar porcamente pode ser utilizado um List de List que o XmlSerializer vai funcionar.

Anúncios