dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.26k stars 4.73k forks source link

[Feature Request] Add xml serialization support for immutable arrays #66264

Open miu-na opened 2 years ago

miu-na commented 2 years ago

Currently, the xml serializer will throw an error when serializing immutable arrays containing objects of reference type. Is it possible to add xml serialization support for immutable arrays containing objects of reference type? Or how to implement this function?

Exception:

Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at System.Runtime.CompilerServices.CastHelpers.IsInstanceOfClass(Void*, System.Object)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterScene.Write6_Scene(System.String, System.String, SceneSerializer.Scene, Boolean, Boolean)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterScene.Write7_Scene(System.Object)
   at System.RuntimeMethodHandle.InvokeMethod(System.Object, System.Span`1<System.Object> ByRef, System.Signature, Boolean, Boolean)
   at System.Reflection.RuntimeMethodInfo.Invoke(System.Object, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo)
   at System.Reflection.MethodBase.Invoke(System.Object, System.Object[])
   at System.Xml.Serialization.TempAssembly.InvokeWriter(System.Xml.Serialization.XmlMapping, System.Xml.XmlWriter, System.Object, System.Xml.Serialization.XmlSerializerNamespaces, System.String, System.String)
   at System.Xml.Serialization.XmlSerializer.Serialize(System.Xml.XmlWriter, System.Object, System.Xml.Serialization.XmlSerializerNamespaces, System.String, System.String)
   at System.Xml.Serialization.XmlSerializer.Serialize(System.Xml.XmlWriter, System.Object)
MichalPetryka commented 2 years ago

66163

huoyaoyuan commented 2 years ago

Xml serialization was not actively developed recently, and there's no infrastructure for rich conversions like json.

StephenMolloy commented 2 years ago

If you try to serialize as an IEnumerable which is backed by the ImmutableArray... would this work then? That might be a way to unblock until this can be addressed.