1
1
using JetBrains . Annotations ;
2
2
using System . Collections . Generic ;
3
- using System . Reflection ;
4
3
using System . Linq . Dynamic . Core . Validation ;
4
+ using System . Reflection ;
5
5
6
6
namespace System . Linq . Dynamic . Core . CustomTypeProviders
7
7
{
8
8
/// <summary>
9
- /// The abstract DynamicLinqCustomTypeProvider which is used by the <see cref="IDynamicLinkCustomTypeProvider"/> and can be used by a custom TypeProvider like in .NET Core.
9
+ /// The abstract DynamicLinqCustomTypeProvider which is used by the DefaultDynamicLinqCustomTypeProvider and can be used by a custom TypeProvider like in .NET Core.
10
10
/// </summary>
11
11
public abstract class AbstractDynamicLinqCustomTypeProvider
12
12
{
13
13
/// <summary>
14
- /// Finds the types marked with DynamicLinqTypeAttribute.
14
+ /// Finds the unique types marked with DynamicLinqTypeAttribute.
15
15
/// </summary>
16
16
/// <param name="assemblies">The assemblies to process.</param>
17
- /// <returns>IEnumerable{Type}</returns>
17
+ /// <returns><see cref=" IEnumerable{Type}" /> </returns>
18
18
protected IEnumerable < Type > FindTypesMarkedWithDynamicLinqTypeAttribute ( [ NotNull ] IEnumerable < Assembly > assemblies )
19
19
{
20
20
Check . NotNull ( assemblies , nameof ( assemblies ) ) ;
21
21
#if ! NET35
22
- assemblies = assemblies . Where ( x => ! x . IsDynamic ) ;
23
- #endif
24
- var definedTypes = GetAssemblyTypes ( assemblies ) ;
25
-
26
- #if ( WINDOWS_APP || DOTNET5_1 || UAP10_0 || NETSTANDARD )
27
- return definedTypes . Where ( x => x . CustomAttributes . Any ( y => y . AttributeType == typeof ( DynamicLinqTypeAttribute ) ) ) . Select ( x => x . AsType ( ) ) ;
28
- #else
29
- return definedTypes . Where ( x => x . GetCustomAttributes ( typeof ( DynamicLinqTypeAttribute ) , false ) . Any ( ) ) ;
22
+ assemblies = assemblies . Where ( a => ! a . IsDynamic ) ;
30
23
#endif
24
+ return GetAssemblyTypesWithDynamicLinqTypeAttribute ( assemblies ) . Distinct ( ) . ToArray ( ) ;
31
25
}
32
26
33
27
/// <summary>
@@ -41,7 +35,7 @@ protected Type ResolveType([NotNull] IEnumerable<Assembly> assemblies, [NotNull]
41
35
Check . NotNull ( assemblies , nameof ( assemblies ) ) ;
42
36
Check . NotEmpty ( typeName , nameof ( typeName ) ) ;
43
37
44
- foreach ( Assembly assembly in assemblies )
38
+ foreach ( var assembly in assemblies )
45
39
{
46
40
Type resolvedType = assembly . GetType ( typeName , false , true ) ;
47
41
if ( resolvedType != null )
@@ -55,28 +49,32 @@ protected Type ResolveType([NotNull] IEnumerable<Assembly> assemblies, [NotNull]
55
49
56
50
#if ( WINDOWS_APP || DOTNET5_1 || UAP10_0 || NETSTANDARD )
57
51
/// <summary>
58
- /// Gets the assembly types in an Exception friendly way.
52
+ /// Gets the assembly types annotated with <see cref="DynamicLinqTypeAttribute"/> in an Exception friendly way.
59
53
/// </summary>
60
54
/// <param name="assemblies">The assemblies to process.</param>
61
- /// <returns>IEnumerable{Type}</returns>
62
- protected IEnumerable < TypeInfo > GetAssemblyTypes ( [ NotNull ] IEnumerable < Assembly > assemblies )
55
+ /// <returns><see cref=" IEnumerable{Type}" /> </returns>
56
+ protected IEnumerable < Type > GetAssemblyTypesWithDynamicLinqTypeAttribute ( [ NotNull ] IEnumerable < Assembly > assemblies )
63
57
{
64
58
Check . NotNull ( assemblies , nameof ( assemblies ) ) ;
65
59
66
60
foreach ( var assembly in assemblies )
67
61
{
68
- IEnumerable < TypeInfo > definedTypes = null ;
62
+ Type [ ] definedTypes = null ;
69
63
70
64
try
71
65
{
72
- definedTypes = assembly . DefinedTypes ;
66
+ definedTypes = assembly . ExportedTypes . Where ( t => t . GetTypeInfo ( ) . IsDefined ( typeof ( DynamicLinqTypeAttribute ) , false ) ) . ToArray ( ) ;
67
+ }
68
+ catch ( ReflectionTypeLoadException reflectionTypeLoadException )
69
+ {
70
+ definedTypes = reflectionTypeLoadException . Types ;
73
71
}
74
72
catch
75
73
{
76
- // Ignore error
74
+ // Ignore all other exceptions
77
75
}
78
76
79
- if ( definedTypes != null )
77
+ if ( definedTypes != null && definedTypes . Length > 0 )
80
78
{
81
79
foreach ( var definedType in definedTypes )
82
80
{
@@ -87,28 +85,33 @@ protected IEnumerable<TypeInfo> GetAssemblyTypes([NotNull] IEnumerable<Assembly>
87
85
}
88
86
#else
89
87
/// <summary>
90
- /// Gets the assembly types in an Exception friendly way.
88
+ /// Gets the assembly types annotated with <see cref="DynamicLinqTypeAttribute"/> in an Exception friendly way.
91
89
/// </summary>
92
90
/// <param name="assemblies">The assemblies to process.</param>
93
- /// <returns>IEnumerable{Type}</returns>
94
- protected IEnumerable < Type > GetAssemblyTypes ( [ NotNull ] IEnumerable < Assembly > assemblies )
91
+ /// <returns><see cref=" IEnumerable{Type}" /> </returns>
92
+ protected IEnumerable < Type > GetAssemblyTypesWithDynamicLinqTypeAttribute ( [ NotNull ] IEnumerable < Assembly > assemblies )
95
93
{
96
94
Check . NotNull ( assemblies , nameof ( assemblies ) ) ;
97
95
98
- foreach ( var assembly in assemblies )
96
+ foreach ( var assembly in assemblies . Where ( a => ! a . GlobalAssemblyCache ) ) // Skip System DLL's
99
97
{
100
- IEnumerable < Type > definedTypes = null ;
98
+ Type [ ] definedTypes = null ;
101
99
102
100
try
103
101
{
104
- definedTypes = assembly . GetTypes ( ) ;
102
+ definedTypes = assembly . GetExportedTypes ( )
103
+ . Where ( t => t . IsDefined ( typeof ( DynamicLinqTypeAttribute ) , false ) ) . ToArray ( ) ;
104
+ }
105
+ catch ( ReflectionTypeLoadException reflectionTypeLoadException )
106
+ {
107
+ definedTypes = reflectionTypeLoadException . Types ;
105
108
}
106
109
catch
107
110
{
108
- // Ignore error
111
+ // Ignore all other exceptions
109
112
}
110
113
111
- if ( definedTypes != null )
114
+ if ( definedTypes != null && definedTypes . Length > 0 )
112
115
{
113
116
foreach ( var definedType in definedTypes )
114
117
{
0 commit comments