1313
1414namespace Shuttle . Core . Reflection
1515{
16- public class ReflectionService : IReflectionService
17- {
16+ public class ReflectionService : IReflectionService
17+ {
1818 private readonly List < string > _assemblyExtensions = new List < string >
1919 {
2020 ".dll" ,
2121 ".exe"
2222 } ;
2323
24- private readonly ILog _log ;
24+ private readonly ILog _log ;
2525
26- public ReflectionService ( )
27- {
28- _log = Log . For ( this ) ;
29- }
26+ public ReflectionService ( )
27+ {
28+ _log = Log . For ( this ) ;
29+ }
3030
31- public string AssemblyPath ( Assembly assembly )
32- {
33- Guard . AgainstNull ( assembly , nameof ( assembly ) ) ;
31+ public string AssemblyPath ( Assembly assembly )
32+ {
33+ Guard . AgainstNull ( assembly , nameof ( assembly ) ) ;
3434
35- return ! assembly . IsDynamic
36- ? new Uri ( Uri . UnescapeDataString ( new UriBuilder ( assembly . CodeBase ) . Path ) ) . LocalPath
37- : string . Empty ;
38- }
35+ return ! assembly . IsDynamic
36+ ? new Uri ( Uri . UnescapeDataString ( new UriBuilder ( assembly . CodeBase ) . Path ) ) . LocalPath
37+ : string . Empty ;
38+ }
3939
40- public Assembly GetAssembly ( string assemblyPath )
41- {
40+ public Assembly GetAssembly ( string assemblyPath )
41+ {
4242 var result = GetRuntimeAssemblies ( )
4343 . FirstOrDefault ( assembly => AssemblyPath ( assembly )
4444 . Equals ( assemblyPath , StringComparison . InvariantCultureIgnoreCase ) ) ;
4545
46- if ( result != null )
47- {
48- return result ;
49- }
50-
51- try
52- {
53- switch ( Path . GetExtension ( assemblyPath ) )
54- {
55- case ".dll" :
56- case ".exe" :
57- {
58- result = Path . GetDirectoryName ( assemblyPath ) == AppDomain . CurrentDomain . BaseDirectory
59- ? Assembly . Load ( Path . GetFileNameWithoutExtension ( assemblyPath ) ?? throw new InvalidOperationException ( string . Format ( Resources . GetFileNameWithoutExtensionException , assemblyPath ) ) )
60- : Assembly . LoadFile ( assemblyPath ) ;
61- break ;
62- }
63-
64- default :
65- {
66- result = Assembly . Load ( assemblyPath ) ;
67-
68- break ;
69- }
70- }
71- }
72- catch ( Exception ex )
73- {
74- _log . Warning ( string . Format ( Resources . AssemblyLoadException , assemblyPath , ex . Message ) ) ;
75-
76- if ( ex is ReflectionTypeLoadException reflection )
77- {
78- foreach ( var exception in reflection . LoaderExceptions )
79- {
46+ if ( result != null )
47+ {
48+ return result ;
49+ }
50+
51+ try
52+ {
53+ switch ( Path . GetExtension ( assemblyPath ) )
54+ {
55+ case ".dll" :
56+ case ".exe" :
57+ {
58+ result = Path . GetDirectoryName ( assemblyPath ) == AppDomain . CurrentDomain . BaseDirectory
59+ ? Assembly . Load ( Path . GetFileNameWithoutExtension ( assemblyPath ) ??
60+ throw new InvalidOperationException ( string . Format ( Resources . GetFileNameWithoutExtensionException , assemblyPath ) ) )
61+ : Assembly . LoadFile ( assemblyPath ) ;
62+ break ;
63+ }
64+
65+ default :
66+ {
67+ result = Assembly . Load ( assemblyPath ) ;
68+
69+ break ;
70+ }
71+ }
72+ }
73+ catch ( Exception ex )
74+ {
75+ _log . Warning ( string . Format ( Resources . AssemblyLoadException , assemblyPath , ex . Message ) ) ;
76+
77+ if ( ex is ReflectionTypeLoadException reflection )
78+ {
79+ foreach ( var exception in reflection . LoaderExceptions )
80+ {
8081 _log . Trace ( $ "'{ exception . Message } '.") ;
81- }
82- }
83- else
84- {
82+ }
83+ }
84+ else
85+ {
8586 _log . Trace ( $ "{ ex . GetType ( ) } : '{ ex . Message } '.") ;
86- }
87+ }
8788
88- return null ;
89- }
89+ return null ;
90+ }
9091
91- return result ;
92+ return result ;
9293 }
9394
9495 public Assembly FindAssemblyNamed ( string name )
@@ -165,7 +166,7 @@ public IEnumerable<Assembly> GetAssemblies()
165166 public IEnumerable < Assembly > GetMatchingAssemblies ( string regex , string folder )
166167 {
167168 return GetMatchingAssemblies ( new Regex ( regex , RegexOptions . IgnoreCase ) , folder ) ;
168- }
169+ }
169170
170171 private IEnumerable < Assembly > GetMatchingAssemblies ( Regex expression , string folder )
171172 {
@@ -192,35 +193,35 @@ public IEnumerable<Assembly> GetMatchingAssemblies(string regex)
192193 {
193194 var assemblies = new List < Assembly > ( GetRuntimeAssemblies ( ) ) ;
194195
195- foreach (
196- var assembly in
196+ foreach (
197+ var assembly in
197198 GetMatchingAssemblies ( regex , AppDomain . CurrentDomain . BaseDirectory )
198- . Where ( assembly => assemblies . Find ( candidate => candidate . Equals ( assembly ) ) == null ) )
199- {
200- assemblies . Add ( assembly ) ;
201- }
202-
203- var privateBinPath = Path . Combine ( AppDomain . CurrentDomain . BaseDirectory ,
204- AppDomain . CurrentDomain . RelativeSearchPath ?? string . Empty ) ;
205-
206- if ( ! privateBinPath . Equals ( AppDomain . CurrentDomain . BaseDirectory ) )
207- {
208- foreach (
209- var assembly in
199+ . Where ( assembly => assemblies . Find ( candidate => candidate . Equals ( assembly ) ) == null ) )
200+ {
201+ assemblies . Add ( assembly ) ;
202+ }
203+
204+ var privateBinPath = Path . Combine ( AppDomain . CurrentDomain . BaseDirectory ,
205+ AppDomain . CurrentDomain . RelativeSearchPath ?? string . Empty ) ;
206+
207+ if ( ! privateBinPath . Equals ( AppDomain . CurrentDomain . BaseDirectory ) )
208+ {
209+ foreach (
210+ var assembly in
210211 GetMatchingAssemblies ( regex , privateBinPath )
211- . Where ( assembly => assemblies . Find ( candidate => candidate . Equals ( assembly ) ) == null ) )
212- {
213- assemblies . Add ( assembly ) ;
214- }
215- }
212+ . Where ( assembly => assemblies . Find ( candidate => candidate . Equals ( assembly ) ) == null ) )
213+ {
214+ assemblies . Add ( assembly ) ;
215+ }
216+ }
216217
217- return assemblies ;
218- }
218+ return assemblies ;
219+ }
219220
220- public IEnumerable < Assembly > GetRuntimeAssemblies ( )
221- {
221+ public IEnumerable < Assembly > GetRuntimeAssemblies ( )
222+ {
222223#if ( ! NETCOREAPP2_0 && ! NETSTANDARD2_0 )
223- return AppDomain . CurrentDomain . GetAssemblies ( ) ;
224+ return AppDomain . CurrentDomain . GetAssemblies ( ) ;
224225#else
225226 var result = new List < Assembly > ( ) ;
226227
@@ -234,66 +235,66 @@ public IEnumerable<Assembly> GetRuntimeAssemblies()
234235 }
235236
236237 public IEnumerable < Type > GetTypesAssignableTo < T > ( )
237- {
238- return GetTypesAssignableTo ( typeof ( T ) ) ;
239- }
240-
241- public IEnumerable < Type > GetTypesAssignableTo ( Type type )
242- {
243- var result = new List < Type > ( ) ;
244-
245- foreach ( var assembly in GetAssemblies ( ) )
246- {
247- GetTypesAssignableTo ( type , assembly )
248- . Where ( candidate => result . Find ( existing => existing == candidate ) == null )
249- . ToList ( )
250- . ForEach ( add => result . Add ( add ) ) ;
251- }
252-
253- return result ;
254- }
255-
256- public IEnumerable < Type > GetTypesAssignableTo < T > ( Assembly assembly )
257- {
258- return GetTypesAssignableTo ( typeof ( T ) , assembly ) ;
259- }
260-
261- public IEnumerable < Type > GetTypesAssignableTo ( Type type , Assembly assembly )
262- {
263- Guard . AgainstNull ( type , nameof ( type ) ) ;
264- Guard . AgainstNull ( assembly , nameof ( assembly ) ) ;
265-
266- return GetTypes ( assembly ) . Where ( candidate => candidate . IsAssignableTo ( type ) && ! ( candidate . IsInterface && candidate == type ) ) . ToList ( ) ;
267- }
268-
269- public IEnumerable < Type > GetTypes ( Assembly assembly )
270- {
271- Type [ ] types ;
272-
273- try
274- {
275- _log . Trace ( string . Format ( Resources . TraceGetTypesFromAssembly , assembly ) ) ;
276-
277- types = assembly . GetTypes ( ) ;
278- }
279- catch ( Exception ex )
280- {
281- if ( ex is ReflectionTypeLoadException reflection )
282- {
283- foreach ( var exception in reflection . LoaderExceptions )
284- {
238+ {
239+ return GetTypesAssignableTo ( typeof ( T ) ) ;
240+ }
241+
242+ public IEnumerable < Type > GetTypesAssignableTo ( Type type )
243+ {
244+ var result = new List < Type > ( ) ;
245+
246+ foreach ( var assembly in GetAssemblies ( ) )
247+ {
248+ GetTypesAssignableTo ( type , assembly )
249+ . Where ( candidate => result . Find ( existing => existing == candidate ) == null )
250+ . ToList ( )
251+ . ForEach ( add => result . Add ( add ) ) ;
252+ }
253+
254+ return result ;
255+ }
256+
257+ public IEnumerable < Type > GetTypesAssignableTo < T > ( Assembly assembly )
258+ {
259+ return GetTypesAssignableTo ( typeof ( T ) , assembly ) ;
260+ }
261+
262+ public IEnumerable < Type > GetTypesAssignableTo ( Type type , Assembly assembly )
263+ {
264+ Guard . AgainstNull ( type , nameof ( type ) ) ;
265+ Guard . AgainstNull ( assembly , nameof ( assembly ) ) ;
266+
267+ return GetTypes ( assembly ) . Where ( candidate => candidate . IsAssignableTo ( type ) && ! ( candidate . IsInterface && candidate == type ) ) . ToList ( ) ;
268+ }
269+
270+ public IEnumerable < Type > GetTypes ( Assembly assembly )
271+ {
272+ Type [ ] types ;
273+
274+ try
275+ {
276+ _log . Trace ( string . Format ( Resources . TraceGetTypesFromAssembly , assembly ) ) ;
277+
278+ types = assembly . GetTypes ( ) ;
279+ }
280+ catch ( Exception ex )
281+ {
282+ if ( ex is ReflectionTypeLoadException reflection )
283+ {
284+ foreach ( var exception in reflection . LoaderExceptions )
285+ {
285286 _log . Error ( $ "'{ exception . Message } '.") ;
286- }
287- }
288- else
289- {
287+ }
288+ }
289+ else
290+ {
290291 _log . Error ( $ "{ ex . GetType ( ) } : '{ ex . Message } '.") ;
291- }
292+ }
292293
293- return new List < Type > ( ) ;
294- }
294+ return new List < Type > ( ) ;
295+ }
295296
296- return types ;
297- }
298- }
297+ return types ;
298+ }
299+ }
299300}
0 commit comments