小编Tod*_*odd的帖子

为什么这种转换不能像C#一样在Powershell中工作?

我正在尝试将计算值转换为uint16。为了这个例子,我已经进行了硬编码。在C#中有效。但是,我认为Powershell中的相同代码失败了。考虑以下:

在powershell代码示例中,它产生:

Cannot convert value "101398986" to type "System.UInt16". Error: "Value was either too large or too small for a UInt16."
At D:\OneDrive\Desktop\VaribleCasting2.ps1:2 char:1
+ [uint16]$v = [uint16]$g
+ ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastIConvertible
Run Code Online (Sandbox Code Playgroud)

我试过[convert] :: ToUInt16($ g)它产生:

Exception calling "ToUInt16" with "1" argument(s): "Value was either too large or too small for a UInt16."
At D:\OneDrive\Desktop\VaribleCasting2.ps1:2 char:1
+ [uint16]$v = [convert]::ToUInt16($g)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId …
Run Code Online (Sandbox Code Playgroud)

c# powershell types

1
推荐指数
1
解决办法
63
查看次数

标签 统计

c# ×1

powershell ×1

types ×1