This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
/
DefaultControllerTypeProvider.cs
122 lines (109 loc) · 4.52 KB
/
DefaultControllerTypeProvider.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
// 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.Linq;
using System.Reflection;
using Microsoft.AspNet.Mvc.Infrastructure;
namespace Microsoft.AspNet.Mvc.Controllers
{
/// <summary>
/// A <see cref="IControllerTypeProvider"/> that identifies controller types from assemblies
/// specified by the registered <see cref="IAssemblyProvider"/>.
/// </summary>
public class DefaultControllerTypeProvider : IControllerTypeProvider
{
private const string ControllerTypeName = "Controller";
private static readonly TypeInfo ObjectTypeInfo = typeof(object).GetTypeInfo();
private readonly IAssemblyProvider _assemblyProvider;
/// <summary>
/// Initializes a new instance of <see cref="DefaultControllerTypeProvider"/>.
/// </summary>
/// <param name="assemblyProvider"><see cref="IAssemblyProvider"/> that provides assemblies to look for
/// controllers in.</param>
public DefaultControllerTypeProvider(IAssemblyProvider assemblyProvider)
{
_assemblyProvider = assemblyProvider;
}
/// <inheritdoc />
public virtual IEnumerable<TypeInfo> ControllerTypes
{
get
{
var candidateAssemblies = new HashSet<Assembly>(_assemblyProvider.CandidateAssemblies);
var types = candidateAssemblies.SelectMany(a => a.DefinedTypes);
return types.Where(typeInfo => IsController(typeInfo, candidateAssemblies));
}
}
/// <summary>
/// Returns <c>true</c> if the <paramref name="typeInfo"/> is a controller. Otherwise <c>false</c>.
/// </summary>
/// <param name="typeInfo">The <see cref="TypeInfo"/>.</param>
/// <param name="candidateAssemblies">The set of candidate assemblies.</param>
/// <returns><c>true</c> if the <paramref name="typeInfo"/> is a controller. Otherwise <c>false</c>.</returns>
protected internal virtual bool IsController(
TypeInfo typeInfo,
ISet<Assembly> candidateAssemblies)
{
if (typeInfo == null)
{
throw new ArgumentNullException(nameof(typeInfo));
}
if (candidateAssemblies == null)
{
throw new ArgumentNullException(nameof(candidateAssemblies));
}
if (!typeInfo.IsClass)
{
return false;
}
if (typeInfo.IsAbstract)
{
return false;
}
// We only consider public top-level classes as controllers. IsPublic returns false for nested
// classes, regardless of visibility modifiers
if (!typeInfo.IsPublic)
{
return false;
}
if (typeInfo.ContainsGenericParameters)
{
return false;
}
if (!typeInfo.Name.EndsWith(ControllerTypeName, StringComparison.OrdinalIgnoreCase) &&
!DerivesFromController(typeInfo, candidateAssemblies))
{
return false;
}
if (typeInfo.IsDefined(typeof(NonControllerAttribute)))
{
return false;
}
return true;
}
private bool DerivesFromController(TypeInfo typeInfo, ISet<Assembly> candidateAssemblies)
{
while (typeInfo != ObjectTypeInfo)
{
var baseTypeInfo = typeInfo.BaseType.GetTypeInfo();
// A base type will be treated as a controller if
// a) it ends in the term "Controller" and
// b) it's assembly is one of the candidate assemblies we're considering. This ensures that the assembly
// the base type is declared in references Mvc.
if (baseTypeInfo.Name.EndsWith(ControllerTypeName, StringComparison.Ordinal) &&
candidateAssemblies.Contains(baseTypeInfo.Assembly))
{
return true;
}
// c). The base type is called 'Controller.
if (string.Equals(baseTypeInfo.Name, ControllerTypeName, StringComparison.Ordinal))
{
return true;
}
typeInfo = baseTypeInfo;
}
return false;
}
}
}