6

我正在使用字符串设置 XML 属性,PowerShell 告诉我“只有字符串可以用作设置 XmlNode 属性的值”。这是一个简单的例子。首先,我运行这个:

$xmlDoc = [xml]@"
<root>
  <ComponentRef Id="a" />
</root>
"@

$newId = "b"

$length = $newId.Length

Write-Host ("`n`$newId is a string, see: `$newId.GetType().FullName = " + $newId.GetType().FullName + "`n")
Write-Host ("Running `"`$xmlDoc.root.ComponentRef.Id = `$newId`"...`n")
$xmlDoc.root.ComponentRef.Id = $newId
Write-Host ("ComponentRef.Id is now: " + $xmlDoc.root.ComponentRef.Id)

对我来说,输出是:

$newId is a string, see: $newId.GetType().FullName = System.String

Running "$xmlDoc.root.ComponentRef.Id = $newId"...

Cannot set "Id" because only strings can be used as values to set XmlNode properties.
At D:\Build\Tools\mass processing\Untitled4.ps1:14 char:27
+ $xmlDoc.root.ComponentRef. <<<< Id = $newId
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

ComponentRef.Id is now: a

该错误消息一定是错误的。等号右侧的值一个字符串,如上面的输出所示。但它出错了,所以 XML 属性仍然读取“a”。现在它变得更奇怪了。让我们注释掉调用 $newId.length 的行,并观察它是否正常工作。

像这样注释掉:#$length = $newId.Length。现在的输出是:

$newId is a string, see: $newId.GetType().FullName = System.String

Running "$xmlDoc.root.ComponentRef.Id = $newId"...

ComponentRef.Id is now: b

我不是要求修复,因为我知道如何通过转换为最后一个赋值运算符右侧的 [string] 来解决此问题。我想知道的是:

谁能解释为什么调用 $newId.Length (一个吸气剂!)会导致 PowerShell 认为 $newId 不再是一个字符串?

谢谢!

4

1 回答 1

6

这是 V2 扩展类型系统中的一个不幸的错误,其中psobject可以围绕基本 .NET 类型创建包装器。当您向对象添加成员或访问不存在的属性时,可能会发生这种情况。当您访问psobject对象 IIRC 例如的属性时,也会发生这种情况$newId.psobject。当您留在 PowerShell 中时,这通常不会导致任何问题。

更新:调用 .NET 不是问题。一些快速的 .NET 测试代码表明它为属性设置器获取了一个未包装的对象。在查看它之后,Trace-Command它似乎是 PowerShell 中的一个错误XmlNodeAdapater

DEBUG: ETS Information: 0 :  Method      Enter PSObject..ctor():object = System.Management.Automation.RuntimeException:
 Cannot set "Id" because only strings can be used as values to set XmlNode properties. --->
System.Management.Automation.SetValueException: Cannot set "Id" because only strings can be used as values to set
XmlNode properties.
   at System.Management.Automation.XmlNodeAdapter.PropertySet(PSProperty property, Object setValue, Boolean
convertIfPossible)
   at System.Management.Automation.Adapter.BasePropertySet(PSProperty property, Object setValue, Boolean convert)
   at System.Management.Automation.PSProperty.SetAdaptedValue(Object setValue, Boolean shouldConvert)
   at System.Management.Automation.PSProperty.set_Value(Object value)
   at System.Management.Automation.PropertyReferenceNode.SetValue(PSObject obj, Object property, Object value,
ExecutionContext context)
   --- End of inner exception stack trace ---
   at System.Management.Automation.PropertyReferenceNode.SetValue(PSObject obj, Object property, Object value,
ExecutionContext context)
   at System.Management.Automation.AssignablePropertyReference.SetValue(Object value, ExecutionContext context)
   at System.Management.Automation.AssignmentStatementNode.Execute(Array input, Pipe outputPipe, ExecutionContext
context)
   at System.Management.Automation.StatementListNode.ExecuteStatement(ParseTreeNode statement, Array input, Pipe
outputPipe, ArrayList& resultList, ExecutionContext context)

确保您始终获得base.NET 对象的一种方法是:

$xmlDoc.root.ComponentRef.Id = $newId.psobject.baseobject

好消息是这个问题在 V3 中已修复,例如:

PS> $xmlDoc = [xml]@"
<root>
  <ComponentRef Id="a" />
</root>
"@

PS> $newId = "b"

PS> $newId.Length
1

PS> $newId.psobject.typenames
System.String
System.Object

PS> $xmlDoc.root.ComponentRef.Id = $newId

PS> $xmlDoc | format-xml # From PowerShell Community Extensions
<root>
  <ComponentRef Id="b" />
</root>
于 2012-04-27T23:19:18.917 回答