@@ -39,7 +39,6 @@ internal class PsesInternalHost : PSHost, IHostSupportsInteractiveSession, IRuns
39
39
40
40
private static readonly PropertyInfo s_scriptDebuggerTriggerObjectProperty ;
41
41
42
- #if ! CoreCLR
43
42
/// <summary>
44
43
/// To workaround a horrid bug where the `TranscribeOnly` field of the PSHostUserInterface
45
44
/// can accidentally remain true, we have to use a bunch of reflection so that <see
@@ -62,7 +61,6 @@ internal class PsesInternalHost : PSHost, IHostSupportsInteractiveSession, IRuns
62
61
private static readonly PropertyInfo s_executionContextProperty ;
63
62
64
63
private static readonly PropertyInfo s_internalHostProperty ;
65
- #endif
66
64
67
65
private readonly ILoggerFactory _loggerFactory ;
68
66
@@ -130,7 +128,12 @@ static PsesInternalHost()
130
128
"TriggerObject" ,
131
129
BindingFlags . Instance | BindingFlags . NonPublic ) ;
132
130
133
- #if ! CoreCLR
131
+ if ( VersionUtils . IsNetCore )
132
+ {
133
+ // The following reflection methods are only needed for the .NET Framework.
134
+ return ;
135
+ }
136
+
134
137
PropertyInfo transcribeOnlyProperty = typeof ( PSHostUserInterface )
135
138
. GetProperty ( "TranscribeOnly" , BindingFlags . NonPublic | BindingFlags . Instance ) ;
136
139
@@ -144,12 +147,11 @@ static PsesInternalHost()
144
147
s_setTranscribeOnlyDelegate = ( Action < PSHostUserInterface , bool > ) Delegate . CreateDelegate (
145
148
typeof ( Action < PSHostUserInterface , bool > ) , transcribeOnlySetMethod ) ;
146
149
147
- s_executionContextProperty = typeof ( System . Management . Automation . Runspaces . Runspace )
150
+ s_executionContextProperty = typeof ( Runspace )
148
151
. GetProperty ( "ExecutionContext" , BindingFlags . NonPublic | BindingFlags . Instance ) ;
149
152
150
153
s_internalHostProperty = s_executionContextProperty . PropertyType
151
154
. GetProperty ( "InternalHost" , BindingFlags . NonPublic | BindingFlags . Instance ) ;
152
- #endif
153
155
}
154
156
155
157
public PsesInternalHost (
@@ -544,23 +546,29 @@ public void InvokePSDelegate(string representation, ExecutionOptions executionOp
544
546
// This works around a bug in PowerShell 5.1 (that was later fixed) where a running
545
547
// transcription could cause output to disappear since the `TranscribeOnly` property was
546
548
// accidentally not reset to false.
547
- #pragma warning disable CA1822 // Warning to make it static when it's empty for CoreCLR.
548
549
internal void DisableTranscribeOnly ( )
549
- #pragma warning restore CA1822
550
550
{
551
- #if ! CoreCLR
551
+ if ( VersionUtils . IsNetCore )
552
+ {
553
+ return ;
554
+ }
555
+
552
556
// To fix the TranscribeOnly bug, we have to get the internal UI, which involves a lot
553
557
// of reflection since we can't always just use PowerShell to execute `$Host.UI`.
554
558
s_internalPSHostUserInterface ??=
555
559
( s_internalHostProperty . GetValue (
556
560
s_executionContextProperty . GetValue ( CurrentPowerShell . Runspace ) )
557
- as PSHost ) . UI ;
561
+ as PSHost ) ? . UI ;
562
+
563
+ if ( s_internalPSHostUserInterface is null )
564
+ {
565
+ return ;
566
+ }
558
567
559
568
if ( s_getTranscribeOnlyDelegate ( s_internalPSHostUserInterface ) )
560
569
{
561
570
s_setTranscribeOnlyDelegate ( s_internalPSHostUserInterface , false ) ;
562
571
}
563
- #endif
564
572
}
565
573
566
574
internal Task LoadHostProfilesAsync ( CancellationToken cancellationToken )
0 commit comments