如何将字符串转换为枚举? [英] How to convert a string to a enum?
本文介绍了如何将字符串转换为枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在PowerShell中将字符串转换为枚举值,但在任何地方都找不到...
I'm trying to convert a string to a enum value in PowerShell but couldn't find this anywhere...
我得到一个JSON结果,我只想使用定义为字符串的Healthstate.
I'm getting a JSON result, where I only want to consume the Healthstate which is defined as a string.
enum HealthState
{
Invalid = 0
Ok = 1
Warning = 2
Error = 3
Unknown = 65535
}
$jsonResult = "Ok"
$HealthStateResultEnum = [Enum]::ToObject([HealthState], $jsonResult)
谢谢.
推荐答案
您可以简单地将字符串结果转换为Enum类型:
You can simply cast the string result as the Enum type:
$HealthStateResultEnum = [HealthState]$jsonResult
无论 $ jsonResult
包含枚举类型的字符串还是值,这都将起作用.
This will work whether $jsonResult
contains a string or value from the enum type.
这篇关于如何将字符串转换为枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文