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
/
ModelBinderProviderExtensions.cs
56 lines (50 loc) · 1.91 KB
/
ModelBinderProviderExtensions.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
// 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;
namespace Microsoft.AspNetCore.Mvc.ModelBinding
{
/// <summary>
/// Extension methods for <see cref="IModelBinderProvider"/>.
/// </summary>
public static class ModelBinderProviderExtensions
{
/// <summary>
/// Removes all model binder providers of the specified type.
/// </summary>
/// <param name="list">The list of <see cref="IModelBinderProvider"/>s.</param>
/// <typeparam name="TModelBinderProvider">The type to remove.</typeparam>
public static void RemoveType<TModelBinderProvider>(this IList<IModelBinderProvider> list) where TModelBinderProvider : IModelBinderProvider
{
if (list == null)
{
throw new ArgumentNullException(nameof(list));
}
RemoveType(list, typeof(TModelBinderProvider));
}
/// <summary>
/// Removes all model binder providers of the specified type.
/// </summary>
/// <param name="list">The list of <see cref="IModelBinderProvider"/>s.</param>
/// <param name="type">The type to remove.</param>
public static void RemoveType(this IList<IModelBinderProvider> list, Type type)
{
if (list == null)
{
throw new ArgumentNullException(nameof(list));
}
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}
for (var i = list.Count - 1; i >= 0; i--)
{
var modelBinderProvider = list[i];
if (modelBinderProvider.GetType() == type)
{
list.RemoveAt(i);
}
}
}
}
}