You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PowerShell中扩展字典类并实现Getter/Setter?

在PowerShell中实现带自定义Getter/Setter的字典类

你想要创建一个可跨PowerShell会话读取的自定义字典类,用于临时存储第三方应用配置,同时通过自定义Getter/Setter提升开发者友好性,替代常规的Set-MyConfigVariable/Get-MyConfigVariable cmdlet。你的原型代码因PowerShell不支持C#风格的$This索引器语法报错,以下是可行的实现方案:

一、用PowerShell类实现自定义索引器

PowerShell类不支持直接用$This定义索引器,但可以通过实现get_Item和set_Item方法来模拟,既可以封装内部字典,也可以继承原生Dictionary类保留全部原生功能:

方案1:继承原生Dictionary并重写读写逻辑

这种方式保留字典的所有原生方法,同时加入自定义读写逻辑,还能轻松实现跨会话存储:

class MyStore : System.Collections.Generic.Dictionary[string, string] {
    # 重写索引器的读取方法
    [string] get_Item([string]$Key) {
        Write-Host "触发读取:键 = '$Key'"
        # 可选:读取前从外部存储同步最新数据
        return ([System.Collections.Generic.Dictionary[string, string]]$this)[Key]
    }

    # 重写索引器的写入方法
    void set_Item([string]$Key, [string]$Value) {
        Write-Host "触发写入:键 = '$Key',值 = '$Value'"
        # 先执行原生写入逻辑
        ([System.Collections.Generic.Dictionary[string, string]]$this)[Key] = $Value
        # 写入临时文件实现跨会话共享
        $this | ConvertTo-Json | Out-File -Path "$env:TEMP\MyAppConfig.json" -Force
    }

    # 构造函数:初始化时读取之前保存的配置
    MyStore() {
        $configPath = "$env:TEMP\MyAppConfig.json"
        if (Test-Path $configPath) {
            $savedConfig = Get-Content $configPath | ConvertFrom-Json
            foreach ($key in $savedConfig.PSObject.Properties.Name) {
                $this[$key] = $savedConfig.$key
            }
        }
    }
}

# 测试代码
$store = [MyStore]::new()
$store['dbConnectionString'] = 'Server=localhost;DB=MyApp'
$store['dbConnectionString']

方案2:封装内部字典(不继承)

如果不需要原生字典的全部方法,可以自行封装内部存储,仅暴露需要的功能:

class MyStore {
    # 内部存储用的私有字典
    hidden [System.Collections.Generic.Dictionary[string, string]]$_store = [System.Collections.Generic.Dictionary[string, string]]::new()

    # 自定义索引器读取方法
    [string] get_Item([string]$Key) {
        Write-Host "读取键 '$Key'"
        return $this._store[$Key]
    }

    # 自定义索引器写入方法
    void set_Item([string]$Key, [string]$Value) {
        Write-Host "写入键 '$Key',值 '$Value'"
        $this._store[$Key] = $Value
        # 跨会话存储逻辑
        $this._store | ConvertTo-Json | Out-File "$env:TEMP\MyAppConfig.json" -Force
    }

    # 可选:实现常用字典方法
    [bool] ContainsKey([string]$Key) {
        return $this._store.ContainsKey($Key)
    }

    [void] Remove([string]$Key) {
        $this._store.Remove($Key)
    }
}

# 测试
$store = [MyStore]::new()
$store['apiKey'] = 'abc123'
$store['apiKey']

二、单个实例实现动态键的自定义读写

如果不需要创建类,仅针对单个实例实现自定义逻辑,可以通过Add-Member为字典实例添加脚本方法:

# 创建基础字典实例
$myStore = [System.Collections.Generic.Dictionary[string, string]]::new()

# 添加自定义读取逻辑
$myStore | Add-Member -MemberType ScriptMethod -Name get_Item -Value {
    param([string]$Key)
    Write-Host "读取键 '$Key'"
    return $this.BaseObject[$Key]
} -Force

# 添加自定义写入逻辑
$myStore | Add-Member -MemberType ScriptMethod -Name set_Item -Value {
    param([string]$Key, [string]$Value)
    Write-Host "写入键 '$Key',值 '$Value'"
    $this.BaseObject[$Key] = $Value
    # 跨会话存储
    $this.BaseObject | ConvertTo-Json | Out-File "$env:TEMP\SingleInstanceConfig.json" -Force
} -Force

# 测试
$myStore['theme'] = 'dark'
$myStore['theme']

关键说明

  • PowerShell通过get_Item和set_Item方法识别索引器操作,使用[]访问时会自动调用这两个方法。
  • 跨会话读取的核心是将数据持久化到外部存储(如临时文件、注册表),在实例初始化时加载。

内容的提问来源于stack exchange,提问作者iRon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 16:27:52