VB.Net Enum ToString 返回未知数字

发布于 2024-07-27 15:03:43 字数 731 浏览 4 评论 0原文

我构建了一个简单的 vb.net winforms 项目,用于 ping IP 地址并记录结果。 它在我尝试过的大多数机器上运行良好。 我使用 IPStatus.tostring 方法记录 ping (System.Net.NetworkInformation.IPStatus) 的状态结果。

通常这会返回一个文本结果,例如“Success”或“TimedOut”

昨天,在一台机器上它返回“65”......这不是枚举值之一。 我有一种感觉,这可能是价值观的结合。 我运行了一些测试代码:

Dim status As System.Net.NetworkInformation.IPStatus
status = Net.NetworkInformation.IPStatus.Success
MsgBox(status.ToString)

哪个返回“成功”

而这个:

status = Net.NetworkInformation.IPStatus.BadDestination Or Net.NetworkInformation.IPStatus.BadHeader
MsgBox(status.ToString)

哪个返回“11050”

我怀疑我看到的“65”是某些枚举值组合的结果。 有什么方法可以更改第二个示例中的代码以显示两个值的文本名称? 也就是说......我可以通过什么方式看到这个变量中的所有值?

I built a simple vb.net winforms project that pings IP addresses and logs the results. It works fine on most machines I've tried it on. I log the status result of the ping (System.Net.NetworkInformation.IPStatus) by using the IPStatus.tostring method.

Normally this returns a text result such as "Success" or "TimedOut"

Yesterday, on one machine it returned "65" ...which is not one of the enum values. I have a feeling it might be a combination of values. I ran some test code:

Dim status As System.Net.NetworkInformation.IPStatus
status = Net.NetworkInformation.IPStatus.Success
MsgBox(status.ToString)

Which returns "Success"

And this:

status = Net.NetworkInformation.IPStatus.BadDestination Or Net.NetworkInformation.IPStatus.BadHeader
MsgBox(status.ToString)

Which returns "11050"

I suspect the "65" I saw was the result of some combination of enum values. Is there any way I can change the code in my second example to show the text names of both values? That is... any way I can see ALL values in this variable?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

陈独秀 2024-08-03 15:03:43

IPStatus 不是 Flags 枚举,因此不适合以这种方式组合其成员值。 这是它通过 Reflector 的定义:

Public Enum IPStatus
    ' Fields
    BadDestination = &H2B0A
    BadHeader = &H2B22
    BadOption = &H2AFF
    BadRoute = &H2B04
    DestinationHostUnreachable = &H2AFB
    DestinationNetworkUnreachable = &H2AFA
    DestinationPortUnreachable = &H2AFD
    DestinationProhibited = &H2AFC
    DestinationProtocolUnreachable = &H2AFC
    DestinationScopeMismatch = &H2B25
    DestinationUnreachable = &H2B20
    HardwareError = &H2B00
    IcmpError = &H2B24
    NoResources = &H2AFE
    PacketTooBig = &H2B01
    ParameterProblem = &H2B07
    SourceQuench = &H2B08
    Success = 0
    TimedOut = &H2B02
    TimeExceeded = &H2B21
    TtlExpired = &H2B05
    TtlReassemblyTimeExceeded = &H2B06
    Unknown = -1
    UnrecognizedNextHeader = &H2B23
End Enum

如何获得 IPStatus 值 65 - 现在这是真正的问题:)

IPStatus is NOT a Flags enum, therefore it is not appropriate to combine its member values in this way. This is its definition via Reflector:

Public Enum IPStatus
    ' Fields
    BadDestination = &H2B0A
    BadHeader = &H2B22
    BadOption = &H2AFF
    BadRoute = &H2B04
    DestinationHostUnreachable = &H2AFB
    DestinationNetworkUnreachable = &H2AFA
    DestinationPortUnreachable = &H2AFD
    DestinationProhibited = &H2AFC
    DestinationProtocolUnreachable = &H2AFC
    DestinationScopeMismatch = &H2B25
    DestinationUnreachable = &H2B20
    HardwareError = &H2B00
    IcmpError = &H2B24
    NoResources = &H2AFE
    PacketTooBig = &H2B01
    ParameterProblem = &H2B07
    SourceQuench = &H2B08
    Success = 0
    TimedOut = &H2B02
    TimeExceeded = &H2B21
    TtlExpired = &H2B05
    TtlReassemblyTimeExceeded = &H2B06
    Unknown = -1
    UnrecognizedNextHeader = &H2B23
End Enum

How you are getting an IPStatus value of 65 - now that's the real question :)

北方的韩爷 2024-08-03 15:03:43

尝试使用 System.Enum 来获取值的名称。

在您的示例中,使用:
MsgBox(System.Enum.GetName(GetType(Net.NetworkInformation.IPStatus), 状态))

Try using System.Enum to get the name of the value.

In your example, use:
MsgBox(System.Enum.GetName(GetType(Net.NetworkInformation.IPStatus), status))

请爱~陌生人 2024-08-03 15:03:43

看起来除了 Success (0) 和 Unknown (-1) 之外,定义的枚举值范围是从 11002 到 11045,因此 65 不是任何定义的枚举值的组合。

如果返回 65,您将无法将其解析为字符串。

It looks like apart from Success (0) and Unknown (-1), the defined enum values range from 11002 to 11045, so 65 is not a combination of any of the defined enum values.

If you are getting 65 back, you will not be able to resolve this to a string.

极致的悲 2024-08-03 15:03:43

该枚举未用 FlagsAttribute 标记,因此不应进行或运算,因为结果可能会重叠。 您最好创建自己的枚举来包含您正在寻找的值。

That enum is not marked with the FlagsAttribute and therefore should not be or'd together because the result could overlap. You are better off creating your own enum to contain the values you are looking for.

与君绝 2024-08-03 15:03:43

您应该能够使用 [1] 之类的方法循环遍历枚举的范围,测试当前枚举位是否在值中表示并将其添加到字符串生成器中。

我发现很难在这个小文本框中用 VB.NET 编写工作示例,但我确信其他人会帮忙。

[1] http ://damieng.com/blog/2008/04/10/using-linq-to-foreach-over-an-enum-in-c

You should be able to loop over the enum's range using something like [1], test whether the current enum bit is represented in the value and add it to a string builder.

I find it hard to write up a working sample in VB.NET in this little text box, but I'm sure someone else will oblige.

[1] http://damieng.com/blog/2008/04/10/using-linq-to-foreach-over-an-enum-in-c

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文