无法获取 ironpython 中元素的值使用 powershell

Unable to get the value of an element in ironpython use powershell

以下代码需要获取元素的值,但是输出为空

[reflection.assembly]::LoadFrom("C:\Program Files\IronPython 2.7\IronPython.dll")
$py = [ironpython.hosting.python]::CreateEngine()
$pyv = $py.CreateScope()
$pyc = $py.CreateScriptSourceFromString("d = {'one':1,'two':2}")
$pyc.Execute($pyv)
$d = $pyv.GetVariable("d")
$d.one
$d.two

您可以使用 get("key") 函数或 $variable["key"] 获取字典值:

$engine = [ironpython.hosting.python]::CreateEngine()
$pyv = $engine.CreateScope()
$pyc = $engine.CreateScriptSourceFromString("d = {'one':1,'two':2}")
$pyc.Execute($pyv)
$d = $pyv.GetVariable("d")
$d.get("one")
$d["two"]

这是 python 字典 的预期行为,如果您在对象上尝试 get-member 那么您可以看到属性一和二是不存在,但即使您尝试访问不存在的属性,powershell 也不会抛出错误