Freigeben über


Type.IsInstanceOfType-Methode

Bestimmt, ob das angegebene Objekt eine Instanz des aktuellen Type ist.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Overridable Function IsInstanceOfType ( _
    o As Object _
) As Boolean
'Usage
Dim instance As Type
Dim o As Object
Dim returnValue As Boolean

returnValue = instance.IsInstanceOfType(o)
public virtual bool IsInstanceOfType (
    Object o
)
public:
virtual bool IsInstanceOfType (
    Object^ o
)
public boolean IsInstanceOfType (
    Object o
)
public function IsInstanceOfType (
    o : Object
) : boolean

Parameter

  • o
    Das Objekt, das mit dem aktuellen Type verglichen werden soll.

Rückgabewert

true unter den folgenden Bedingungen: Der aktuelle Type befindet sich in der Vererbungshierarchie des Objekts, das von o dargestellt wird, oder der aktuelle Type ist eine Schnittstelle, die von o unterstützt wird. false, wenn keine diese beiden Bedingungen zutrifft, oNULL (Nothing in Visual Basic) ist oder der aktuelle Type ein offener generischer Typ ist (das heißt, ContainsGenericParameters gibt true zurück).

Hinweise

Diese Methode kann von einer abgeleiteten Klasse überschrieben werden.

Hinweis

Ein konstruierter Typ ist keine Instanz seiner generischen Typdefinition. Das heißt, MyGenericList<int> (MyGenericList(Of Integer) in Visual Basic) ist keine Instanz von MyGenericList<T> (MyGenericList(Of T) in Visual Basic).

Beispiel

Das folgende Beispiel veranschaulicht die Verwendung der IsInstanceOfType-Methode.

Imports System
Public Interface IMyIfc
End Interface 'IMyIfc
Public Class [MyClass]
    Implements IMyIfc
End Class '[MyClass]
Public Class MyDerivedClass
    Inherits [MyClass]
End Class 'MyDerivedClass
Class IsInstanceTest
    Public Shared Sub Main()
        Dim imyifcType As Type = GetType(IMyIfc)
        Dim mc As New [MyClass]()
        Dim mcType As Type = mc.GetType()
        Dim mdc = New MyDerivedClass()
        Dim mdcType As Type = mdc.GetType()
        Dim array(10) As Integer
        Dim arrayType As Type = GetType(Array)
        Console.WriteLine("Is int[] an instance of the Array class? {0}.", arrayType.IsInstanceOfType(array))
        Console.WriteLine("Is myclass an instance of MyClass? {0}.", mcType.IsInstanceOfType(mc))
        Console.WriteLine("Is myderivedclass an instance of MyClass? {0}.", mcType.IsInstanceOfType(mdc))
        Console.WriteLine("Is myclass an instance of IMyIfc? {0}.", imyifcType.IsInstanceOfType(mc))
        Console.WriteLine("Is myderivedclass an instance of IMyIfc? {0}.", imyifcType.IsInstanceOfType(mdc))
    End Sub 'Main
End Class 'IsInstanceTest
using System;
public interface IMyIfc {}
public class MyClass : IMyIfc {}
public class MyDerivedClass : MyClass {}
class IsInstanceTest 
{
    public static void Main() 
    {
        Type imyifcType=typeof(IMyIfc);
        MyClass mc = new MyClass();
        Type mcType = mc.GetType();
        MyClass mdc = new MyDerivedClass();
        Type mdcType = mdc.GetType();
        int [] array  = new int [10];
        Type arrayType = typeof(Array);
        Console.WriteLine("Is int[] an instance of the Array class? {0}.", arrayType.IsInstanceOfType(array));
        Console.WriteLine("Is myclass an instance of MyClass? {0}.", mcType.IsInstanceOfType(mc));
        Console.WriteLine("Is myderivedclass an instance of MyClass? {0}.", mcType.IsInstanceOfType(mdc));
        Console.WriteLine("Is myclass an instance of IMyIfc? {0}.", imyifcType.IsInstanceOfType(mc));
        Console.WriteLine("Is myderivedclass an instance of IMyIfc? {0}.", imyifcType.IsInstanceOfType(mdc));
    }
}
using namespace System;
interface class IMyIfc{};
public ref class MyClass: public IMyIfc{};

public ref class MyDerivedClass: public MyClass{};

int main()
{
   Type^ imyifcType = IMyIfc::typeid;
   MyClass^ mc = gcnew MyClass;
   Type^ mcType = mc->GetType();
   MyClass^ mdc = gcnew MyDerivedClass;
   Type^ mdcType = mdc->GetType();
   array<Int32>^arr = gcnew array<Int32>(10);
   Type^ arrayType = Array::typeid;
   Console::WriteLine( "Is Int32[] an instance of the Array class? {0}.", arrayType->IsInstanceOfType( arr ) );
   Console::WriteLine( "Is myclass an instance of MyClass? {0}.", mcType->IsInstanceOfType( mc ) );
   Console::WriteLine( "Is myderivedclass an instance of MyClass? {0}.", mcType->IsInstanceOfType( mdc ) );
   Console::WriteLine( "Is myclass an instance of IMyIfc? {0}.", imyifcType->IsInstanceOfType( mc ) );
   Console::WriteLine( "Is myderivedclass an instance of IMyIfc? {0}.", imyifcType->IsInstanceOfType( mdc ) );
}
import System.*;
public interface IMyIfc
{
} //IMyIfc

public class MyClass implements IMyIfc
{
} //MyClass

public class MyDerivedClass extends MyClass
{
} //MyDerivedClass

class IsInstanceTest
{
    public static void main(String[] args)
    {
        Type imyifcType = IMyIfc.class.ToType();
        MyClass mc = new MyClass();
        Type mcType = mc.GetType();
        MyClass mdc = new MyDerivedClass();
        Type mdcType = mdc.GetType();
        int array[] = new int[10];
        Type arrayType = Array.class.ToType();
        Console.WriteLine("Is int[] an instance of the Array class? {0}.",
            System.Convert.ToString(arrayType.IsInstanceOfType(array)));
        Console.WriteLine("Is myclass an instance of MyClass? {0}.", 
            System.Convert.ToString(mcType.IsInstanceOfType(mc)));
        Console.WriteLine("Is myderivedclass an instance of MyClass? {0}.",
            System.Convert.ToString(mcType.IsInstanceOfType(mdc)));
        Console.WriteLine("Is myclass an instance of IMyIfc? {0}.",
            System.Convert.ToString(imyifcType.IsInstanceOfType(mc)));
        Console.WriteLine("Is myderivedclass an instance of IMyIfc? {0}.", 
            System.Convert.ToString(imyifcType.IsInstanceOfType(mdc)));
    } //main
} //IsInstanceTest

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

Type-Klasse
Type-Member
System-Namespace