public void AttachToHost(PSHost host) { if (this.host != null) { return; } if (host == null) { return; } const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic; // When PowerShell went open source, they renamed the private variables to have _underbarPrefixes if (host.Version >= new Version(6, 0)) { object uiRef = host.GetType().GetField("_internalUIRef", flags)?.GetValue(host); object ui = uiRef.GetType().GetProperty("Value", flags).GetValue(uiRef, null); FieldInfo externalUIField = ui.GetType().GetField("_externalUI", flags); externalUI = (PSHostUserInterface)externalUIField.GetValue(ui); externalUIField.SetValue(ui, this); } else { // Try the WindowsPowerShell version: object uiRef = host.GetType().GetField("internalUIRef", flags).GetValue(host); object ui = uiRef.GetType().GetProperty("Value", flags).GetValue(uiRef, null); FieldInfo externalUIField = ui.GetType().GetField("externalUI", flags); externalUI = (PSHostUserInterface)externalUIField.GetValue(ui); externalUIField.SetValue(ui, this); } this.host = host; }
public void DetachFromHost() { if (host == null) { return; } const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic; // When PowerShell went open source, they renamed the private variables to have _underbarPrefixes if (host.Version >= new Version(6, 0)) { object uiRef = host.GetType().GetField("_internalUIRef", flags)?.GetValue(host); object ui = uiRef.GetType().GetProperty("Value", flags).GetValue(uiRef, null); FieldInfo externalUIField = ui.GetType().GetField("_externalUI", flags); if (externalUIField.GetValue(ui) == this) { externalUIField.SetValue(ui, externalUI); } } else { // Try the WindowsPowerShell version: object uiRef = host.GetType().GetField("internalUIRef", flags).GetValue(host); object ui = uiRef.GetType().GetProperty("Value", flags).GetValue(uiRef, null); FieldInfo externalUIField = ui.GetType().GetField("externalUI", flags); if (externalUIField.GetValue(ui) == this) { externalUIField.SetValue(ui, externalUI); } } externalUI = null; host = null; }
public void AttachToHost(PSHost host) { if (this.host != null) { return; } if (host == null) { return; } const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic; object uiRef = host.GetType().GetField("internalUIRef", flags).GetValue(host); object ui = uiRef.GetType().GetProperty("Value", flags).GetValue(uiRef, null); FieldInfo externalUIField = ui.GetType().GetField("externalUI", flags); externalUI = (PSHostUserInterface)externalUIField.GetValue(ui); externalUIField.SetValue(ui, this); this.host = host; }
public void DetachFromHost() { if (host == null) { return; } const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic; object uiRef = host.GetType().GetField("internalUIRef", flags).GetValue(host); object ui = uiRef.GetType().GetProperty("Value", flags).GetValue(uiRef, null); FieldInfo externalUIField = ui.GetType().GetField("externalUI", flags); if (externalUIField.GetValue(ui) == this) { externalUIField.SetValue(ui, externalUI); } externalUI = null; host = null; }