Enum.IsDefined(Type, Object) 方法

定义

返回一个布尔值,该值指示给定的整数值或其名称字符串是否存在于指定的枚举中。Returns a Boolean telling whether a given integral value, or its name as a string, exists in a specified enumeration.

public:
 static bool IsDefined(Type ^ enumType, System::Object ^ value);
[System.Runtime.InteropServices.ComVisible(true)]
public static bool IsDefined (Type enumType, object value);
static member IsDefined : Type * obj -> bool
Public Shared Function IsDefined (enumType As Type, value As Object) As Boolean

参数

enumType
Type

枚举类型。An enumeration type.

value
Object

enumType 中的常量的值或名称。The value or name of a constant in enumType.

返回

如果 enumType 中的某个常量的值等于 value,则为 true;否则为 falsetrue if a constant in enumType has a value equal to value; otherwise, false.

属性

异常

enumTypevaluenullenumType or value is null.

enumType 不是 EnumenumType is not an Enum.

-or- value 的类型是枚举,但它不是类型 enumType 的枚举。The type of value is an enumeration, but it is not an enumeration of type enumType.

-or- value 的类型不是 enumType 的基础类型。The type of value is not an underlying type of enumType.

value 的类型不为 SByteInt16Int32Int64ByteUInt16UInt32UInt64Stringvalue is not type SByte, Int16, Int32, Int64, Byte, UInt16, UInt32, or UInt64, or String.

示例

下面的示例定义了一个名为 PetType 的枚举,其中包含单个位域。The following example defines an enumeration named PetType that consists of individual bit fields. 然后,它会调用 IsDefined 方法,该方法具有可能的基础枚举值、字符串名称以及设置多个位域导致的复合值。It then calls the IsDefined method with possible underlying enumeration values, string names, and composite values that result from setting multiple bit fields.

using System;

[Flags] public enum PetType
{
   None = 0, Dog = 1, Cat = 2, Rodent = 4, Bird = 8, Reptile = 16, Other = 32
};

public class Example
{
   public static void Main()
   {
      object value; 
      
      // Call IsDefined with underlying integral value of member.
      value = 1;
      Console.WriteLine("{0}: {1}", value, Enum.IsDefined(typeof(PetType), value));
      // Call IsDefined with invalid underlying integral value.
      value = 64;
      Console.WriteLine("{0}: {1}", value, Enum.IsDefined(typeof(PetType), value));
      // Call IsDefined with string containing member name.
      value = "Rodent";
      Console.WriteLine("{0}: {1}", value, Enum.IsDefined(typeof(PetType), value));
      // Call IsDefined with a variable of type PetType.
      value = PetType.Dog;
      Console.WriteLine("{0}: {1}", value, Enum.IsDefined(typeof(PetType), value));
      value = PetType.Dog | PetType.Cat;
      Console.WriteLine("{0}: {1}", value, Enum.IsDefined(typeof(PetType), value));
      // Call IsDefined with uppercase member name.      
      value = "None";
      Console.WriteLine("{0}: {1}", value, Enum.IsDefined(typeof(PetType), value));
      value = "NONE";
      Console.WriteLine("{0}: {1}", value, Enum.IsDefined(typeof(PetType), value));
      // Call IsDefined with combined value
      value = PetType.Dog | PetType.Bird;
      Console.WriteLine("{0:D}: {1}", value, Enum.IsDefined(typeof(PetType), value));
      value = value.ToString();
      Console.WriteLine("{0:D}: {1}", value, Enum.IsDefined(typeof(PetType), value));
   }
}
// The example displays the following output:
//       1: True
//       64: False
//       Rodent: True
//       Dog: True
//       Dog, Cat: False
//       None: True
//       NONE: False
//       9: False
//       Dog, Bird: False
<Flags> Public Enum PetType As Integer
   None = 0
   Dog = 1
   Cat = 2
   Rodent = 4
   Bird = 8
   Reptile = 16
   Other = 32
End Enum

