/
FilterCollection.cs
169 lines (153 loc) · 6.97 KB
/
FilterCollection.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.ObjectModel;
using Microsoft.AspNetCore.Mvc.Core;
namespace Microsoft.AspNetCore.Mvc.Filters;
/// <summary>
/// A collection of <see cref="IFilterMetadata"/>.
/// </summary>
public class FilterCollection : Collection<IFilterMetadata>
{
/// <summary>
/// Adds a type representing a <see cref="IFilterMetadata"/>.
/// </summary>
/// <typeparam name="TFilterType">Type representing a <see cref="IFilterMetadata"/>.</typeparam>
/// <returns>A <see cref="IFilterMetadata"/> representing the added type.</returns>
/// <remarks>
/// Filter instances will be created using
/// <see cref="Microsoft.Extensions.DependencyInjection.ActivatorUtilities"/>.
/// Use <see cref="AddService(Type)"/> to register a service as a filter.
/// The added filter will be assigned an order of 0.
/// </remarks>
public IFilterMetadata Add<TFilterType>() where TFilterType : IFilterMetadata
{
return Add(typeof(TFilterType));
}
/// <summary>
/// Adds a type representing a <see cref="IFilterMetadata"/>.
/// </summary>
/// <param name="filterType">Type representing a <see cref="IFilterMetadata"/>.</param>
/// <returns>A <see cref="IFilterMetadata"/> representing the added type.</returns>
/// <remarks>
/// Filter instances will be created using
/// <see cref="Microsoft.Extensions.DependencyInjection.ActivatorUtilities"/>.
/// Use <see cref="AddService(Type)"/> to register a service as a filter.
/// The added filter will be assigned an order of 0.
/// </remarks>
public IFilterMetadata Add(Type filterType)
{
ArgumentNullException.ThrowIfNull(filterType);
return Add(filterType, order: 0);
}
/// <summary>
/// Adds a type representing a <see cref="IFilterMetadata"/>.
/// </summary>
/// <typeparam name="TFilterType">Type representing a <see cref="IFilterMetadata"/>.</typeparam>
/// <param name="order">The order of the added filter.</param>
/// <returns>A <see cref="IFilterMetadata"/> representing the added type.</returns>
/// <remarks>
/// Filter instances will be created using
/// <see cref="Microsoft.Extensions.DependencyInjection.ActivatorUtilities"/>.
/// Use <see cref="AddService(Type)"/> to register a service as a filter.
/// </remarks>
public IFilterMetadata Add<TFilterType>(int order) where TFilterType : IFilterMetadata
{
return Add(typeof(TFilterType), order);
}
/// <summary>
/// Adds a type representing a <see cref="IFilterMetadata"/>.
/// </summary>
/// <param name="filterType">Type representing a <see cref="IFilterMetadata"/>.</param>
/// <param name="order">The order of the added filter.</param>
/// <returns>A <see cref="IFilterMetadata"/> representing the added type.</returns>
/// <remarks>
/// Filter instances will be created using
/// <see cref="Microsoft.Extensions.DependencyInjection.ActivatorUtilities"/>.
/// Use <see cref="AddService(Type)"/> to register a service as a filter.
/// </remarks>
public IFilterMetadata Add(Type filterType, int order)
{
ArgumentNullException.ThrowIfNull(filterType);
if (!typeof(IFilterMetadata).IsAssignableFrom(filterType))
{
var message = Resources.FormatTypeMustDeriveFromType(
filterType.FullName,
typeof(IFilterMetadata).FullName);
throw new ArgumentException(message, nameof(filterType));
}
var filter = new TypeFilterAttribute(filterType) { Order = order };
Add(filter);
return filter;
}
/// <summary>
/// Adds a type representing a <see cref="IFilterMetadata"/>.
/// </summary>
/// <typeparam name="TFilterType">Type representing a <see cref="IFilterMetadata"/>.</typeparam>
/// <returns>A <see cref="IFilterMetadata"/> representing the added service type.</returns>
/// <remarks>
/// Filter instances will be created through dependency injection. Use
/// <see cref="Add(Type)"/> to register a service that will be created via
/// type activation.
/// The added filter will be assigned an order of 0.
/// </remarks>
public IFilterMetadata AddService<TFilterType>() where TFilterType : IFilterMetadata
{
return AddService(typeof(TFilterType));
}
/// <summary>
/// Adds a type representing a <see cref="IFilterMetadata"/>.
/// </summary>
/// <param name="filterType">Type representing a <see cref="IFilterMetadata"/>.</param>
/// <returns>A <see cref="IFilterMetadata"/> representing the added service type.</returns>
/// <remarks>
/// Filter instances will be created through dependency injection. Use
/// <see cref="Add(Type)"/> to register a service that will be created via
/// type activation.
/// The added filter will be assigned an order of 0.
/// </remarks>
public IFilterMetadata AddService(Type filterType)
{
ArgumentNullException.ThrowIfNull(filterType);
return AddService(filterType, order: 0);
}
/// <summary>
/// Adds a type representing a <see cref="IFilterMetadata"/>.
/// </summary>
/// <typeparam name="TFilterType">Type representing a <see cref="IFilterMetadata"/>.</typeparam>
/// <param name="order">The order of the added filter.</param>
/// <returns>A <see cref="IFilterMetadata"/> representing the added service type.</returns>
/// <remarks>
/// Filter instances will be created through dependency injection. Use
/// <see cref="Add(Type)"/> to register a service that will be created via
/// type activation.
/// </remarks>
public IFilterMetadata AddService<TFilterType>(int order) where TFilterType : IFilterMetadata
{
return AddService(typeof(TFilterType), order);
}
/// <summary>
/// Adds a type representing a <see cref="IFilterMetadata"/>.
/// </summary>
/// <param name="filterType">Type representing a <see cref="IFilterMetadata"/>.</param>
/// <param name="order">The order of the added filter.</param>
/// <returns>A <see cref="IFilterMetadata"/> representing the added service type.</returns>
/// <remarks>
/// Filter instances will be created through dependency injection. Use
/// <see cref="Add(Type)"/> to register a service that will be created via
/// type activation.
/// </remarks>
public IFilterMetadata AddService(Type filterType, int order)
{
ArgumentNullException.ThrowIfNull(filterType);
if (!typeof(IFilterMetadata).IsAssignableFrom(filterType))
{
var message = Resources.FormatTypeMustDeriveFromType(
filterType.FullName,
typeof(IFilterMetadata).FullName);
throw new ArgumentException(message, nameof(filterType));
}
var filter = new ServiceFilterAttribute(filterType) { Order = order };
Add(filter);
return filter;
}
}