如何使用变量名从对象访问 属性?

How to access a property from an object using a variable name?

这个有效:

$psISE.Options.DebugBackgroundColor = '#FFC86400'

这不是:

$attribute = 'DebugBackgroundColor' 
($psISE.Options)[$attribute] = '#FFC86400'

ERROR: Unable to index into an object of type Microsoft.PowerShell.Host.ISE.ISEOptions

我想使用 $attribute 变量在 foreach 循环中设置选项属性。

有办法吗?

只需在点后使用双引号:

$attribute = 'DebugBackgroundColor'
$psISE.Options."$attribute"