// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections; using System.Collections.Generic; namespace Microsoft.AspNetCore.Mvc.Formatters.Xml { /// /// Delegates enumeration of elements to the original enumerator and wraps the items /// with the supplied . /// /// The type to which the individual elements need to be wrapped to. /// The original type of the element being wrapped. public class DelegatingEnumerator : IEnumerator { private readonly IEnumerator _inner; private readonly IWrapperProvider _wrapperProvider; /// /// Initializes a which enumerates /// over the elements of the original enumerator and wraps them using the supplied /// . /// /// The original enumerator. /// The wrapper provider to wrap individual elements. public DelegatingEnumerator(IEnumerator inner, IWrapperProvider wrapperProvider) { if (inner == null) { throw new ArgumentNullException(nameof(inner)); } _inner = inner; _wrapperProvider = wrapperProvider; } /// public TWrapped Current { get { object obj = _inner.Current; if (_wrapperProvider == null) { // if there is no wrapper, then this cast should not fail return (TWrapped)obj; } return (TWrapped)_wrapperProvider.Wrap(obj); } } /// object IEnumerator.Current { get { return Current; } } /// public void Dispose() { _inner.Dispose(); } /// public bool MoveNext() { return _inner.MoveNext(); } /// public void Reset() { _inner.Reset(); } } }