Editare

Partajați prin


Type.IsClass Property

Definition

Gets a value indicating whether the Type is a class or a delegate; that is, not a value type or interface.

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

Property Value

true if the Type is a class; otherwise, false.

Implements

Examples

The following example creates an instance of a type and indicates whether the type is a class.

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

Remarks

This property returns true for classes as well as delegates. It returns false for value types (for structures and enumerations) even if they are boxed.

If the current Type represents a type parameter in the definition of a generic type or generic method, this property always returns true. If the current Type represents a constructed generic type, this property returns true if the generic type definition is a class definition; that is, it does not define an interface or a value type.

Note

This property returns true for Type instances that represent the Enum and ValueType classes. These two classes are the base types for enumerations and value types, respectively, but they are not enumerations or value types themselves. For more information, see the IsValueType and IsEnum properties.

The TypeAttributes.ClassSemanticsMask enumeration value distinguishes a type declaration as class or interface. However, both classes and value types are marked with the TypeAttributes.Class attribute. If you retrieve the value of a type's Attributes property and use the TypeAttributes.ClassSemanticsMask value to determine whether a type is a class instead of a value type, you must also call the IsValueType property. The example for the TypeAttributes enumeration contains additional information as well as anexample.

This property is read-only.

Applies to

See also