11#if ! ( WINDOWS_APP || UAP10_0 )
22using System . Collections . Generic ;
3+ using System . Reflection ;
34
45namespace System . Linq . Dynamic . Core . CustomTypeProviders
56{
@@ -22,21 +23,69 @@ public virtual HashSet<Type> GetCustomTypes()
2223 return _customTypes ?? ( _customTypes = new HashSet < Type > ( FindTypesMarkedWithAttribute ( ) ) ) ;
2324 }
2425
25- private IEnumerable < Type > FindTypesMarkedWithAttribute ( )
26+ protected IEnumerable < Type > FindTypesMarkedWithAttribute ( )
2627 {
27- var assemblies = _assemblyHelper . GetAssemblies ( ) ;
28+ IEnumerable < Assembly > assemblies = _assemblyHelper . GetAssemblies ( ) ;
2829#if ! NET35
29- assemblies = assemblies . Where ( x => ! x . IsDynamic ) . ToArray ( ) ;
30+ assemblies = assemblies . Where ( x => ! x . IsDynamic ) ;
3031#endif
3132
33+ var definedTypes = ExceptionFriedlyGetAssemblyTypes ( assemblies ) ;
34+
3235#if ( WINDOWS_APP || DOTNET5_1 || UAP10_0 || NETSTANDARD )
33- var definedTypes = assemblies . SelectMany ( x => x . DefinedTypes ) ;
3436 return definedTypes . Where ( x => x . CustomAttributes . Any ( y => y . AttributeType == typeof ( DynamicLinqTypeAttribute ) ) ) . Select ( x => x . AsType ( ) ) ;
3537#else
36- var definedTypes = assemblies . SelectMany ( x => x . GetTypes ( ) ) ;
3738 return definedTypes . Where ( x => x . GetCustomAttributes ( typeof ( DynamicLinqTypeAttribute ) , false ) . Any ( ) ) ;
3839#endif
3940 }
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
4089 }
4190}
4291#endif
0 commit comments