返回列表 发帖

[原创代码] 分享一个PowerShell随机出列的类

class RandomListClass{
    static AddListMember([ref]$InputObject){
        if($InputObject.Value -is [System.Collections.iDictionary] -or $InputObject.Value -is [Hashtable]){
            $InputObject.Value|Add-Member -MemberType ScriptProperty -Name RandomKeys -Force -Value {
                $this.Keys|Get-Random -Count $this.Count
            }
            $InputObject.Value|Add-Member -MemberType ScriptProperty -Name RandomValues -Force -Value {
                $this.Values|Get-Random -Count $this.Count
            }
            $InputObject.Value|Add-Member -MemberType ScriptProperty -Name Dequeue -Force -Value {
                if($this.Count){
                    $Keys=$this.Keys|Get-Random
                    $this.Remove($Keys)|Out-Null
                    $Keys
                }
            }
        }
    }
}
$RandomList=[System.Collections.Generic.Dictionary[string,string]]@{}
[RandomListClass]::AddListMember($RandomList)
1..10|%{
    $RandomList.Add($_,$_)
}
$RandomList.DequeueCOPY
不过不知为什么换List添加不了成员对象,重载不了此类,但是字典和哈希却可以?

返回列表