Module Example
   Public Sub Main()
      Dim value As Object
      
      ' Call IsDefined with underlying integral value of member.
      value = 1
      Console.WriteLine("{0}: {1}", value, [Enum].IsDefined(GetType(PetType), value))
      ' Call IsDefined with invalid underlying integral value.
      value = 64
      Console.WriteLine("{0}: {1}", value, [Enum].IsDefined(GetType(PetType), value))
      ' Call IsDefined with string containing member name.
      value = "Rodent"
      Console.WriteLine("{0}: {1}", value, [Enum].IsDefined(GetType(PetType), value))
      ' Call IsDefined with a variable of type PetType.
      value = PetType.Dog
      Console.WriteLine("{0}: {1}", value, [Enum].IsDefined(GetType(PetType), value))
      value = PetType.Dog Or PetType.Cat
      Console.WriteLine("{0}: {1}", value, [Enum].IsDefined(GetType(PetType), value))
      ' Call IsDefined with uppercase member name.      
      value = "None"
      Console.WriteLine("{0}: {1}", value, [Enum].IsDefined(GetType(PetType), value))
      value = "NONE"
      Console.WriteLine("{0}: {1}", value, [Enum].IsDefined(GetType(PetType), value))
      ' Call IsDefined with combined value
      value = PetType.Dog Or PetType.Bird
      Console.WriteLine("{0:D}: {1}", value, [Enum].IsDefined(GetType(PetType), value))
      value = value.ToString()
      Console.WriteLine("{0:D}: {1}", value, [Enum].IsDefined(GetType(PetType), value))
   End Sub
End Module
' The example displays the following output:
'       1: True
'       64: False 
'       Rodent: True
'       Dog: True
'       Dog, Cat: False
'       None: True
'       NONE: False
'       9: False
'       Dog, Bird: False

注解

value 参数可以是以下任一项:The value parameter can be any of the following:

  • enumType类型的任何成员。Any member of type enumType.

  • 一个变量,其值为 enumType类型的枚举成员。A variable whose value is an enumeration member of type enumType.

  • 枚举成员的名称的字符串表示形式。The string representation of the name of an enumeration member. 字符串中的字符必须与枚举成员名称的大小写相同。The characters in the string must have the same case as the enumeration member name.

  • enumType的基础类型的值。A value of the underlying type of enumType.

如果 enumType 中的常量定义一组位域并且 value 包含多个位域的值、名称或基础值,则 IsDefined 方法返回 falseIf the constants in enumType define a set of bit fields and value contains the values, names, or underlying values of multiple bit fields, the IsDefined method returns false. 换言之,对于定义一组位域的枚举,该方法仅确定单个位字段是否属于枚举。In other words, for enumerations that define a set of bit fields, the method determines only whether a single bit field belongs to the enumeration. 若要确定是否在用 FlagsAttribute 特性标记的枚举类型中设置了多个位域,可以调用 HasFlag 方法。To determine whether multiple bit fields are set in an enumeration type that is tagged with the FlagsAttribute attribute, you can call the HasFlag method.

调用方说明

如果 enumType 是使用 FlagsAttribute 特性定义的枚举,则方法将返回 false 如果设置了 value 中的多个位域,但 value 与复合枚举值不对应,或者 value 是的字符串串联多个位标志的名称。If enumType is an enumeration that is defined by using the FlagsAttribute attribute, the method returns false if multiple bit fields in value are set but value does not correspond to a composite enumeration value, or if value is a string concatenation of the names of multiple bit flags. 在下面的示例中,使用 FlagsAttribute 特性定义了一个宠物枚举。In the following example, a Pets enumeration is defined with the FlagsAttribute attribute. 当向其传递具有两位域( Pets.Cat 和)集的枚举值并向其传递该枚举值的字符串表示形式(" Dog,Cat")时,IsDefined(Type, Object) 方法返回 falseThe IsDefined(Type, Object) method returns false when you pass it an enumeration value that has two bit fields (Pets.Dog and Pets.Cat) set, and when you pass it the string representation of that enumeration value ("Dog, Cat").

[!code-csharpSystem.Enum.IsDefined#2] [!code-vbSystem.Enum.IsDefined#2][!code-csharpSystem.Enum.IsDefined#2] [!code-vbSystem.Enum.IsDefined#2] 可以通过调用 HasFlag(Enum) 方法来确定是否设置了多个位域。You can determine whether multiple bit fields are set by calling the HasFlag(Enum) method.

适用于

另请参阅