在 Powershell 5.0 类中使用自定义类型

Jan*_*lka 1 powershell powershell-5.0

这是目前让我头疼的示例代码。

if (("Win32.NativeMethods" -as [type]) -eq $null){
    Add-Type -MemberDefinition '[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
        ' -name NativeMethods -namespace Win32
}

class AppInstance 
{    
    [string]$App = 'Notepad.exe'
    [IntPtr]$hWnd = 0
    [System.Object]$process

    AppInstance () {
        Start-Process $this.App
        $this.process = get-process ($this.App.split('.'))[0]
        start-sleep -Milliseconds 100
        $this.hWnd = $this.process.MainWindowHandle    
    }

    [void] Show () {
        [Win32.NativeMethods]::ShowWindowAsync($this.hWnd, 3)
    }

    [void] Hide () {
        [Win32.NativeMethods]::ShowWindowAsync($this.hWnd, 2)
    }
}
Run Code Online (Sandbox Code Playgroud)

这个类可以像这样使用

$notepad = [AppInstance]::new()
$notepad.Hide()
$notepad.Show()
Run Code Online (Sandbox Code Playgroud)

基本上,我想要做的是从 user32.dllas 类型[Win32.NativeMethods],然后在class.

如果我执行 Add-TypePowershell_ISE创建的类型中单独语句,然后脚本就可以正常工作。

但是,当我尝试在手动创建类型之前执行整个脚本时,我收到以下 Powershell 解析器错误

At C:\class.ps1:26 char:10
+         [Win32.NativeMethods]::ShowWindowAsync($this.hWnd, 3)
+          ~~~~~~~~~~~~~~~~~~~
Unable to find type [Win32.NativeMethods].
At C:\Uclass.ps1:31 char:10
+         [Win32.NativeMethods]::ShowWindowAsync($this.hWnd, 2)
+          ~~~~~~~~~~~~~~~~~~~
Unable to find type [Win32.NativeMethods].
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : TypeNotFound
Run Code Online (Sandbox Code Playgroud)

看起来解析器忽略了 Add-Type语句并在执行前退出。

有没有办法克服这个问题?也许与using声明?或者,有没有办法告诉解析器该类型是动态创建的?

编辑 1:

我已经阅读了答案 在 Powershell (V5) 类中使用 .Net 对象接受的答案不是我的问题的答案。将一个简单的脚本拆分为多个文件并不是真正的答案。

我要问的是天气有一种方法可以告诉解析器该类型是动态创建的。

编辑2:

为了进一步澄清这一点,这里的代码与上面的代码等效,但使用functions代替classes.

At C:\class.ps1:26 char:10
+         [Win32.NativeMethods]::ShowWindowAsync($this.hWnd, 3)
+          ~~~~~~~~~~~~~~~~~~~
Unable to find type [Win32.NativeMethods].
At C:\Uclass.ps1:31 char:10
+         [Win32.NativeMethods]::ShowWindowAsync($this.hWnd, 2)
+          ~~~~~~~~~~~~~~~~~~~
Unable to find type [Win32.NativeMethods].
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : TypeNotFound
Run Code Online (Sandbox Code Playgroud)

此代码将解析并执行得非常好。classes解析器是否以与脚本其余部分不同的方式处理?

Mat*_*sen 6

正如您自己发现的那样,在定义函数时,解析器几乎不像在定义类时那么严格——原因很简单,函数定义不需要编译,因此解析器只检查语法而不是类型解析。

您可以使用此观察来解决您的问题 - 只需在 Class 定义之外定义一个函数,该函数将调用包装起来[Win32.NativeMethods]::ShowWindowAsync(),然后从您的类方法内部调用该函数:

function __ShowWindowAsync
{
    param([IntPtr]$WindowHandle,[int]$ShowState)

    if (("Win32.NativeMethods" -as [type]) -eq $null){
        Add-Type -MemberDefinition '[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);' -Name NativeMethods -namespace Win32
    }

    [Win32.NativeMethods]::ShowWindowAsync($this.hWnd, $ShowState)
}

class AppInstance 
{    
    [string]$App = 'Notepad.exe'
    [IntPtr]$hWnd = 0
    [System.Object]$process

    AppInstance () {
        # this is way more reliable than running Get-Process subsequently
        $this.process = Start-Process $this.App -PassThru
        start-sleep -Milliseconds 100
        $this.hWnd = $this.process.MainWindowHandle
    }

    [void] Show () {
        $Maximized = 3
        __ShowWindowAsync -WindowHandle $this.hWnd -ShowState $Maximized
    }

    [void] Hide () {
        $Minimized = 2
        __ShowWindowAsync -WindowHandle $this.hWnd -ShowState $Minimized
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢。这正是我一直在寻找的。仍然不明白为什么这里的人们这么快就将问题称为重复问题。您的回答提供了更多信息和非常好的解决方案。 (2认同)