This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
/
DelegatingEnumerable.cs
83 lines (75 loc) · 3.44 KB
/
DelegatingEnumerable.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// 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;
using System.Linq;
namespace Microsoft.AspNetCore.Mvc.Formatters.Xml
{
/// <summary>
/// Serializes <see cref="IEnumerable{T}"/> types by delegating them through a concrete implementation.
/// </summary>
/// <typeparam name="TWrapped">The wrapping or original type of the <see cref="IEnumerable{T}"/>
/// to proxy.</typeparam>
/// <typeparam name="TDeclared">The type parameter of the original <see cref="IEnumerable{T}"/>
/// to proxy.</typeparam>
public class DelegatingEnumerable<TWrapped, TDeclared> : IEnumerable<TWrapped>
{
private readonly IEnumerable<TDeclared> _source;
private readonly IWrapperProvider _wrapperProvider;
/// <summary>
/// Initializes a <see cref="DelegatingEnumerable{TWrapped, TDeclared}"/>.
/// </summary>
/// <remarks>
/// This constructor is necessary for <see cref="System.Runtime.Serialization.DataContractSerializer"/>
/// to serialize.
/// </remarks>
public DelegatingEnumerable()
{
_source = Enumerable.Empty<TDeclared>();
}
/// <summary>
/// Initializes a <see cref="DelegatingEnumerable{TWrapped, TDeclared}"/> with the original
/// <see cref="IEnumerable{T}"/> and the wrapper provider for wrapping individual elements.
/// </summary>
/// <param name="source">The <see cref="IEnumerable{T}"/> instance to get the enumerator from.</param>
/// <param name="elementWrapperProvider">The wrapper provider for wrapping individual elements.</param>
public DelegatingEnumerable(IEnumerable<TDeclared> source, IWrapperProvider elementWrapperProvider)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
_source = source;
_wrapperProvider = elementWrapperProvider;
}
/// <summary>
/// Gets a delegating enumerator of the original <see cref="IEnumerable{T}"/> source which is being
/// wrapped.
/// </summary>
/// <returns>The delegating enumerator of the original <see cref="IEnumerable{T}"/> source.</returns>
public IEnumerator<TWrapped> GetEnumerator()
{
return new DelegatingEnumerator<TWrapped, TDeclared>(_source.GetEnumerator(), _wrapperProvider);
}
/// <summary>
/// The serializer requires every type it encounters can be serialized and deserialized.
/// This type will never be used for deserialization, but we are required to implement the add
/// method so that the type can be serialized. This will never be called.
/// </summary>
/// <param name="item">The item to add. Unused.</param>
public void Add(object item)
{
throw new NotImplementedException();
}
/// <summary>
/// Gets a delegating enumerator of the original <see cref="IEnumerable{T}"/> source which is being
/// wrapped.
/// </summary>
/// <returns>The delegating enumerator of the original <see cref="IEnumerable{T}"/> source.</returns>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}