@@ -25,8 +25,11 @@ public static int Main(string[] args)
25
25
26
26
private static string s_references = string . Empty ;
27
27
private static readonly Dictionary < string , string > s_referencedic = new Dictionary < string , string > ( StringComparer . OrdinalIgnoreCase ) ;
28
+ public static TextWriter InfoWriter { get ; internal set ; } = Console . Out ;
29
+ public static TextWriter WarningWriter { get ; internal set ; } = Console . Out ;
30
+ public static TextWriter ErrorWriter { get ; internal set ; } = Console . Error ;
28
31
29
- private int Run ( string [ ] args )
32
+ internal int Run ( string [ ] args )
30
33
{
31
34
string assembly = null ;
32
35
var types = new List < string > ( ) ;
@@ -177,15 +180,15 @@ private int Run(string[] args)
177
180
{
178
181
foreach ( string err in errs )
179
182
{
180
- Console . Error . WriteLine ( FormatMessage ( parsableErrors , true , SR . Format ( SR . Warning , err ) ) ) ;
183
+ ErrorWriter . WriteLine ( FormatMessage ( parsableErrors , true , SR . Format ( SR . Warning , err ) ) ) ;
181
184
}
182
185
}
183
186
184
187
if ( args . Length == 0 || assembly == null )
185
188
{
186
189
if ( assembly == null )
187
190
{
188
- Console . Error . WriteLine ( FormatMessage ( parsableErrors , false , SR . Format ( SR . ErrMissingRequiredArgument , SR . Format ( SR . ErrAssembly , "assembly" ) ) ) ) ;
191
+ ErrorWriter . WriteLine ( FormatMessage ( parsableErrors , false , SR . Format ( SR . ErrMissingRequiredArgument , SR . Format ( SR . ErrAssembly , "assembly" ) ) ) ) ;
189
192
}
190
193
191
194
WriteHelp ( ) ;
@@ -194,8 +197,8 @@ private int Run(string[] args)
194
197
195
198
if ( disableRun )
196
199
{
197
- Console . WriteLine ( "This tool is not intended to be used directly." ) ;
198
- Console . WriteLine ( "Please refer to https://go.microsoft.com/fwlink/?linkid=858594 on how to use it." ) ;
200
+ InfoWriter . WriteLine ( "This tool is not intended to be used directly." ) ;
201
+ InfoWriter . WriteLine ( "Please refer to https://go.microsoft.com/fwlink/?linkid=858594 on how to use it." ) ;
199
202
return 0 ;
200
203
}
201
204
@@ -254,7 +257,7 @@ private static void GenerateFile(List<string> typeNames, string defaultNamespace
254
257
Type type = assembly . GetType ( typeName ) ;
255
258
if ( type == null )
256
259
{
257
- Console . Error . WriteLine ( FormatMessage ( parsableerrors , false , SR . Format ( SR . ErrorDetails , SR . Format ( SR . ErrLoadType , typeName , assemblyName ) ) ) ) ;
260
+ ErrorWriter . WriteLine ( FormatMessage ( parsableerrors , false , SR . Format ( SR . ErrorDetails , SR . Format ( SR . ErrLoadType , typeName , assemblyName ) ) ) ) ;
258
261
}
259
262
260
263
types [ typeIndex ++ ] = type ;
@@ -275,7 +278,7 @@ private static void GenerateFile(List<string> typeNames, string defaultNamespace
275
278
{
276
279
if ( verbose )
277
280
{
278
- Console . WriteLine ( SR . Format ( SR . ImportInfo , type . Name , i + 1 , types . Length ) ) ;
281
+ InfoWriter . WriteLine ( SR . Format ( SR . ImportInfo , type . Name , i + 1 , types . Length ) ) ;
279
282
}
280
283
281
284
bool isObsolete = false ;
@@ -300,7 +303,7 @@ private static void GenerateFile(List<string> typeNames, string defaultNamespace
300
303
{
301
304
if ( verbose )
302
305
{
303
- Console . Out . WriteLine ( FormatMessage ( parsableerrors , true , SR . Format ( SR . InfoIgnoreType , type . FullName ) ) ) ;
306
+ InfoWriter . WriteLine ( FormatMessage ( parsableerrors , true , SR . Format ( SR . InfoIgnoreType , type . FullName ) ) ) ;
304
307
WriteWarning ( e , parsableerrors ) ;
305
308
}
306
309
@@ -372,7 +375,7 @@ private static void GenerateFile(List<string> typeNames, string defaultNamespace
372
375
373
376
if ( method == null )
374
377
{
375
- Console . Error . WriteLine ( FormatMessage ( parsableerrors : false , warning : false , message : SR . GenerateSerializerNotFound ) ) ;
378
+ ErrorWriter . WriteLine ( FormatMessage ( parsableerrors : false , warning : false , message : SR . GenerateSerializerNotFound ) ) ;
376
379
}
377
380
else
378
381
{
@@ -404,22 +407,21 @@ private static void GenerateFile(List<string> typeNames, string defaultNamespace
404
407
{
405
408
if ( ! silent )
406
409
{
407
- Console . Out . WriteLine ( SR . Format ( SR . InfoFileName , codePath ) ) ;
408
- Console . Out . WriteLine ( SR . Format ( SR . InfoGeneratedFile , assembly . Location , codePath ) ) ;
410
+ InfoWriter . WriteLine ( SR . Format ( SR . InfoFileName , codePath ) ) ;
411
+ InfoWriter . WriteLine ( SR . Format ( SR . InfoGeneratedFile , assembly . Location , codePath ) ) ;
409
412
}
410
413
}
411
414
else
412
415
{
413
- Console . Out . WriteLine ( FormatMessage ( parsableerrors , false , SR . Format ( SR . ErrGenerationFailed , assembly . Location ) ) ) ;
416
+ InfoWriter . WriteLine ( FormatMessage ( parsableerrors , false , SR . Format ( SR . ErrGenerationFailed , assembly . Location ) ) ) ;
414
417
}
415
418
}
416
419
else
417
420
{
418
- Console . Out . WriteLine ( FormatMessage ( parsableerrors , true , SR . Format ( SR . InfoNoSerializableTypes , assembly . Location ) ) ) ;
421
+ InfoWriter . WriteLine ( FormatMessage ( parsableerrors , true , SR . Format ( SR . InfoNoSerializableTypes , assembly . Location ) ) ) ;
419
422
}
420
423
}
421
424
422
-
423
425
private static bool ArgumentMatch ( string arg , string formal )
424
426
{
425
427
// Full name format, eg: --assembly
@@ -459,7 +461,7 @@ private static void ImportType(Type type, string defaultNamespace, List<XmlMappi
459
461
460
462
if ( verbose )
461
463
{
462
- Console . Out . WriteLine ( FormatMessage ( parsableerrors , true , SR . Format ( SR . InfoIgnoreType , type . FullName ) ) ) ;
464
+ InfoWriter . WriteLine ( FormatMessage ( parsableerrors , true , SR . Format ( SR . InfoIgnoreType , type . FullName ) ) ) ;
463
465
WriteWarning ( e , parsableerrors ) ;
464
466
}
465
467
@@ -489,22 +491,22 @@ private static Assembly LoadAssembly(string assemblyName)
489
491
private static void WriteHeader ( )
490
492
{
491
493
// do not localize Copyright header
492
- Console . WriteLine ( $ ".NET Xml Serialization Generation Utility, Version { ThisAssembly . InformationalVersion } ]") ;
494
+ InfoWriter . WriteLine ( $ ".NET Xml Serialization Generation Utility, Version { ThisAssembly . InformationalVersion } ]") ;
493
495
}
494
496
495
497
private void WriteHelp ( )
496
498
{
497
- Console . Out . WriteLine ( SR . HelpDescription ) ;
498
- Console . Out . WriteLine ( SR . Format ( SR . HelpUsage , this . GetType ( ) . Assembly . GetName ( ) . Name . Substring ( "dotnet-" . Length ) ) ) ;
499
- Console . Out . WriteLine ( SR . HelpDevOptions ) ;
500
- Console . Out . WriteLine ( SR . Format ( SR . HelpAssembly , "-a" , "--assembly" ) ) ;
501
- Console . Out . WriteLine ( SR . Format ( SR . HelpType , "--type" ) ) ;
502
- Console . Out . WriteLine ( SR . Format ( SR . HelpProxy , "--proxytypes" ) ) ;
503
- Console . Out . WriteLine ( SR . Format ( SR . HelpForce , "--force" ) ) ;
504
- Console . Out . WriteLine ( SR . Format ( SR . HelpOut , "-o" , "--out" ) ) ;
505
-
506
- Console . Out . WriteLine ( SR . HelpMiscOptions ) ;
507
- Console . Out . WriteLine ( SR . Format ( SR . HelpHelp , "-h" , "--help" ) ) ;
499
+ InfoWriter . WriteLine ( SR . HelpDescription ) ;
500
+ InfoWriter . WriteLine ( SR . Format ( SR . HelpUsage , this . GetType ( ) . Assembly . GetName ( ) . Name . Substring ( "dotnet-" . Length ) ) ) ;
501
+ InfoWriter . WriteLine ( SR . HelpDevOptions ) ;
502
+ InfoWriter . WriteLine ( SR . Format ( SR . HelpAssembly , "-a" , "--assembly" ) ) ;
503
+ InfoWriter . WriteLine ( SR . Format ( SR . HelpType , "--type" ) ) ;
504
+ InfoWriter . WriteLine ( SR . Format ( SR . HelpProxy , "--proxytypes" ) ) ;
505
+ InfoWriter . WriteLine ( SR . Format ( SR . HelpForce , "--force" ) ) ;
506
+ InfoWriter . WriteLine ( SR . Format ( SR . HelpOut , "-o" , "--out" ) ) ;
507
+
508
+ InfoWriter . WriteLine ( SR . HelpMiscOptions ) ;
509
+ InfoWriter . WriteLine ( SR . Format ( SR . HelpHelp , "-h" , "--help" ) ) ;
508
510
}
509
511
510
512
private static string FormatMessage ( bool parsableerrors , bool warning , string message )
@@ -524,7 +526,7 @@ private static string FormatMessage(bool parsableerrors, bool warning, string co
524
526
525
527
private static void WriteError ( Exception e , bool parsableerrors )
526
528
{
527
- Console . Error . WriteLine ( FormatMessage ( parsableerrors , false , e . Message ) ) ;
529
+ ErrorWriter . WriteLine ( FormatMessage ( parsableerrors , false , e . Message ) ) ;
528
530
if ( e . InnerException != null )
529
531
{
530
532
WriteError ( e . InnerException , parsableerrors ) ;
@@ -533,7 +535,7 @@ private static void WriteError(Exception e, bool parsableerrors)
533
535
534
536
private static void WriteWarning ( Exception e , bool parsableerrors )
535
537
{
536
- Console . Out . WriteLine ( FormatMessage ( parsableerrors , true , e . Message ) ) ;
538
+ WarningWriter . WriteLine ( FormatMessage ( parsableerrors , true , e . Message ) ) ;
537
539
if ( e . InnerException != null )
538
540
{
539
541
WriteWarning ( e . InnerException , parsableerrors ) ;
0 commit comments