Enum.IsDefined Yöntem
Tanım
Önemli
Bazı bilgiler ürünün ön sürümüyle ilgilidir ve sürüm öncesinde önemli değişiklikler yapılmış olabilir. Burada verilen bilgilerle ilgili olarak Microsoft açık veya zımni hiçbir garanti vermez.
Aşırı Yüklemeler
| Name | Description |
|---|---|
| IsDefined(Type, Object) |
Belirtilen bir tamsayı değerinin mi yoksa dize olarak adının mı belirtilen bir numaralandırmada var olduğunu belirten bir Boole değeri döndürür. |
| IsDefined<TEnum>(TEnum) |
Belirtilen bir tamsayı değerinin mi yoksa dize olarak adının mı belirtilen bir numaralandırmada var olduğunu belirten bir boole döndürür. |
IsDefined(Type, Object)
- Kaynak:
- Enum.cs
- Kaynak:
- Enum.cs
- Kaynak:
- Enum.cs
- Kaynak:
- Enum.cs
- Kaynak:
- Enum.cs
Belirtilen bir tamsayı değerinin mi yoksa dize olarak adının mı belirtilen bir numaralandırmada var olduğunu belirten bir Boole değeri döndürür.
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 -> bool
Public Shared Function IsDefined (enumType As Type, value As Object) As Boolean
Parametreler
- enumType
- Type
Numaralandırma türü.
- value
- Object
içindeki enumTypebir sabitin değeri veya adı.
Döndürülenler
true içindeki enumType bir sabite eşit valuebir değer varsa; değilse, false.
- Öznitelikler
Özel durumlar
enumType veya value şeklindedir null.
enumType bir Enumdeğildir.
-veya-
türü value bir sabit listesidir, ancak türünde enumTypebir numaralandırma değildir.
-veya-
türü value , temel alınan bir türü enumTypedeğildir.
Örnekler
Aşağıdaki örnek, tek tek bit alanlarından oluşan adlı PetType bir numaralandırma tanımlar. Ardından, birden çok bit alanı ayarlanmasından IsDefined kaynaklanan olası temel numaralandırma değerleri, dize adları ve bileşik değerlerle yöntemini çağırır.
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
Açıklamalar
value parametresi aşağıdakilerden biri olabilir:
türünde
enumTypeherhangi bir üye.değeri türünde
enumTypebir sabit listesi üyesi olan bir değişken.Bir numaralandırma üyesinin adının dize gösterimi. Dizedeki karakterlerin, numaralandırma üye adıyla aynı büyük/küçük harfe sahip olması gerekir.
Temel alınan türündeki
enumTypebir değer.
içindeki enumType sabitler bir bit alanı kümesi tanımlarsa ve value birden çok bit alanının değerlerini, adlarını veya temel değerlerini içeriyorsa, IsDefined yöntemi döndürür false. Başka bir deyişle, bit alanları kümesini tanımlayan numaralandırmalar için yöntemi yalnızca tek bir bit alanının numaralandırmaya ait olup olmadığını belirler. Özniteliğiyle etiketlenmiş bir numaralandırma türünde birden çok bit alanının ayarlanıp ayarlanmadığını FlagsAttribute belirlemek için yöntemini çağırabilirsiniz HasFlag .
Arayanlara Notlar
özniteliği kullanılarak tanımlanan bir numaralandırmaysaenumType, yöntemi içindeki birden çok bit alanı FlagsAttribute ayarlandıysa, ancak false bileşik sabit listesi değerine karşılık gelmiyorsa veya birden çok bit bayrağının adlarının dize birleştirmesiyse value döndürürvalue.value Aşağıdaki örnekte, özniteliğiyle Pets bir FlagsAttribute numaralandırma tanımlanmıştır. yöntemi, IsDefined(Type, Object) iki bit alanı (false ve ) ayarlanmış bir numaralandırma değeri geçirdiğinizde ve Pets.Dogbunu geçirdiğinizde bu sabit listesi değerinin dize gösterimini ("Köpek, Kedi") geçirirPets.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
yöntemini çağırarak birden çok bit alanının ayarlanıp ayarlanmayacağını HasFlag(Enum) belirleyebilirsiniz.
Ayrıca bkz.
Şunlara uygulanır
IsDefined<TEnum>(TEnum)
- Kaynak:
- Enum.cs
- Kaynak:
- Enum.cs
- Kaynak:
- Enum.cs
- Kaynak:
- Enum.cs
- Kaynak:
- Enum.cs
Belirtilen bir tamsayı değerinin mi yoksa dize olarak adının mı belirtilen bir numaralandırmada var olduğunu belirten bir boole döndürür.
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
Tür Parametreleri
- TEnum
Numaralandırmanın türü.
Parametreler
- value
- TEnum
içindeki TEnumbir sabitin değeri veya adı.
Döndürülenler
true belirtilen bir tam sayı değeri veya dize olarak adı varsa; false Aksi takdir -de.