This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
EnumerableWrapperProviderFactory.cs
76 lines (66 loc) · 3.22 KB
/
EnumerableWrapperProviderFactory.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
// 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.Generic;
using System.Reflection;
using Microsoft.Extensions.Internal;
namespace Microsoft.AspNetCore.Mvc.Formatters.Xml
{
/// <summary>
/// Creates an <see cref="EnumerableWrapperProvider"/> for interface types implementing the
/// <see cref="IEnumerable{T}"/> type.
/// </summary>
public class EnumerableWrapperProviderFactory : IWrapperProviderFactory
{
private readonly IEnumerable<IWrapperProviderFactory> _wrapperProviderFactories;
/// <summary>
/// Initializes an <see cref="EnumerableWrapperProviderFactory"/> with a list
/// <see cref="IWrapperProviderFactory"/>.
/// </summary>
/// <param name="wrapperProviderFactories">List of <see cref="IWrapperProviderFactory"/>.</param>
public EnumerableWrapperProviderFactory(IEnumerable<IWrapperProviderFactory> wrapperProviderFactories)
{
if (wrapperProviderFactories == null)
{
throw new ArgumentNullException(nameof(wrapperProviderFactories));
}
_wrapperProviderFactories = wrapperProviderFactories;
}
/// <summary>
/// Gets an <see cref="EnumerableWrapperProvider"/> for the provided context.
/// </summary>
/// <param name="context">The <see cref="WrapperProviderContext"/>.</param>
/// <returns>An instance of <see cref="EnumerableWrapperProvider"/> if the declared type is
/// an interface and implements <see cref="IEnumerable{T}"/>.</returns>
public IWrapperProvider GetProvider(WrapperProviderContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (context.IsSerialization)
{
// Example: IEnumerable<SerializableError>
var declaredType = context.DeclaredType;
var declaredTypeInfo = declaredType.GetTypeInfo();
// We only wrap interfaces types(ex: IEnumerable<T>, IQueryable<T>, IList<T> etc.) and not
// concrete types like List<T>, Collection<T> which implement IEnumerable<T>.
if (declaredType != null && declaredTypeInfo.IsInterface && declaredTypeInfo.IsGenericType)
{
var enumerableOfT = ClosedGenericMatcher.ExtractGenericInterface(
declaredType,
typeof(IEnumerable<>));
if (enumerableOfT != null)
{
var elementType = enumerableOfT.GenericTypeArguments[0];
var wrapperProviderContext = new WrapperProviderContext(elementType, context.IsSerialization);
var elementWrapperProvider =
_wrapperProviderFactories.GetWrapperProvider(wrapperProviderContext);
return new EnumerableWrapperProvider(enumerableOfT, elementWrapperProvider);
}
}
}
return null;
}
}
}