2
2
{
3
3
using System ;
4
4
using System . Collections . Generic ;
5
+ using System . ComponentModel ;
5
6
using System . Linq ;
6
- using System . Runtime . CompilerServices ;
7
+ using System . Reflection ;
7
8
8
9
/// <summary>
9
10
/// This is where we set what our convention is all about.
@@ -19,31 +20,190 @@ public Types(string descriptionOfTypes)
19
20
20
21
public string Description { get ; private set ; }
21
22
22
- public bool HasData { get { return TypesToVerify . Any ( ) ; } }
23
+ public bool HasData
24
+ {
25
+ get { return TypesToVerify . Any ( ) ; }
26
+ }
27
+
28
+ /// <summary>
29
+ /// Gets a filtered list of types from the assembly of the specified
30
+ /// type, <typeparam name="T" />, using the specified <param name="predicate" />.
31
+ /// </summary>
32
+ /// <typeparam name="T">A type residing in the assembly to get types from.</typeparam>
33
+ /// <param name="predicate">A function to test each type for a condition.</param>
34
+ public static Types InAssemblyOf < T > ( Func < Type , bool > predicate )
35
+ {
36
+ return InAssemblyOf ( typeof ( T ) , predicate ) ;
37
+ }
38
+
39
+ /// <summary>
40
+ /// Gets a filtered list of types from the assembly of the specified
41
+ /// type, <param name="type" />, using the specified <param name="predicate" />.
42
+ /// </summary>
43
+ /// <param name="type">A type residing in the assembly to get types from.</param>
44
+ /// <param name="predicate">A function to test each type for a condition.</param>
45
+ public static Types InAssemblyOf ( Type type , Func < Type , bool > predicate )
46
+ {
47
+ return InAssembly ( type . Assembly , predicate ) ;
48
+ }
49
+
50
+ /// <summary>
51
+ /// Gets a filtered list of types from the specified <param name="assembly" /> using the specified <param name="predicate" />.
52
+ /// </summary>
53
+ /// <param name="assembly">The assembly to get types from.</param>
54
+ /// <param name="predicate">A function to test each type for a condition.</param>
55
+ public static Types InAssembly ( Assembly assembly , Func < Type , bool > predicate )
56
+ {
57
+ return InAssembly ( assembly , GetAssemblyName ( assembly ) , predicate ) ;
58
+ }
59
+
60
+ /// <summary>
61
+ /// Gets a filtered list of types from the assembly of the specified
62
+ /// type, <typeparam name="T" />, using the specified <param name="predicate" />.
63
+ /// </summary>
64
+ /// <typeparam name="T">A type residing in the assembly to get types from.</typeparam>
65
+ /// <param name="descriptionOfTypes">A description of the matched types.</param>
66
+ /// <param name="predicate">A function to test each type for a condition.</param>
67
+ public static Types InAssemblyOf < T > ( string descriptionOfTypes , Func < Type , bool > predicate )
68
+ {
69
+ return InAssemblyOf ( typeof ( T ) , descriptionOfTypes , predicate ) ;
70
+ }
71
+
72
+ /// <summary>
73
+ /// Gets a filtered list of types from the assembly of the specified
74
+ /// type, <param name="type" />, using the specified <param name="predicate" />.
75
+ /// </summary>
76
+ /// <param name="type">A type residing in the assembly to get types from.</param>
77
+ /// <param name="descriptionOfTypes">A description of the matched types.</param>
78
+ /// <param name="predicate">A function to test each type for a condition.</param>
79
+ public static Types InAssemblyOf ( Type type , string descriptionOfTypes , Func < Type , bool > predicate )
80
+ {
81
+ return InAssembly ( type . Assembly , descriptionOfTypes , predicate ) ;
82
+ }
83
+
84
+ /// <summary>
85
+ /// Gets a filtered list of types from the specified <param name="assembly" /> using the specified <param name="predicate" />.
86
+ /// </summary>
87
+ /// <param name="assembly">The assembly to get types from.</param>
88
+ /// <param name="descriptionOfTypes">A description of the matched types.</param>
89
+ /// <param name="predicate">A function to test each type for a condition.</param>
90
+ public static Types InAssembly ( Assembly assembly , string descriptionOfTypes , Func < Type , bool > predicate )
91
+ {
92
+ return InAssemblies ( new [ ] { assembly } , descriptionOfTypes , predicate ) ;
93
+ }
94
+
95
+ /// <summary>
96
+ /// Gets an optionally filtered list of types from the specified <param name="assemblies" /> using the specified <param name="predicate" />.
97
+ /// </summary>
98
+ /// <param name="assemblies">A list of assemblies to get types from.</param>
99
+ /// <param name="descriptionOfTypes">A description of the matched types.</param>
100
+ /// <param name="predicate">A function to test each type for a condition.</param>
101
+ public static Types InAssemblies ( IEnumerable < Assembly > assemblies , string descriptionOfTypes , Func < Type , bool > predicate )
102
+ {
103
+ return InCollection ( assemblies . SelectMany ( x => x . GetTypes ( ) ) . Where ( predicate ) , descriptionOfTypes ) ;
104
+ }
23
105
106
+ /// <summary>
107
+ /// Gets an optionally filtered list of types from the assembly of the specified type, <typeparam name="T" />.
108
+ /// </summary>
109
+ /// <typeparam name="T">A type residing in the assembly to get types from.</typeparam>
110
+ /// <param name="excludeCompilerGeneratedTypes">Compiler generated types will be excluded if set to <c>true</c>.</param>
24
111
public static Types InAssemblyOf < T > ( bool excludeCompilerGeneratedTypes = true )
25
112
{
26
- var assembly = typeof ( T ) . Assembly ;
27
- var typesToVerify = assembly . GetTypes ( ) ;
28
- if ( excludeCompilerGeneratedTypes )
29
- {
30
- typesToVerify = typesToVerify
31
- . Where ( t => ! t . GetCustomAttributes ( typeof ( CompilerGeneratedAttribute ) , true ) . Any ( ) )
32
- . ToArray ( ) ;
33
- }
34
- return new Types ( assembly . GetName ( ) . Name )
35
- {
36
- TypesToVerify = typesToVerify
37
- } ;
113
+ return InAssemblyOf ( typeof ( T ) , excludeCompilerGeneratedTypes ) ;
38
114
}
39
115
116
+ /// <summary>
117
+ /// Gets an optionally filtered list of types from the assembly of the specified type, <param name="type" />.
118
+ /// </summary>
119
+ /// <param name="type">A type residing in the assembly to get types from.</param>
120
+ /// <param name="excludeCompilerGeneratedTypes">Compiler generated types will be excluded if set to <c>true</c>.</param>
121
+ public static Types InAssemblyOf ( Type type , bool excludeCompilerGeneratedTypes = true )
122
+ {
123
+ return InAssembly ( type . Assembly , excludeCompilerGeneratedTypes ) ;
124
+ }
125
+
126
+ /// <summary>
127
+ /// Gets an optionally filtered list of types from the specified <param name="assembly" />.
128
+ /// </summary>
129
+ /// <param name="assembly">The assembly to get types from.</param>
130
+ /// <param name="excludeCompilerGeneratedTypes">Compiler generated types will be excluded if set to <c>true</c>.</param>
131
+ public static Types InAssembly ( Assembly assembly , bool excludeCompilerGeneratedTypes = true )
132
+ {
133
+ return InAssembly ( assembly , GetAssemblyName ( assembly ) , excludeCompilerGeneratedTypes ) ;
134
+ }
135
+
136
+ /// <summary>
137
+ /// Gets an optionally filtered list of types from the assembly of the specified
138
+ /// type, <typeparam name="T" />, using the specified <param name="types" /> filter.
139
+ /// </summary>
140
+ /// <typeparam name="T">A type residing in the assembly to get types from.</typeparam>
141
+ /// <param name="descriptionOfTypes">A description of the matched types.</param>
142
+ /// <param name="types">A function to filter or add matched types.</param>
143
+ [ EditorBrowsable ( EditorBrowsableState . Never ) ]
144
+ [ Obsolete ( "This method is obsolete and should not be used. Use the overload with a predicate instead." ) ]
40
145
public static Types InAssemblyOf < T > ( string descriptionOfTypes , Func < IEnumerable < Type > , IEnumerable < Type > > types )
41
146
{
42
- var assembly = typeof ( T ) . Assembly ;
43
- return new Types ( descriptionOfTypes )
44
- {
45
- TypesToVerify = types ( assembly . GetTypes ( ) ) . ToArray ( )
46
- } ;
147
+ return InCollection ( types ( typeof ( T ) . Assembly . GetTypes ( ) ) , descriptionOfTypes ) ;
148
+ }
149
+
150
+ /// <summary>
151
+ /// Gets an optionally filtered list of types from the assembly of the specified type, <typeparam name="T" />.
152
+ /// </summary>
153
+ /// <typeparam name="T">A type residing in the assembly to get types from.</typeparam>
154
+ /// <param name="descriptionOfTypes">A description of the matched types.</param>
155
+ /// <param name="excludeCompilerGeneratedTypes">Compiler generated types will be excluded if set to <c>true</c>.</param>
156
+ public static Types InAssemblyOf < T > ( string descriptionOfTypes , bool excludeCompilerGeneratedTypes = true )
157
+ {
158
+ return InAssemblyOf ( typeof ( T ) , descriptionOfTypes , excludeCompilerGeneratedTypes ) ;
159
+ }
160
+
161
+ /// <summary>
162
+ /// Gets an optionally filtered list of types from the assembly of the specified type, <param name="type" />.
163
+ /// </summary>
164
+ /// <param name="type">A type residing in the assembly to get types from.</param>
165
+ /// <param name="descriptionOfTypes">A description of the matched types.</param>
166
+ /// <param name="excludeCompilerGeneratedTypes">Compiler generated types will be excluded if set to <c>true</c>.</param>
167
+ public static Types InAssemblyOf ( Type type , string descriptionOfTypes , bool excludeCompilerGeneratedTypes = true )
168
+ {
169
+ return InAssembly ( type . Assembly , descriptionOfTypes , excludeCompilerGeneratedTypes ) ;
170
+ }
171
+
172
+ /// <summary>
173
+ /// Gets an optionally filtered list of types from the specified <param name="assembly" />.
174
+ /// </summary>
175
+ /// <param name="assembly">The assembly to get types from.</param>
176
+ /// <param name="descriptionOfTypes">A description of the matched types.</param>
177
+ /// <param name="excludeCompilerGeneratedTypes">Compiler generated types will be excluded if set to <c>true</c>.</param>
178
+ public static Types InAssembly ( Assembly assembly , string descriptionOfTypes , bool excludeCompilerGeneratedTypes = true )
179
+ {
180
+ return InAssemblies ( new [ ] { assembly } , descriptionOfTypes , excludeCompilerGeneratedTypes ) ;
181
+ }
182
+
183
+ /// <summary>
184
+ /// Gets an optionally filtered list of types from the specified <param name="assemblies" />.
185
+ /// </summary>
186
+ /// <param name="assemblies">A list of assemblies to get types from.</param>
187
+ /// <param name="descriptionOfTypes">A description of the matched types.</param>
188
+ /// <param name="excludeCompilerGeneratedTypes">Compiler generated types will be excluded if set to <c>true</c>.</param>
189
+ public static Types InAssemblies ( IEnumerable < Assembly > assemblies , string descriptionOfTypes , bool excludeCompilerGeneratedTypes = true )
190
+ {
191
+ return InAssemblies ( assemblies , descriptionOfTypes , type => ! ( excludeCompilerGeneratedTypes && type . IsCompilerGenerated ( ) ) ) ;
192
+ }
193
+
194
+ /// <summary>
195
+ /// Gets a list of types from the specified <param name="types" /> collection.
196
+ /// </summary>
197
+ /// <param name="types">The types.</param>
198
+ /// <param name="descriptionOfTypes">A description of the matched types.</param>
199
+ public static Types InCollection ( IEnumerable < Type > types , string descriptionOfTypes )
200
+ {
201
+ return new Types ( descriptionOfTypes ) { TypesToVerify = types . ToArray ( ) } ;
202
+ }
203
+
204
+ private static string GetAssemblyName ( Assembly assembly )
205
+ {
206
+ return assembly . GetName ( ) . Name ;
47
207
}
48
208
}
49
209
}
0 commit comments