@@ -89,8 +89,9 @@ public void AddSubscriber(IHostIOSubscriber subscriber)
8989 public void AttachToHost ( PSHost host )
9090 {
9191 if ( this . host != null ) { return ; }
92+ if ( host == null ) { return ; }
9293
93- var flags = BindingFlags . Instance | BindingFlags . NonPublic ;
94+ const BindingFlags flags = BindingFlags . Instance | BindingFlags . NonPublic ;
9495
9596 object uiRef = host . GetType ( ) . GetField ( "internalUIRef" , flags ) . GetValue ( host ) ;
9697 object ui = uiRef . GetType ( ) . GetProperty ( "Value" , flags ) . GetValue ( uiRef , null ) ;
@@ -106,7 +107,7 @@ public void DetachFromHost()
106107 {
107108 if ( host == null ) { return ; }
108109
109- var flags = BindingFlags . Instance | BindingFlags . NonPublic ;
110+ const BindingFlags flags = BindingFlags . Instance | BindingFlags . NonPublic ;
110111
111112 object uiRef = host . GetType ( ) . GetField ( "internalUIRef" , flags ) . GetValue ( host ) ;
112113 object ui = uiRef . GetType ( ) . GetProperty ( "Value" , flags ) . GetValue ( uiRef , null ) ;
@@ -133,7 +134,7 @@ public override Dictionary<string, PSObject> Prompt(string caption,
133134
134135 Dictionary < string , PSObject > result = externalUI . Prompt ( caption , message , descriptions ) ;
135136
136- SendToSubscribers ( "Prompt" , result ) ;
137+ SendToSubscribers ( s => s . Prompt ( result ) ) ;
137138
138139 return result ;
139140 }
@@ -150,7 +151,7 @@ public override int PromptForChoice(string caption,
150151
151152 int result = externalUI . PromptForChoice ( caption , message , choices , defaultChoice ) ;
152153
153- SendToSubscribers ( "ChoicePrompt" , choices [ result ] ) ;
154+ SendToSubscribers ( s => s . ChoicePrompt ( choices [ result ] ) ) ;
154155
155156 return result ;
156157 }
@@ -167,7 +168,7 @@ public override PSCredential PromptForCredential(string caption,
167168
168169 PSCredential result = externalUI . PromptForCredential ( caption , message , userName , targetName ) ;
169170
170- SendToSubscribers ( "CredentialPrompt" , result ) ;
171+ SendToSubscribers ( s => s . CredentialPrompt ( result ) ) ;
171172
172173 return result ;
173174 }
@@ -191,7 +192,7 @@ public override PSCredential PromptForCredential(string caption,
191192 allowedCredentialTypes ,
192193 options ) ;
193194
194- SendToSubscribers ( "CredentialPrompt" , result ) ;
195+ SendToSubscribers ( s => s . CredentialPrompt ( result ) ) ;
195196
196197 return result ;
197198 }
@@ -205,7 +206,7 @@ public override string ReadLine()
205206
206207 string result = externalUI . ReadLine ( ) ;
207208
208- SendToSubscribers ( "ReadFromHost" , result ) ;
209+ SendToSubscribers ( s => s . ReadFromHost ( result ) ) ;
209210
210211 return result ;
211212 }
@@ -283,7 +284,8 @@ public override void WriteDebugLine(string message)
283284 string [ ] lines = message . Split ( new [ ] { "\r \n " , "\n " } , StringSplitOptions . None ) ;
284285 foreach ( string line in lines )
285286 {
286- SendToSubscribers ( "WriteDebug" , line . TrimEnd ( ) + "\r \n " ) ;
287+ string temp = line ;
288+ SendToSubscribers ( s => s . WriteDebug ( temp . TrimEnd ( ) + "\r \n " ) ) ;
287289 }
288290
289291 externalUI . WriteDebugLine ( message ) ;
@@ -299,7 +301,8 @@ public override void WriteErrorLine(string message)
299301 string [ ] lines = message . Split ( new [ ] { "\r \n " , "\n " } , StringSplitOptions . None ) ;
300302 foreach ( string line in lines )
301303 {
302- SendToSubscribers ( "WriteError" , line . TrimEnd ( ) + "\r \n " ) ;
304+ string temp = line ;
305+ SendToSubscribers ( s => s . WriteError ( temp . TrimEnd ( ) + "\r \n " ) ) ;
303306 }
304307
305308 externalUI . WriteErrorLine ( message ) ;
@@ -315,7 +318,8 @@ public override void WriteLine()
315318 string [ ] lines = writeCache . ToString ( ) . Split ( new [ ] { "\r \n " , "\n " } , StringSplitOptions . None ) ;
316319 foreach ( string line in lines )
317320 {
318- SendToSubscribers ( "WriteOutput" , line . TrimEnd ( ) + "\r \n " ) ;
321+ string temp = line ;
322+ SendToSubscribers ( s => s . WriteOutput ( temp . TrimEnd ( ) + "\r \n " ) ) ;
319323 }
320324
321325 writeCache . Length = 0 ;
@@ -332,7 +336,8 @@ public override void WriteLine(string value)
332336 string [ ] lines = ( writeCache + value ) . Split ( new [ ] { "\r \n " , "\n " } , StringSplitOptions . None ) ;
333337 foreach ( string line in lines )
334338 {
335- SendToSubscribers ( "WriteOutput" , line . TrimEnd ( ) + "\r \n " ) ;
339+ string temp = line ;
340+ SendToSubscribers ( s => s . WriteOutput ( temp . TrimEnd ( ) + "\r \n " ) ) ;
336341 }
337342
338343 writeCache . Length = 0 ;
@@ -349,7 +354,8 @@ public override void WriteLine(ConsoleColor foregroundColor, ConsoleColor backgr
349354 string [ ] lines = ( writeCache + value ) . Split ( new [ ] { "\r \n " , "\n " } , StringSplitOptions . None ) ;
350355 foreach ( string line in lines )
351356 {
352- SendToSubscribers ( "WriteOutput" , line . TrimEnd ( ) + "\r \n " ) ;
357+ string temp = line ;
358+ SendToSubscribers ( s => s . WriteOutput ( temp . TrimEnd ( ) + "\r \n " ) ) ;
353359 }
354360
355361 writeCache . Length = 0 ;
@@ -363,7 +369,7 @@ public override void WriteProgress(long sourceId, ProgressRecord record)
363369 throw new InvalidOperationException ( ) ;
364370 }
365371
366- SendToSubscribers ( "WriteProgress" , sourceId , record ) ;
372+ SendToSubscribers ( s => s . WriteProgress ( sourceId , record ) ) ;
367373
368374 externalUI . WriteProgress ( sourceId , record ) ;
369375 }
@@ -378,7 +384,8 @@ public override void WriteVerboseLine(string message)
378384 string [ ] lines = message . Split ( new [ ] { "\r \n " , "\n " } , StringSplitOptions . None ) ;
379385 foreach ( string line in lines )
380386 {
381- SendToSubscribers ( "WriteVerbose" , line . TrimEnd ( ) + "\r \n " ) ;
387+ string temp = line ;
388+ SendToSubscribers ( s => s . WriteVerbose ( temp . TrimEnd ( ) + "\r \n " ) ) ;
382389 }
383390
384391 externalUI . WriteVerboseLine ( message ) ;
@@ -394,7 +401,8 @@ public override void WriteWarningLine(string message)
394401 string [ ] lines = message . Split ( new [ ] { "\r \n " , "\n " } , StringSplitOptions . None ) ;
395402 foreach ( string line in lines )
396403 {
397- SendToSubscribers ( "WriteWarning" , line . TrimEnd ( ) + "\r \n " ) ;
404+ string temp = line ;
405+ SendToSubscribers ( s => s . WriteWarning ( temp . TrimEnd ( ) + "\r \n " ) ) ;
398406 }
399407
400408 externalUI . WriteWarningLine ( message ) ;
@@ -404,26 +412,9 @@ public override void WriteWarningLine(string message)
404412
405413 #region Private Methods
406414
407- private void SendToSubscribers ( string methodName , params object [ ] args )
415+ public void SendToSubscribers ( Action < IHostIOSubscriber > action )
408416 {
409- // Refactored the duplicate code that enumerates the _subscribers list and removes dead
410- // references in to this method. It uses Reflection to invoke the methods on
411- // subscriber objects because I'm not sure how to accomplish the same thing using
412- // delegates (assuming it is even possible), when the target methods have different
413- // signatures.
414-
415- if ( paused )
416- {
417- return ;
418- }
419-
420- MethodInfo method = typeof ( IHostIOSubscriber ) . GetMethod ( methodName ) ;
421- if ( method == null )
422- {
423- throw new ArgumentException (
424- "Method '" + methodName + "' does not exist in the IHostIoSubscriber interface." ,
425- "methodName" ) ;
426- }
417+ if ( paused ) { return ; }
427418
428419 var deadReferences = new List < WeakReference > ( ) ;
429420
@@ -436,7 +427,7 @@ private void SendToSubscribers(string methodName, params object[] args)
436427 }
437428 else
438429 {
439- method . Invoke ( subscriber , args ) ;
430+ action ( subscriber ) ;
440431 }
441432 }
442433
0 commit comments