请查看以下示例
$URI = "http://localhost:8080/service.wsdl"
$proxy = New-WebServiceProxy -uri $URI
$type = $proxy.GetType().Namespace
$request = New-Object($type+".OrderRequest");
$request.amount = $null
$request.amount -eq $null # return false
amount的类型为System.Decimal,我可以将其他属性归零,但我无法将此特定属性置空,我真的感到困惑。
以下是根据wsdl
的请求对象的定义<xs:element xmlns:xs="http://www.w3.org/2001/XMLSchema" name="OrderReuqest">
<xs:complexType xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:all xmlns:xs="http://www.w3.org/2001/XMLSchema">
...
<xs:element xmlns:xs="http://www.w3.org/2001/XMLSchema" name="amount" type="Amount_SimpleType"/>
...
</xs:all>
</xs:complexType>
</xs:element>
<simpleType xmlns="http://www.w3.org/2001/XMLSchema" name="Amount_SimpleType">
<restriction xmlns="http://www.w3.org/2001/XMLSchema" base="decimal">
<fractionDigits xmlns="http://www.w3.org/2001/XMLSchema" value="6"/>
<totalDigits xmlns="http://www.w3.org/2001/XMLSchema" value="18"/>
</restriction>
</simpleType>
这是由命令行开关从WSDL生成的请求对象
PS T:\> New-Object("WebServiceProxy.OrderRequest") | Get-Member | Where- Object {$_.Name -eq "amount"}
TypeName: WebServiceProxy.OrderRequest
Name MemberType Definition
---- ---------- ----------
amount Property System.Decimal amount {get;set;}
因此,根据WSDL,amount可以为空,但不是根据生成的对象,可以以某种方式更改吗?
答案 0 :(得分:0)
大多数数字数据类型在.Net中不可为空。当为它们分配空值时,它们将具有默认值。例如:
PS C:\> [decimal]$null
0
PS C:\> [decimal]$null -eq $null
False
如果您需要使类型可以为空,则需要使用Powershell中指定的special nullable datatypes [System.Nullable[<datatype>]]
类型。
PS U:\> [System.Nullable[decimal]]$null
PS U:\> [System.Nullable[decimal]]$null -eq $null
True
可以通过这种方式声明许多基本数据类型,包括所有数字类型以及其他一些数据类型,例如[datetime]
或[timespan]
,但值得注意的是没有[System.Nullable[string]]
。
但是,在这里,您要处理由任意方法返回的对象的属性。除非该属性是对象类定义中的可空数据类型,否则当您将$null
分配给该值时,它将获得值0
。我没有看到一个很好的方法让你在不修改对象定义的情况下解决这个问题,这可能是不可能的。