gpt4 book ai didi

powershell - 动态变量属性

转载 作者:行者123 更新时间:2023-12-03 00:35:16 25 4
gpt4 key购买 nike

我正在foreach循环中生成一个名称是动态的变量,我需要设置属性

$variableName = "res" + $i
New-Variable -Name $variableName -Value New-Object System.Windows.Forms.Label

然后我需要添加属性,但只需使用
$variableName.text = "ipsum"  

将设置$ variableName的属性,而不是设置的变量。

最佳答案

您应该能够使用Get-Variable cmdlet通过-ValueOnly参数执行此操作,以便获得返回的变量,而不是包含有关该变量的属性的对象:

(Get-Variable $variableName -ValueOnly).text = "ipsum"

请注意,您的代码中还有一个错误,即:
New-Variable -Name $variableName -Value New-Object System.Windows.Forms.Label

需要是:
New-Variable -Name $variableName -Value (New-Object System.Windows.Forms.Label)

屏幕截图:

Accessing a dynamically named variable with PowerShell

关于powershell - 动态变量属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43096836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com