如何在PowerShell中使用行为(方法)创建真实对象?

Jup*_*aol 10 c# powershell powershell-2.0 powershell-3.0

可能这个问题之前已经得到了回答......但我还没有找到满足我需求的具体答案.

顺便说一下,我正在使用PowerShell 3

好吧,我是PowerShell的新手,但我作为C#开发人员有很多经验,所以使用对象对我来说非常重要.

所以我想知道在PowerShell脚本中是否有一种干净的方式来应用OOP概念(不是所有这些概念都很棒),例如我想做的具体事情.

注意:我知道我可以在PowerShell中编写C#代码来创建DTO,并且我可以在C#中创建PowerShell二进制模块,我过去已经这样做了,但我现在正在寻找的是写入的能力我在PowerShell中的所有代码,但是以面向对象的方式.

我想做的事情:

  • 在PowerShell中创建一个对象,并公开一个用PowerShell编写的函数,如下所示:

    function New-Person
    (
        [Parameter()][string]$firstName
    )
    {
        function Walk()
        {
            Write-Host "Walking...";
        }
    
        $person = New-Object psobject;
    
        $person | Add-Member -MemberType NoteProperty -Name FirstName -Value $firstName;
    
        #This line does not work
        #$person | Add-Member -MemberType CodeMethod -Name Walk -Value Walk;
    
        return $person;
    }
    
    $MyPerson = New-Person -firstName "JP";
    
    $MyPerson;
    
    #This line does not work
    $MyPerson.Walk();
    
    Run Code Online (Sandbox Code Playgroud)
  • 封装行为,这意味着在我的对象中创建函数,然后将它们标记为私有

  • [很高兴有].创建基类,以便我可以继承和专门化我的行为重写方法

  • [很高兴有].创建接口,这样我就可以开始考虑单独测试我的PowerShell方法了(我知道像Pester这样的工具可以做到这一点我只关注OOP功能)

到目前为止我所做的是创建仅具有属性的对象(DTO),但我想向我的对象添加行为

如果你们指出我正确的方向,我会很感激

Bar*_*ekB 12

使用方法创建对象的两个选项:

  1. 加入会员
  2. 新模块-AsCustomObject

代码示例:

$person | Add-Member -MemberType ScriptMethod -Value {
    'I do stuff!'
}

$person = New-Module -AsCustomObject -ScriptBlock {
    $Property = 'value'
    [string]$Other = 'Can be strongly typed'

    function MyMethod {
        'I do stuff!'
    }

}
Run Code Online (Sandbox Code Playgroud)

编辑:谈到私人/公共......在后一个示例中,属性不会"默认"显示.您可以决定什么是公共使用Export-ModuleMember和指定-Variable(属性)和/或-Function(方法)将公开.如果没有显式Export-ModuleMember,它将与"普通"模块中的行为相同 - 仅导出函数(方法).

  • 在函数定义中添加一个常规的`param`块.例如`$ obj ="foo"| Add-Member -MemberType ScriptMethod -Value {param($ first,$ second)"first is $ first,second is $ second"} -Name DoStuff -PassThru; $ obj.DoStuff('abc','123')` (4认同)

Fox*_*loy 7

PowerShell v5 引入了完整的类支持,使您可以轻松地使用属性和实现方法构建自己的类。

在此处查看 Trevor 关于该主题的精彩博文。Trevor Sullivan,实现 .net 类

独立示例

这是一个名为 Fox 的组合类型的 PowerShell 类,它有一个.Deploy()方法,应该显示这是如何完成的

class Fox {
    # describes the sixe of the fox
    [String] $Size;
    # Property: the foxes color
    [String] $Color;

    # Constructor: Creates a new Fox object, with the specified
    #              size and name / owner.
    Fox([string] $NewSize, [String] $NewName) {
        # describes the sixe of the fox
        $this.Size = $NewSize;
        # Property: the foxes color
        $this.Color = $NewName;
    }

    # Method: Change the size of the fox     
    [void] Morph([UInt32] $Amount) {
        try {
            $this.Size = $this.Size - $Amount;
        }
        catch {
            Write-Warning -Message 'You tried to set an invalid size!';
        }
    }

    # Method: BreakGlass resets the beer size to 0.
    [void] Deploy() {
        Write-Warning -Message "The $($this.Color) fox, which is $($this.Size) in stature, goes off into the distance"        
    }
}
Run Code Online (Sandbox Code Playgroud)

在实践中: 在此处输入图片说明


RB.*_*RB. 5

如果你想要完整的 OOP(包括继承,虽然不是接口),那么PSClass是一个MS-PL许可的实现.