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
/
TypeFilterAttribute.cs
83 lines (71 loc) · 2.97 KB
/
TypeFilterAttribute.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.Diagnostics;
using System.Linq;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.DependencyInjection;
namespace Microsoft.AspNetCore.Mvc
{
/// <summary>
/// A filter that creates another filter of type <see cref="ImplementationType"/>, retrieving missing constructor
/// arguments from dependency injection if available there.
/// </summary>
/// <remarks>
/// <para>
/// Primarily used in <see cref="M:FilterCollection.Add"/> calls.
/// </para>
/// <para>
/// Similar to the <see cref="ServiceFilterAttribute"/> in that both use constructor injection. Use
/// <see cref="ServiceFilterAttribute"/> instead if the filter is itself a service.
/// </para>
/// </remarks>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
[DebuggerDisplay("TypeFilter: Type={ImplementationType} Order={Order}")]
public class TypeFilterAttribute : Attribute, IFilterFactory, IOrderedFilter
{
private ObjectFactory _factory;
/// <summary>
/// Instantiates a new <see cref="TypeFilterAttribute"/> instance.
/// </summary>
/// <param name="type">The <see cref="Type"/> of filter to create.</param>
public TypeFilterAttribute(Type type)
{
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}
ImplementationType = type;
}
/// <summary>
/// Gets or sets the non-service arguments to pass to the <see cref="ImplementationType"/> constructor.
/// </summary>
/// <remarks>
/// Service arguments are found in the dependency injection container i.e. this filter supports constructor
/// injection in addition to passing the given <see cref="Arguments"/>.
/// </remarks>
public object[] Arguments { get; set; }
/// <summary>
/// Gets the <see cref="Type"/> of filter to create.
/// </summary>
public Type ImplementationType { get; }
/// <inheritdoc />
public int Order { get; set; }
/// <inheritdoc />
public bool IsReusable { get; set; }
/// <inheritdoc />
public IFilterMetadata CreateInstance(IServiceProvider serviceProvider)
{
if (serviceProvider == null)
{
throw new ArgumentNullException(nameof(serviceProvider));
}
if (_factory == null)
{
var argumentTypes = Arguments?.Select(a => a.GetType())?.ToArray();
_factory = ActivatorUtilities.CreateFactory(ImplementationType, argumentTypes ?? Type.EmptyTypes);
}
return (IFilterMetadata)_factory(serviceProvider, Arguments);
}
}
}