.net - 将 Nullable Enum 设置为 $null - 这真的会导致 PSInvalidCastException 吗?

标签 .net powershell enums powershell-3.0 nullable

在 PowerShell 中将可空枚举设置为 $null 会导致 System.Management.Automation.PSInvalidCastException异常(exception)。这是出乎意料的(至少对我来说)。对此有合理的解释吗?下面是一个示例,说明如何设置 Nullable Int32 是成功的,但设置 Nullable Enum 会导致异常:

Add-Type @"
public enum ColorEnum
{
    Red = 1,
    Blue = 2,
    Green = 3,
}

public class Thing
{
    public ColorEnum? NullableColor = ColorEnum.Blue;
    public System.Int32? NullableInt = 123;
}
"@

$test = New-Object Thing

# Setting the Nullable Int32 to $null works, as expected.
$test.NullableInt = $null

# Setting the Nullable Enum to $null causes exception.
$test.NullableColor = $null

异常消息如下:

Exception setting "NullableColor": "Cannot convert null to type "ColorEnum" due to enumeration values that are not valid. Specify one of the following enumeration values and try again. The possible enumeration values are "Red, Blue, Green"."



我希望能够使用 Nullable Enum 而不是默认值为 0 的 Enum 的原因是因为我要使用的 Enum 表示一个可为 null 的数据库列,当没有设置有效值时该列应为 null .我无法更改数据库模型,所以不幸的是,感觉解决方案可能是使用 Int32 而不是 Enum。

有没有其他人经历过这个?也许是一个错误?

$PsVersion 表:
Name                           Value                                                                                                                                                                                            
----                           -----                                                                                                                                                                                            
PSVersion                      3.0                                                                                                                                                                                              
WSManStackVersion              3.0                                                                                                                                                                                              
SerializationVersion           1.1.0.1                                                                                                                                                                                          
CLRVersion                     4.0.30319.18444                                                                                                                                                                                  
BuildVersion                   6.2.9200.16481                                                                                                                                                                                   
PSCompatibleVersions           {1.0, 2.0, 3.0}                                                                                                                                                                                  
PSRemotingProtocolVersion      2.2     

最佳答案

这是 PowerShell 4 中的一个错误(可能是 3,但我还没有尝试过)。

它已在 PowerShell V5 中修复(针对最近的内部版本进行了验证),我相信它应该在公共(public)版本中修复,例如 9 月的 WMF5 预览版或 Windows 10 预览版。

关于.net - 将 Nullable Enum 设置为 $null - 这真的会导致 PSInvalidCastException 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26763955/

相关文章:

java - 枚举类型的Android单元测试

c# - 通用约束和类型参数困惑

c# - 窗体上的 Mouseenter 和 Mouseleave

powershell - 从 PSCustomObject 开始删除额外的值

javascript - 枚举成员如何在 TypeScript 中使用别名?

ios - 检查不带参数的枚举大小写

c# - 如何在 Environment.Exit() 之前调用事件?

.net - 我可以使用 string.format() 或 c# 中的任何其他函数来旋转字符串吗?

powershell - 如何确定文件在 PowerShell 中是否以制表符分隔?

powershell - 在 IIS 8 应用程序池中为多台服务器启用自动启动属性