@@ -16,7 +16,7 @@ public static class ConsoleAppEngineHostBuilderExtensions
1616 /// <summary>
1717 /// Setup multiple ConsoleApp that are searched from all assemblies.
1818 /// </summary>
19- public static IHostBuilder UseConsoleAppFramework ( this IHostBuilder hostBuilder , string [ ] args , IConsoleAppInterceptor ? interceptor = null )
19+ public static IHostBuilder UseConsoleAppFramework ( this IHostBuilder hostBuilder , string [ ] args , IConsoleAppInterceptor ? interceptor = null , Assembly [ ] ? searchAssemblies = null )
2020 {
2121 IHostBuilder ConfigureEmptyService ( )
2222 {
@@ -28,10 +28,12 @@ IHostBuilder ConfigureEmptyService()
2828 return hostBuilder ;
2929 }
3030
31+ searchAssemblies ??= AppDomain . CurrentDomain . GetAssemblies ( ) ;
32+
3133 // () or -help
3234 if ( args . Length == 0 || ( args . Length == 1 && TrimEquals ( args [ 0 ] , HelpCommand ) ) )
3335 {
34- ShowMethodList ( ) ;
36+ ShowMethodList ( searchAssemblies ) ;
3537 ConfigureEmptyService ( ) ;
3638 return hostBuilder ;
3739 }
@@ -61,7 +63,7 @@ IHostBuilder ConfigureEmptyService()
6163
6264 if ( methodIndex != - 1 )
6365 {
64- var ( t , mi ) = GetTypeFromAssemblies ( args [ methodIndex ] , null ) ;
66+ var ( t , mi ) = GetTypeFromAssemblies ( args [ methodIndex ] , null , searchAssemblies ) ;
6567 if ( mi != null )
6668 {
6769 Console . Write ( new CommandHelpBuilder ( ) . BuildHelpMessage ( mi , showCommandName : true ) ) ;
@@ -79,7 +81,7 @@ IHostBuilder ConfigureEmptyService()
7981 MethodInfo ? methodInfo = null ;
8082 if ( args . Length >= 1 )
8183 {
82- ( type , methodInfo ) = GetTypeFromAssemblies ( args [ 0 ] , null ) ;
84+ ( type , methodInfo ) = GetTypeFromAssemblies ( args [ 0 ] , null , searchAssemblies ) ;
8385 }
8486
8587 hostBuilder = hostBuilder
@@ -111,15 +113,15 @@ IHostBuilder ConfigureEmptyService()
111113 /// <summary>
112114 /// Run multiple ConsoleApp that are searched from all assemblies.
113115 /// </summary>
114- public static Task RunConsoleAppFrameworkAsync ( this IHostBuilder hostBuilder , string [ ] args , IConsoleAppInterceptor ? interceptor = null )
116+ public static Task RunConsoleAppFrameworkAsync ( this IHostBuilder hostBuilder , string [ ] args , IConsoleAppInterceptor ? interceptor = null , Assembly [ ] ? searchAssemblies = null )
115117 {
116- return UseConsoleAppFramework ( hostBuilder , args , interceptor ) . Build ( ) . RunAsync ( ) ;
118+ return UseConsoleAppFramework ( hostBuilder , args , interceptor , searchAssemblies ) . Build ( ) . RunAsync ( ) ;
117119 }
118120
119121 /// <summary>
120122 /// Setup a single ConsoleApp type that is targeted by type argument.
121123 /// </summary>
122- public static IHostBuilder UseConsoleAppFramework < T > ( this IHostBuilder hostBuilder , string [ ] args , IConsoleAppInterceptor ? interceptor = null )
124+ public static IHostBuilder UseConsoleAppFramework < T > ( this IHostBuilder hostBuilder , string [ ] args , IConsoleAppInterceptor ? interceptor = null , Assembly [ ] ? searchAssemblies = null )
123125 where T : ConsoleAppBase
124126 {
125127 IHostBuilder ConfigureEmptyService ( )
@@ -132,6 +134,8 @@ IHostBuilder ConfigureEmptyService()
132134 return hostBuilder ;
133135 }
134136
137+ searchAssemblies ??= AppDomain . CurrentDomain . GetAssemblies ( ) ;
138+
135139 var methods = typeof ( T ) . GetMethods ( BindingFlags . Public | BindingFlags . Instance | BindingFlags . DeclaredOnly ) ;
136140 var defaultMethod = methods . FirstOrDefault ( x => x . GetCustomAttribute < CommandAttribute > ( ) == null ) ;
137141 var hasHelp = methods . Any ( x => x . GetCustomAttribute < CommandAttribute > ( ) ? . EqualsAny ( HelpCommand ) ?? false ) ;
@@ -186,7 +190,7 @@ IHostBuilder ConfigureEmptyService()
186190
187191 if ( methodIndex != - 1 )
188192 {
189- var ( _, mi ) = GetTypeFromAssemblies ( args [ methodIndex ] , typeof ( T ) ) ;
193+ var ( _, mi ) = GetTypeFromAssemblies ( args [ methodIndex ] , typeof ( T ) , searchAssemblies ) ;
190194 if ( mi != null )
191195 {
192196 Console . Write ( new CommandHelpBuilder ( ) . BuildHelpMessage ( mi , showCommandName : true ) ) ;
@@ -248,16 +252,16 @@ static void ShowVersion()
248252 Console . WriteLine ( version ) ;
249253 }
250254
251- static void ShowMethodList ( )
255+ static void ShowMethodList ( Assembly [ ] searchAssemblies )
252256 {
253- Console . Write ( new CommandHelpBuilder ( ) . BuildHelpMessage ( GetConsoleAppTypes ( ) ) ) ;
257+ Console . Write ( new CommandHelpBuilder ( ) . BuildHelpMessage ( GetConsoleAppTypes ( searchAssemblies ) ) ) ;
254258 }
255259
256- static List < Type > GetConsoleAppTypes ( )
260+ static List < Type > GetConsoleAppTypes ( Assembly [ ] searchAssemblies )
257261 {
258262 List < Type > consoleAppBaseTypes = new List < Type > ( ) ;
259263
260- foreach ( var asm in AppDomain . CurrentDomain . GetAssemblies ( ) )
264+ foreach ( var asm in searchAssemblies )
261265 {
262266 if ( asm . FullName . StartsWith ( "System" ) || asm . FullName . StartsWith ( "Microsoft.Extensions" ) ) continue ;
263267
@@ -283,10 +287,10 @@ static List<Type> GetConsoleAppTypes()
283287 return consoleAppBaseTypes ;
284288 }
285289
286- static ( Type ? , MethodInfo ? ) GetTypeFromAssemblies ( string arg0 , Type ? defaultBaseType )
290+ static ( Type ? , MethodInfo ? ) GetTypeFromAssemblies ( string arg0 , Type ? defaultBaseType , Assembly [ ] searchAssemblies )
287291 {
288292 var consoleAppBaseTypes = ( defaultBaseType == null )
289- ? GetConsoleAppTypes ( )
293+ ? GetConsoleAppTypes ( searchAssemblies )
290294 : new List < Type > { defaultBaseType } ;
291295
292296 if ( consoleAppBaseTypes == null )
0 commit comments