44using System . Linq ;
55using System . Reflection ;
66using System . Text . RegularExpressions ;
7+ using System . Threading . Tasks ;
78using Microsoft . Extensions . DependencyModel ;
89using Shuttle . Core . Contract ;
910
@@ -30,11 +31,11 @@ public string AssemblyPath(Assembly assembly)
3031 : string . Empty ;
3132 }
3233
33- public Assembly GetAssembly ( string assemblyPath )
34+ public async Task < Assembly > GetAssembly ( string assemblyPath )
3435 {
35- var result = GetRuntimeAssemblies ( )
36- . FirstOrDefault ( assembly => AssemblyPath ( assembly )
37- . Equals ( assemblyPath , StringComparison . InvariantCultureIgnoreCase ) ) ;
36+ var assemblies = await GetRuntimeAssemblies ( ) . ConfigureAwait ( false ) ;
37+
38+ var result = assemblies . FirstOrDefault ( assembly => AssemblyPath ( assembly ) . Equals ( assemblyPath , StringComparison . InvariantCultureIgnoreCase ) ) ;
3839
3940 if ( result != null )
4041 {
@@ -47,13 +48,13 @@ public Assembly GetAssembly(string assemblyPath)
4748 }
4849 catch ( Exception ex )
4950 {
50- ExceptionRaised . Invoke ( this , new ExceptionRaisedEventArgs ( $ "GetAssembly({ assemblyPath } )", ex ) ) ;
51+ ExceptionRaised . Invoke ( this , new ExceptionRaisedEventArgs ( $ "GetAssembly({ assemblyPath } )", ex ) ) ;
5152
5253 return null ;
5354 }
5455 }
5556
56- public Assembly FindAssemblyNamed ( string name )
57+ public async Task < Assembly > FindAssemblyNamed ( string name )
5758 {
5859 Guard . AgainstNullOrEmptyString ( name , nameof ( name ) ) ;
5960
@@ -68,9 +69,10 @@ public Assembly FindAssemblyNamed(string name)
6869 hasFileExtension = true ;
6970 }
7071
71- var result = GetRuntimeAssemblies ( )
72- . FirstOrDefault ( assembly => assembly . GetName ( )
73- . Name . Equals ( assemblyName , StringComparison . InvariantCultureIgnoreCase ) ) ;
72+ var assemblies = await GetRuntimeAssemblies ( ) . ConfigureAwait ( false ) ;
73+
74+ var result = assemblies . FirstOrDefault ( assembly => assembly . GetName ( )
75+ . Name . Equals ( assemblyName , StringComparison . InvariantCultureIgnoreCase ) ) ;
7476
7577 if ( result != null )
7678 {
@@ -97,7 +99,7 @@ public Assembly FindAssemblyNamed(string name)
9799
98100 if ( File . Exists ( path ) )
99101 {
100- return GetAssembly ( path ) ;
102+ return await GetAssembly ( path ) ;
101103 }
102104
103105 if ( ! privateBinPath . Equals ( AppDomain . CurrentDomain . BaseDirectory ) )
@@ -106,27 +108,26 @@ public Assembly FindAssemblyNamed(string name)
106108
107109 if ( File . Exists ( path ) )
108110 {
109- return GetAssembly ( path ) ;
111+ return await GetAssembly ( path ) ;
110112 }
111113 }
112114 }
113115
114116 return null ;
115117 }
116118
117- public IEnumerable < Assembly > GetMatchingAssemblies ( Regex regex )
119+ public async Task < IEnumerable < Assembly > > GetMatchingAssemblies ( Regex regex )
118120 {
119121 Guard . AgainstNull ( regex , nameof ( regex ) ) ;
120122
121- var assemblies =
122- new List < Assembly > ( GetRuntimeAssemblies ( ) . Where ( assembly => regex . IsMatch ( assembly . FullName ) ) ) ;
123+ var result = ( await GetRuntimeAssemblies ( ) . ConfigureAwait ( false ) ) . Where ( assembly => regex . IsMatch ( assembly . FullName ) ) . ToList ( ) ;
123124
124125 foreach (
125126 var assembly in
126- GetMatchingAssemblies ( regex , AppDomain . CurrentDomain . BaseDirectory )
127- . Where ( assembly => assemblies . Find ( candidate => candidate . Equals ( assembly ) ) == null ) )
127+ ( await GetMatchingAssemblies ( regex , AppDomain . CurrentDomain . BaseDirectory ) . ConfigureAwait ( false ) )
128+ . Where ( assembly => result . Find ( candidate => candidate . Equals ( assembly ) ) == null ) )
128129 {
129- assemblies . Add ( assembly ) ;
130+ result . Add ( assembly ) ;
130131 }
131132
132133 var privateBinPath = Path . Combine ( AppDomain . CurrentDomain . BaseDirectory ,
@@ -136,25 +137,25 @@ var assembly in
136137 {
137138 foreach (
138139 var assembly in
139- GetMatchingAssemblies ( regex , privateBinPath )
140- . Where ( assembly => assemblies . Find ( candidate => candidate . Equals ( assembly ) ) == null ) )
140+ ( await GetMatchingAssemblies ( regex , privateBinPath ) . ConfigureAwait ( false ) )
141+ . Where ( assembly => result . Find ( candidate => candidate . Equals ( assembly ) ) == null ) )
141142 {
142- assemblies . Add ( assembly ) ;
143+ result . Add ( assembly ) ;
143144 }
144145 }
145146
146- return assemblies ;
147+ return result ;
147148 }
148149
149- public IEnumerable < Assembly > GetRuntimeAssemblies ( )
150+ public async Task < IEnumerable < Assembly > > GetRuntimeAssemblies ( )
150151 {
151152 var result = new List < Assembly > ( ) ;
152153 var dependencyContext = DependencyContext . Default ;
153154
154155 if ( dependencyContext != null )
155156 {
156157 foreach ( var runtimeAssemblyName in dependencyContext . GetRuntimeAssemblyNames ( Environment . OSVersion
157- . Platform . ToString ( ) ) )
158+ . Platform . ToString ( ) ) )
158159 {
159160 result . Add ( Assembly . Load ( runtimeAssemblyName ) ) ;
160161 }
@@ -168,44 +169,49 @@ public IEnumerable<Assembly> GetRuntimeAssemblies()
168169 }
169170 }
170171
171- return result ;
172+ return await Task . FromResult ( result ) ;
172173 }
173174
174- public IEnumerable < Type > GetTypesAssignableTo ( Type type )
175+ public async Task < IEnumerable < Type > > GetTypesAssignableTo ( Type type )
175176 {
176177 var result = new List < Type > ( ) ;
177178
178- foreach ( var assembly in this . GetAssemblies ( ) )
179+ var assemblies = await this . GetAssemblies ( ) . ConfigureAwait ( false ) ;
180+
181+ foreach ( var assembly in assemblies )
179182 {
180- GetTypesAssignableTo ( type , assembly )
181- . Where ( candidate => result . Find ( existing => existing == candidate ) == null )
183+ var types = await GetTypesAssignableTo ( type , assembly ) . ConfigureAwait ( false ) ;
184+
185+ types . Where ( candidate => result . Find ( existing => existing == candidate ) == null )
182186 . ToList ( )
183187 . ForEach ( add => result . Add ( add ) ) ;
184188 }
185189
186190 return result ;
187191 }
188192
189- public IEnumerable < Type > GetTypesAssignableTo ( Type type , Assembly assembly )
193+ public async Task < IEnumerable < Type > > GetTypesAssignableTo ( Type type , Assembly assembly )
190194 {
191195 Guard . AgainstNull ( type , nameof ( type ) ) ;
192196 Guard . AgainstNull ( assembly , nameof ( assembly ) ) ;
193197
194- return GetTypes ( assembly ) . Where ( candidate =>
198+ var types = await GetTypes ( assembly ) . ConfigureAwait ( false ) ;
199+
200+ return types . Where ( candidate =>
195201 candidate . IsAssignableTo ( type ) && ! ( candidate . IsInterface && candidate == type ) ) . ToList ( ) ;
196202 }
197203
198- public Type GetType ( string typeName )
204+ public async Task < Type > GetType ( string typeName )
199205 {
200- return Type . GetType ( typeName ,
206+ return await Task . FromResult ( Type . GetType ( typeName ,
201207 assemblyName =>
202208 {
203209 Assembly assembly ;
204210
205211 try
206212 {
207213 assembly = Assembly . LoadFrom (
208- Path . Combine ( string . IsNullOrEmpty ( AppDomain . CurrentDomain . RelativeSearchPath )
214+ Path . Combine ( string . IsNullOrEmpty ( AppDomain . CurrentDomain . RelativeSearchPath )
209215 ? AppDomain . CurrentDomain . BaseDirectory
210216 : AppDomain . CurrentDomain . RelativeSearchPath , $ "{ assemblyName . Name } .dll") ) ;
211217 }
@@ -219,45 +225,43 @@ public Type GetType(string typeName)
219225 } ,
220226 ( assembly , typeNameSought , ignore ) => assembly == null
221227 ? Type . GetType ( typeNameSought , false , ignore )
222- : assembly . GetType ( typeNameSought , false , ignore ) ) ;
228+ : assembly . GetType ( typeNameSought , false , ignore ) ) ) ;
223229 }
224230
225- public IEnumerable < Type > GetTypes ( Assembly assembly )
231+ public async Task < IEnumerable < Type > > GetTypes ( Assembly assembly )
226232 {
227233 Guard . AgainstNull ( assembly , nameof ( assembly ) ) ;
228234
229- Type [ ] types ;
235+ var types = Enumerable . Empty < Type > ( ) ;
230236
231237 try
232238 {
233- types = assembly . GetTypes ( ) ;
239+ types = assembly . GetTypes ( ) . ToList ( ) ;
234240 }
235241 catch ( Exception ex )
236242 {
237243 ExceptionRaised . Invoke ( this , new ExceptionRaisedEventArgs ( $ "GetTypes({ assembly . FullName } )", ex ) ) ;
238-
239- return new List < Type > ( ) ;
240244 }
241245
242- return types ;
246+ return await Task . FromResult ( types ) ;
243247 }
244248
245- private IEnumerable < Assembly > GetMatchingAssemblies ( Regex expression , string folder )
249+ private async Task < IEnumerable < Assembly > > GetMatchingAssemblies ( Regex expression , string folder )
246250 {
247251 var result = new List < Assembly > ( ) ;
248252
249253 if ( Directory . Exists ( folder ) )
250254 {
251- result . AddRange (
255+ result . AddRange ( await Task . WhenAll (
252256 Directory . GetFiles ( folder , "*.exe" )
253257 . Where ( file => expression . IsMatch ( Path . GetFileNameWithoutExtension ( file ) ) )
254258 . Select ( GetAssembly )
255- . Where ( assembly => assembly != null ) ) ;
256- result . AddRange (
259+ . Where ( assembly => assembly != null ) ) ) ;
260+ result . AddRange ( await Task . WhenAll (
257261 Directory . GetFiles ( folder , "*.dll" )
258262 . Where ( file => expression . IsMatch ( Path . GetFileNameWithoutExtension ( file ) ) )
259- . Select ( GetAssembly )
260- . Where ( assembly => assembly != null ) ) ;
263+ . Select ( assemblyPath => GetAssembly ( assemblyPath ) )
264+ . Where ( assembly => assembly != null ) ) ) ;
261265 }
262266
263267 return result ;
0 commit comments