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

若如初见. 提交于 2019-12-01 19:49:24

问题


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

I want to set option attributes in a foreach loop using the $attribute variable.

Is there a way to do this?


回答1:


Just use double quotes after the dot:

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


来源:https://stackoverflow.com/questions/39667296/how-to-access-a-property-from-an-object-using-a-variable-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!