<PackageReference Include="Castle.Core" Version="3.0.0.4001" />

XmlArraySerializer

using System; using System.Collections; namespace Castle.Components.DictionaryAdapter.Xml { public class XmlArraySerializer : XmlTypeSerializer { public static readonly XmlArraySerializer Instance = new XmlArraySerializer(); public override XmlTypeKind Kind => XmlTypeKind.Collection; public override bool CanGetStub => true; protected XmlArraySerializer() { } public override object GetStub(IXmlNode node, IDictionaryAdapter parent, IXmlAccessor accessor) { Type elementType = node.ClrType.GetElementType(); return Array.CreateInstance(elementType, 0); } public override object GetValue(IXmlNode node, IDictionaryAdapter parent, IXmlAccessor accessor) { ArrayList arrayList = new ArrayList(); Type elementType = node.ClrType.GetElementType(); XmlReferenceManager references = XmlAdapter.For(parent).References; accessor.GetCollectionAccessor(elementType).GetCollectionItems(node, parent, references, arrayList); return arrayList.ToArray(elementType); } public override void SetValue(IXmlNode node, IDictionaryAdapter parent, IXmlAccessor accessor, ref object value) { Array array = (Array)value; Array array2 = null; Type elementType = array.GetType().GetElementType(); IXmlCollectionAccessor collectionAccessor = accessor.GetCollectionAccessor(elementType); IXmlCursor xmlCursor = collectionAccessor.SelectCollectionItems(node, true); XmlTypeSerializer serializer = collectionAccessor.Serializer; XmlReferenceManager references = XmlAdapter.For(parent).References; for (int i = 0; i < array.Length; i++) { object value2 = array.GetValue(i); object newValue = value2; collectionAccessor.SetValue(xmlCursor, parent, references, xmlCursor.MoveNext(), null, ref newValue); if (array2 != null) array2.SetValue(newValue, i); else if (!object.Equals(newValue, value2)) { array2 = Array.CreateInstance(elementType, array.Length); Array.Copy(array, array2, i); array2.SetValue(newValue, i); } } xmlCursor.RemoveAllNext(); if (array2 != null) value = array2; } } }