<PackageReference Include="System.ClientModel" Version="1.7.0" />

CollectionWriter

abstract class CollectionWriter
using System.ClientModel.Internal; using System.Collections; using System.Runtime.CompilerServices; namespace System.ClientModel.Primitives { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] internal abstract class CollectionWriter { internal static CollectionWriter GetCollectionWriter(IEnumerable enumerable, ModelReaderWriterOptions options) { if (options.Format != "J" && options.Format != "W") throw new InvalidOperationException("Format '" + options.Format + "' is not supported. Only 'J' or 'W' format can be written as collections"); if (options.Format == "J") return new JsonCollectionWriter(); IPersistableModel<object> iPersistableModel = GetIPersistableModel(enumerable); string formatFromOptions = iPersistableModel.GetFormatFromOptions(options); if (formatFromOptions == "J" && iPersistableModel is IJsonModel<object>) return new JsonCollectionWriter(); throw new InvalidOperationException(iPersistableModel.GetType().ToFriendlyName() + " has a wire format of '" + formatFromOptions + "'. It must be 'J' to be written as a collection"); } private static IPersistableModel<object> GetIPersistableModel(IEnumerable enumerable) { object firstObject = GetFirstObject(enumerable); IEnumerable enumerable2 = firstObject as IEnumerable; if (enumerable2 != null) return GetIPersistableModel(enumerable2); IPersistableModel<object> persistableModel = firstObject as IPersistableModel<object>; if (persistableModel != null) return persistableModel; throw new InvalidOperationException("Unable to write " + enumerable.GetType().ToFriendlyName() + ". Only collections of 'IPersistableModel' can be written."); } private static object GetFirstObject(IEnumerable enumerable) { IDictionary dictionary = enumerable as IDictionary; if (dictionary != null) { foreach (object key in dictionary.Keys) { object obj = dictionary[key]; if (obj != null) return obj; } throw new InvalidOperationException("Can't use format 'W' format on an empty collection. Please specify a concrete format"); } IEnumerator enumerator2 = enumerable.GetEnumerator(); if (enumerator2.MoveNext()) return enumerator2.Current; throw new InvalidOperationException("Can't use format 'W' format on an empty collection. Please specify a concrete format"); } internal abstract BinaryData Write(IEnumerable enumerable, ModelReaderWriterOptions options); } }