Enum.IsDefined(Type, Object) Enum.IsDefined(Type, Object) Enum.IsDefined(Type, Object) Enum.IsDefined(Type, Object) Method

定義

傳回布林值,告知指定整數值或其作為字串的名稱,是否存在於指定的列舉中。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 Type Type Type

列舉類型。An enumeration type.

value
Object Object Object 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.

範例

下列範例會定義名為列舉型別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:

  • 類型的任何成員enumTypeAny member of type enumType.

  • 其值是列舉成員型別的變數enumTypeA 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.

  • 型別的基礎值enumTypeA 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. 在下列範例中, Pets列舉的定義包含FlagsAttribute屬性。In the following example, a Pets enumeration is defined with the FlagsAttribute attribute. IsDefined(Type, Object)方法會傳回false傳遞時,它具有兩個位元欄位的列舉值 (Pets.DogPets.Cat) 設定,並當您將它傳遞的字串表示,列舉值 ("Dog、 Cat 」)。The 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.

適用於

另請參閱