Enum.IsDefined 方法  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
| IsDefined(Type, Object) | 返回一个布尔值,该值指示给定的整数值或其名称字符串是否存在于指定的枚举中。 | 
| IsDefined<TEnum>(TEnum) | 返回一个布尔值,该值指示给定的整数值或其名称字符串是否存在于指定的枚举中。 | 
IsDefined(Type, Object)
- Source:
- Enum.cs
- Source:
- Enum.cs
- Source:
- Enum.cs
- Source:
- Enum.cs
返回一个布尔值,该值指示给定的整数值或其名称字符串是否存在于指定的枚举中。
public:
 static bool IsDefined(Type ^ enumType, System::Object ^ value);public static bool IsDefined(Type enumType, object value);[System.Runtime.InteropServices.ComVisible(true)]
public static bool IsDefined(Type enumType, object value);static member IsDefined : Type * obj -> bool[<System.Runtime.InteropServices.ComVisible(true)>]
static member IsDefined : Type * obj -> boolPublic Shared Function IsDefined (enumType As Type, value As Object) As Boolean参数
- enumType
- Type
枚举类型。
- value
- Object
              enumType 中的常量的值或名称。
返回
如果 enumType 中的某个常量的值等于 value,则为 true;否则为 false。
- 属性
例外
              enumType 或 value 为 null。
              enumType 不是 Enum。
- 或 -
              value 的类型是枚举,但它不是类型 enumType 的枚举。
- 或 -
              value 的类型不是 enumType 的基础类型。
示例
以下示例定义一个名为 PetType 的枚举,该枚举由单个位字段组成。 然后, IsDefined 它使用可能的基础枚举值、字符串名称和设置多个位字段产生的复合值调用 方法。
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
open System
[<Flags>]
type PetType =
    | None = 0
    | Dog = 1
    | Cat = 2
    | Rodent = 4
    | Bird = 8
    | Reptile = 16
    | Other = 32
[<EntryPoint>]
let main _ =
    // Call IsDefined with underlying integral value of member.
    let value = 1
    printfn $"{value}: {Enum.IsDefined(typeof<PetType>, value)}"
    // Call IsDefined with invalid underlying integral value.
    let value = 64
    printfn $"{value}: {Enum.IsDefined(typeof<PetType>, value)}"
    // Call IsDefined with string containing member name.
    let value = "Rodent"
    printfn $"{value}: {Enum.IsDefined(typeof<PetType>, value)}"
    // Call IsDefined with a variable of type PetType.
    let value = PetType.Dog
    printfn $"{value}: {Enum.IsDefined(typeof<PetType>, value)}"
    let value = PetType.Dog ||| PetType.Cat
    printfn $"{value}: {Enum.IsDefined(typeof<PetType>, value)}"
    // Call IsDefined with uppercase member name.
    let value = "None"
    printfn $"{value}: {Enum.IsDefined(typeof<PetType>, value)}"
    let value = "NONE"
    printfn $"{value}: {Enum.IsDefined(typeof<PetType>, value)}"
    // Call IsDefined with combined value
    let value = PetType.Dog ||| PetType.Bird
    printfn $"{value:D}: {Enum.IsDefined(typeof<PetType>, value)}"
    let value = value.ToString()
    printfn $"{value:D}: {Enum.IsDefined(typeof<PetType>, value)}"
    0
// 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 可以是以下任一项:
- 类型 - enumType的任何成员。
- 一个变量,其值为 类型的 - enumType枚举成员。
- 枚举成员名称的字符串表示形式。 字符串中的字符必须与枚举成员名称具有相同大小写。 
- 基础类型的 - enumType值。
如果 中的 enumType 常量定义一组位字段并 value 包含多个位字段的值、名称或基础值,则 IsDefined 该方法返回 false。 换句话说,对于定义一组位字段的枚举, 方法仅确定单个位字段是否属于枚举。 若要确定是否在用 FlagsAttribute 属性标记的枚举类型中设置多个位字段,可以调用 HasFlag 方法。
调用方说明
如果 enumType 是使用 FlagsAttribute 特性定义的枚举,则如果 中的多个位字段value已设置但value与复合枚举值不对应,或者如果 value 是多个位标志名称的字符串串联,则方法返回 false 。 在下面的示例中, Pets 枚举是使用 FlagsAttribute 属性定义的。 向它传递具有两个位字段 (Pets.Dog和Pets.Cat) 集的枚举值时,该方法IsDefined(Type, Object)返回 false ;传递它时,该枚举值的字符串表示形式 (“Dog, Cat”) 。
using System;
[Flags] public enum Pets {
      None = 0, Dog = 1, Cat = 2, Bird = 4,
      Rodent = 8, Other = 16 };
public class Example
{
   public static void Main()
   {
      Pets value = Pets.Dog | Pets.Cat;
      Console.WriteLine("{0:D} Exists: {1}",
                        value, Pets.IsDefined(typeof(Pets), value));
      string name = value.ToString();
      Console.WriteLine("{0} Exists: {1}",
                        name, Pets.IsDefined(typeof(Pets), name));
   }
}
// The example displays the following output:
//       3 Exists: False
//       Dog, Cat Exists: False
open System
[<Flags>]
type Pets =
    | None = 0
    | Dog = 1
    | Cat = 2
    | Bird = 4
    | Rodent = 8
    | Other = 16
let value = Pets.Dog ||| Pets.Cat
printfn $"{value:D} Exists: {Pets.IsDefined(typeof<Pets>, value)}"
let name = string value
printfn $"{name} Exists: {Pets.IsDefined(typeof<Pets>, name)}"
// The example displays the following output:
//       3 Exists: False
//       Dog, Cat Exists: False
<Flags> Public Enum Pets As Integer
   None = 0
   Dog = 1
   Cat = 2
   Bird = 4
   Rodent = 8
   Other = 16
End Enum
Module Example
   Public Sub Main()
      Dim value As Pets = Pets.Dog Or Pets.Cat
      Console.WriteLine("{0:D} Exists: {1}", 
                        value, Pets.IsDefined(GetType(Pets), value))
      Dim name As String = value.ToString()
      Console.WriteLine("{0} Exists: {1}", 
                        name, Pets.IsDefined(GetType(Pets), name))
   End Sub
End Module
' The example displays the following output:
'       3 Exists: False
'       Dog, Cat Exists: False
可以通过调用 HasFlag(Enum) 方法来确定是否设置多个位字段。
另请参阅
适用于
IsDefined<TEnum>(TEnum)
- Source:
- Enum.cs
- Source:
- Enum.cs
- Source:
- Enum.cs
- Source:
- Enum.cs
返回一个布尔值,该值指示给定的整数值或其名称字符串是否存在于指定的枚举中。
public:
generic <typename TEnum>
 where TEnum : value class static bool IsDefined(TEnum value);public static bool IsDefined<TEnum>(TEnum value) where TEnum : struct;static member IsDefined : 'Enum -> bool (requires 'Enum : struct)Public Shared Function IsDefined(Of TEnum As Structure) (value As TEnum) As Boolean类型参数
- TEnum
枚举的类型。
参数
- value
- TEnum
              TEnum 中的常量的值或名称。
返回
如果给定整数值或其名称字符串存在于指定的枚举中,则为 true,否则为 false。