1
1
#if ! ( WINDOWS_APP || UAP10_0 )
2
2
using System . Collections . Generic ;
3
+ using System . Reflection ;
3
4
4
5
namespace System . Linq . Dynamic . Core . CustomTypeProviders
5
6
{
@@ -22,21 +23,69 @@ public virtual HashSet<Type> GetCustomTypes()
22
23
return _customTypes ?? ( _customTypes = new HashSet < Type > ( FindTypesMarkedWithAttribute ( ) ) ) ;
23
24
}
24
25
25
- private IEnumerable < Type > FindTypesMarkedWithAttribute ( )
26
+ protected IEnumerable < Type > FindTypesMarkedWithAttribute ( )
26
27
{
27
- var assemblies = _assemblyHelper . GetAssemblies ( ) ;
28
+ IEnumerable < Assembly > assemblies = _assemblyHelper . GetAssemblies ( ) ;
28
29
#if ! NET35
29
- assemblies = assemblies . Where ( x => ! x . IsDynamic ) . ToArray ( ) ;
30
+ assemblies = assemblies . Where ( x => ! x . IsDynamic ) ;
30
31
#endif
31
32
33
+ var definedTypes = ExceptionFriedlyGetAssemblyTypes ( assemblies ) ;
34
+
32
35
#if ( WINDOWS_APP || DOTNET5_1 || UAP10_0 || NETSTANDARD )
33
- var definedTypes = assemblies . SelectMany ( x => x . DefinedTypes ) ;
34
36
return definedTypes . Where ( x => x . CustomAttributes . Any ( y => y . AttributeType == typeof ( DynamicLinqTypeAttribute ) ) ) . Select ( x => x . AsType ( ) ) ;
35
37
#else
36
- var definedTypes = assemblies . SelectMany ( x => x . GetTypes ( ) ) ;
37
38
return definedTypes . Where ( x => x . GetCustomAttributes ( typeof ( DynamicLinqTypeAttribute ) , false ) . Any ( ) ) ;
38
39
#endif
39
40
}
41
+
42
+ #if ( WINDOWS_APP || DOTNET5_1 || UAP10_0 || NETSTANDARD )
43
+ protected IEnumerable < TypeInfo > ExceptionFriedlyGetAssemblyTypes ( IEnumerable < Assembly > assemblies )
44
+ {
45
+ foreach ( var assembly in assemblies )
46
+ {
47
+ IEnumerable < TypeInfo > definedTypes = null ;
48
+
49
+ try
50
+ {
51
+ definedTypes = assembly . DefinedTypes ;
52
+ }
53
+ catch ( Exception )
54
+ { }
55
+
56
+ if ( definedTypes != null )
57
+ {
58
+ foreach ( var definedType in definedTypes )
59
+ {
60
+ yield return definedType ;
61
+ }
62
+ }
63
+ }
64
+ }
65
+ #else
66
+ protected IEnumerable < Type > ExceptionFriedlyGetAssemblyTypes ( IEnumerable < Assembly > assemblies )
67
+ {
68
+ foreach ( var assembly in assemblies )
69
+ {
70
+ IEnumerable < Type > definedTypes = null ;
71
+
72
+ try
73
+ {
74
+ definedTypes = assembly . GetTypes ( ) ;
75
+ }
76
+ catch ( Exception )
77
+ { }
78
+
79
+ if ( definedTypes != null )
80
+ {
81
+ foreach ( var definedType in definedTypes )
82
+ {
83
+ yield return definedType ;
84
+ }
85
+ }
86
+ }
87
+ }
88
+ #endif
40
89
}
41
90
}
42
91
#endif
0 commit comments