Type.IsClass Właściwość

Definicja

Pobiera wartość wskazującą, czy Type jest klasą, czy delegatem, czyli nie typem wartości lub interfejsem.

public:
 property bool IsClass { bool get(); };
public bool IsClass { get; }
member this.IsClass : bool
Public ReadOnly Property IsClass As Boolean

Wartość właściwości

truejeśli jest Type klasą; w przeciwnym razie . false

Implementuje

Przykłady

Poniższy przykład tworzy wystąpienie typu i wskazuje, czy typ jest klasą.

using namespace System;
using namespace System::Reflection;
public ref class MyDemoClass{};

int main()
{
   try
   {
      Type^ myType = Type::GetType( "MyDemoClass" );
      
      // Get and display the 'IsClass' property of the 'MyDemoClass' instance.
      Console::WriteLine( "\nIs the specified type a class? {0}.", myType->IsClass );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "\nAn exception occurred: {0}.", e->Message );
   }

}
using System;
using System.Reflection;

public  class MyDemoClass
{
}

public class MyTypeClass
{
    public static void Main(string[] args)
    {
        try
        {
            Type  myType = typeof(MyDemoClass);
            // Get and display the 'IsClass' property of the 'MyDemoClass' instance.
            Console.WriteLine("\nIs the specified type a class? {0}.", myType.IsClass);
        }
        catch(Exception e)
        {
            Console.WriteLine("\nAn exception occurred: {0}." ,e.Message);
        }
    }
}
type MyDemoClass = class end

try
    let myType = typeof<MyDemoClass>
    // Get and display the 'IsClass' property of the 'MyDemoClass' instance.
    printfn $"\nIs the specified type a class? {myType.IsClass}."
with e ->
    printfn $"\nAn exception occurred: {e.Message}."
Imports System.Reflection

Public Class MyDemoClass
End Class

Public Class MyTypeClass
    Public Shared Sub Main()
        Try
            Dim myType As Type = GetType(MyDemoClass)
            ' Get and display the 'IsClass' property of the 'MyDemoClass' instance.
            Console.WriteLine(ControlChars.Cr + "Is the specified type a class? {0}.", myType.IsClass.ToString())
        Catch e As Exception
            Console.WriteLine(ControlChars.Cr + "An exception occurred: {0}.", e.Message.ToString())
        End Try
    End Sub
End Class

Uwagi

Ta właściwość zwraca true wartość dla klas, a także delegatów. Zwraca false wartość dla typów wartości (dla struktur i wyliczenia), nawet jeśli są one w polu.

Jeśli bieżący Type reprezentuje parametr typu w definicji typu ogólnego lub metody ogólnej, ta właściwość zawsze zwraca wartość true. Jeśli bieżący Type reprezentuje skonstruowany typ ogólny, ta właściwość zwraca true wartość, jeśli definicja typu ogólnego jest definicją klasy; oznacza to, że nie definiuje interfejsu ani typu wartości.

Uwaga

Ta właściwość zwraca true wartość dla Type wystąpień reprezentujących Enum klasy i ValueType . Te dwie klasy są typami podstawowymi odpowiednio dla wyliczenia i typów wartości, ale nie są to same wyliczenia ani typy wartości. Aby uzyskać więcej informacji, zobacz właściwości IsValueType i IsEnum.

Wartość TypeAttributes.ClassSemanticsMask wyliczenia rozróżnia deklarację typu jako klasę lub interfejs. Jednak zarówno klasy, jak i typy wartości są oznaczone atrybutem TypeAttributes.Class . Jeśli pobierasz wartość właściwości Atrybuty typu i użyjesz TypeAttributes.ClassSemanticsMask wartości w celu określenia, czy typ jest klasą zamiast typu wartości, należy również wywołać IsValueType właściwość . Przykład wyliczenia TypeAttributes zawiera dodatkowe informacje, a także przykładowe.

Ta właściwość jest tylko do odczytu.

Dotyczy

Zobacz też