简体   繁体   English

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

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

This works: 这有效:

$psISE.Options.DebugBackgroundColor = '#FFC86400'

This doesn't: 这不是:

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

ERROR: Unable to index into an object of type Microsoft.PowerShell.Host.ISE.ISEOptions 错误:无法索引到Microsoft.PowerShell.Host.ISE.ISEOptions类型的对象

I want to set option attributes in a foreach loop using the $attribute variable. 我想使用$attribute变量在foreach循环中设置选项属性。

Is there a way to do this? 有没有办法做到这一点?

Just use double quotes after the dot: 只需在点后使用双引号即可:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM