-
Notifications
You must be signed in to change notification settings - Fork 0
/
AssemblyScanner.cs
113 lines (99 loc) · 3.54 KB
/
AssemblyScanner.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
#region License
// Copyright (c) Jeremy Skinner (http://www.jeremyskinner.co.uk)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// The latest version of this file can be found at http://www.codeplex.com/FluentValidation
#endregion
namespace FluentValidation {
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
/// <summary>
/// Class that can be used to find all the validators from a collection of types.
/// </summary>
public class AssemblyScanner : IEnumerable<AssemblyScanner.AssemblyScanResult> {
readonly IEnumerable<Type> types;
/// <summary>
/// Creates a scanner that works on a sequence of types.
/// </summary>
public AssemblyScanner(IEnumerable<Type> types) {
this.types = types;
}
/// <summary>
/// Finds all the validators in the specified assembly.
/// </summary>
public static AssemblyScanner FindValidatorsInAssembly(Assembly assembly) {
return new AssemblyScanner(assembly.GetExportedTypes());
}
/// <summary>
/// Finds all the validators in the assembly containing the specified type.
/// </summary>
public static AssemblyScanner FindValidatorsInAssemblyContaining<T>() {
return FindValidatorsInAssembly(typeof(T).Assembly);
}
private IEnumerable<AssemblyScanResult> Execute() {
var openGenericType = typeof(IValidator<>);
var query = from type in types
let interfaces = type.GetInterfaces()
let genericInterfaces = interfaces.Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == openGenericType)
let matchingInterface = genericInterfaces.FirstOrDefault()
where matchingInterface != null
select new AssemblyScanResult(matchingInterface, type);
return query;
}
/// <summary>
/// Performs the specified action to all of the assembly scan results.
/// </summary>
public void ForEach(Action<AssemblyScanResult> action) {
foreach(var result in this) {
action(result);
}
}
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>
/// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
/// </returns>
/// <filterpriority>1</filterpriority>
public IEnumerator<AssemblyScanResult> GetEnumerator() {
return Execute().GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
/// <summary>
/// Result of performing a scan.
/// </summary>
public class AssemblyScanResult {
/// <summary>
/// Creates an instance of an AssemblyScanResult.
/// </summary>
public AssemblyScanResult(Type interfaceType, Type validatorType) {
InterfaceType = interfaceType;
ValidatorType = validatorType;
}
/// <summary>
/// Validator interface type, eg IValidator<Foo>
/// </summary>
public Type InterfaceType { get; private set; }
/// <summary>
/// Concrete type that implements the InterfaceType, eg FooValidator.
/// </summary>
public Type ValidatorType { get; private set; }
}
}
}