如何在PowerShell中定义类似文件属性的位组合自定义类型?
PowerShell 自定义位组合类型实现方案
直接用 PowerShell 类就能实现你要的功能,核心是通过位掩码封装位运算,对外暴露直观的布尔属性,无需手动操作位运算。以下是适配6个比特位的完整实现:
class MyAttribute { # 定义每个属性对应的位掩码(6个比特位,对应2^0到2^5) hidden static [int]$ReadMask = 1 # 0b000001 hidden static [int]$WriteMask = 2 # 0b000010 hidden static [int]$ExecuteMask = 4 # 0b000100 hidden static [int]$DeleteMask = 8 # 0b001000 hidden static [int]$AppendMask = 16 # 0b010000 hidden static [int]$ArchiveMask = 32 # 0b100000 # 私有字段存储最终的位组合数值 hidden [int]$_value = 0 # 对外暴露的布尔属性,自动处理位运算 [bool] get_Read() { return ($this._value -band [MyAttribute]$ReadMask) -ne 0 } void set_Read([bool]$value) { if ($value) { $this._value = $this._value -bor [MyAttribute]$ReadMask } else { $this._value = $this._value -band (-bnot [MyAttribute]$ReadMask) } } [bool] get_Write() { return ($this._value -band [MyAttribute]$WriteMask) -ne 0 } void set_Write([bool]$value) { if ($value) { $this._value = $this._value -bor [MyAttribute]$WriteMask } else { $this._value = $this._value -band (-bnot [MyAttribute]$WriteMask) } } [bool] get_Execute() { return ($this._value -band [MyAttribute]$ExecuteMask) -ne 0 } void set_Execute([bool]$value) { if ($value) { $this._value = $this._value -bor [MyAttribute]$ExecuteMask } else { $this._value = $this._value -band (-bnot [MyAttribute]$ExecuteMask) } } [bool] get_Delete() { return ($this._value -band [MyAttribute]$DeleteMask) -ne 0 } void set_Delete([bool]$value) { if ($value) { $this._value = $this._value -bor [MyAttribute]$DeleteMask } else { $this._value = $this._value -band (-bnot [MyAttribute]$DeleteMask) } } [bool] get_Append() { return ($this._value -band [MyAttribute]$AppendMask) -ne 0 } void set_Append([bool]$value) { if ($value) { $this._value = $this._value -bor [MyAttribute]$AppendMask } else { $this._value = $this._value -band (-bnot [MyAttribute]$AppendMask) } } [bool] get_Archive() { return ($this._value -band [MyAttribute]$ArchiveMask) -ne 0 } void set_Archive([bool]$value) { if ($value) { $this._value = $this._value -bor [MyAttribute]$ArchiveMask } else { $this._value = $this._value -band (-bnot [MyAttribute]$ArchiveMask) } } # 对应需求中的 value__ 属性,返回当前位组合的数值 [int] get_value__() { return $this._value } # 重写 ToString 方法,输出激活的属性名称 [string] ToString() { $activeAttributes = @() if ($this.Read) { $activeAttributes += 'read' } if ($this.Write) { $activeAttributes += 'write' } if ($this.Execute) { $activeAttributes += 'execute' } if ($this.Delete) { $activeAttributes += 'delete' } if ($this.Append) { $activeAttributes += 'append' } if ($this.Archive) { $activeAttributes += 'archive' } return $activeAttributes -join ',' } }
使用示例
# 创建实例 $attr = [MyAttribute]::new() # 设置属性 $attr.Read = $true $attr.Write = $true $attr.Execute = $true # 输出激活的属性列表 Write-Host $attr # 输出: read,write,execute # 输出位组合的数值 Write-Host $attr.value__ # 输出: 7
核心逻辑说明
- 位掩码常量:每个属性对应一个唯一的2的幂次数值,确保每个属性占用独立的比特位。
- 属性的 Get/Set 方法:自动处理位运算——
-band(按位与)检查比特位是否激活,-bor(按位或)设置比特位,-bnot(按位取反)配合-band清除比特位。 - ToString 重写:遍历所有属性,收集激活的属性名并格式化输出。
- value__ 属性:直接返回存储的位组合数值,无需额外计算。
内容的提问来源于stack exchange,提问作者Carsten
相关产品推荐
相关产品推荐

