如何确定给定的整数是否在特定的枚举中? [英] How to determine if a given Integer is in a particular Enum?

查看:143
本文介绍了如何确定给定的整数是否在特定的枚举中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VB.NET中,即使使用 Option Strict ,也可以将枚举作为整数传递。

In VB.NET, even with Option Strict on, it's possible to pass an Enum around as an Integer.

在我特殊的情况下,有人使用类似这样的枚举:

In my particular situation, someone's using an enum similar to this:

Public Enum Animals
    Monkey = 1
    Giraffe = 2
    Leopard = 3
    Elephant = 4
End Enum

但是它们作为整数传递,所以它们可以将-1的值设置为无动物(而不必在枚举本身中包含无动物),即:

But they are passing it around as an Integer so they can set a value of -1 to be "No animal" (without having to include "No animal" in the Enum itself), i.e.:

Public Sub MakeAnimalJump(animalType As Int32)
    If animalType < 1 Then
        ' Clearly not an animal...
    Else
        ' Make that animal jump...
    End If
End Sub

然而,稍后,他们要求它再次成为动物类型。我的问题是,除了a)将枚举更改为包含无值,或b)使用 [Enum] .GetValues(...)是否有一个简单的方法来计算一个给定的整数是否映射到枚举中的值?

However, later on, they're asking for it to be an Animals type again. My question is, aside from a) changing the Enum to include a "None" value or b) cycling through each value in the Enum using [Enum].GetValues(...), is there an easy way to work out whether a given Integer maps to a value in the enum or not?

我希望可能有一个[枚举] .TryParse或某些东西,但它看起来不像。

I was hoping there might be an [Enum].TryParse or something, but it doesn't look like there is.

编辑:正如您所建议的那样,.NET 4中有一个Enum.TryParse。不幸的是,有问题的项目还是要支持Windows Server 2000,所以我们不能使用最新的.NET Framework(希望我们能够尽快删除对Windows Server 2000的支持!)。

As some of you have suggested, there is an Enum.TryParse in .NET 4. Unfortunately, the project in question must still support Windows Server 2000, so we can't use the latest .NET Framework (hopefully we'll be able to drop support for Windows Server 2000 soon..!).

推荐答案

尽管.NET 4.0引入了 Enum.TryParse 方法,但您不应该将其用于此特定场景。在.NET中,枚举有一个底层类型,可以是以下任何一种( byte sbyte short ushort int uint long ulong )。默认情况下是 int ,所以任何有效的 int 的值也是有效的枚举值。

Although .NET 4.0 introduced the Enum.TryParse method you should not use it for this specific scenario. In .NET an enumeration has an underlying type which can be any of the following (byte, sbyte, short, ushort, int, uint, long, or ulong). By default is int, so any value that is a valid int is also a valid enumeration value.

这意味着 Enum.TryParse< Animal>( - 1,输出结果)报告成功,即使 -1 不与任何指定的枚举值相关联。

This means that Enum.TryParse<Animal>("-1", out result) reports success even though -1 is not associated to any specified enumeration value.

正如其他人所指出的,对于这种情况,您必须使用 Enum.IsDefined 方法。

As other have noted, for this scenarios, you must use Enum.IsDefined method.

示例代码(C#):

enum Test { Zero, One, Two }

static void Main(string[] args)
{
    Test value;
    bool tryParseResult = Enum.TryParse<Test>("-1", out value);
    bool isDefinedResult = Enum.IsDefined(typeof(Test), -1);

    Console.WriteLine("TryParse: {0}", tryParseResult); // True
    Console.WriteLine("IsDefined: {0}", isDefinedResult); // False
}

这篇关于如何确定给定的整数是否在特定的枚举中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