gpt4 book ai didi

.net - Enum.GetName 与 Enum.ToString

转载 作者:行者123 更新时间:2023-12-02 04:18:27 24 4
gpt4 key购买 nike

对于此枚举,

Enum MyEnum
Value
End Enum

有两种方法可以获取MyEnum.Value的名称表示Value:

[Enum].GetName(GetType(MyEnum), MyEnum.Value) ' aka Enum.GetName

Dim a As MyEnum = MyEnum.Value
a.ToString ' aka Enum.ToString

他们的优点和缺点是什么?到底哪个更好?

PS:one answer对于 java,但这是 .NET,它可能具有不同的功能。

最佳答案

以下是使用此枚举可以完成的操作的一些示例,请注意 flags 属性的用法。

    <Flags>
Public Enum bit As Short
none = 0
s0 = 1 << 0
s1 = 1 << 1
s2 = 1 << 2
s3 = 1 << 3
s4 = 1 << 4
s5 = 1 << 5
s6 = 1 << 6
s7 = 1 << 7
s8 = 1 << 8
s9 = 1 << 9
s10 = 1 << 10
s11 = 1 << 11
s12 = 1 << 12
s13 = 1 << 13
s14 = 1 << 14
all = -1
End Enum

和这段代码

    Dim s As String = bit.s13.ToString
s = (bit.s10 Or bit.s11).ToString

Dim foo() As String
foo = [Enum].GetNames(GetType(bit))

For Each s As String In foo
Debug.WriteLine(s)
Next

Dim test As bit = bit.s1 Or bit.s3 Or bit.s5
Debug.WriteLine(test.ToString)
Debug.WriteLine(CShort(test).ToString)

test = test Or CType([Enum].Parse(GetType(bit), "s14"), bit)
Debug.WriteLine(test.ToString)

关于.net - Enum.GetName 与 Enum.ToString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37895901/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com