VB.Net Enum ToString 返回未知数字
我构建了一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
IPStatus
不是Flags
枚举,因此不适合以这种方式组合其成员值。 这是它通过 Reflector 的定义:如何获得
IPStatus
值 65 - 现在这是真正的问题:)IPStatus
is NOT aFlags
enum, therefore it is not appropriate to combine its member values in this way. This is its definition via Reflector:How you are getting an
IPStatus
value of 65 - now that's the real question :)尝试使用 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))
看起来除了 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.
该枚举未用 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.
您应该能够使用 [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