Type.IsInstanceOfType(Object) Methode
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Bestimmt, ob das angegebene Objekt eine Instanz des aktuellen Type ist.
public:
virtual bool IsInstanceOfType(System::Object ^ o);
public virtual bool IsInstanceOfType (object? o);
public virtual bool IsInstanceOfType (object o);
abstract member IsInstanceOfType : obj -> bool
override this.IsInstanceOfType : obj -> bool
Public Overridable Function IsInstanceOfType (o As Object) As Boolean
Parameter
- o
- Object
Das mit dem aktuellen Typ zu vergleichende Objekt.
Gibt zurück
true
, wenn der aktuelle Type
in der Vererbungshierarchie des von o
dargestellten Objekts vorhanden ist oder wenn der aktuelle Type
eine Schnittstelle ist, die von o
implementiert wird. false
, wenn keine dieser Bedingungen zutrifft, wenn o
den Wert null
hat oder wenn der aktuelle Type
ein offener generischer Typ ist (d. h. ContainsGenericParameters gibt true
zurück).
Implementiert
Beispiele
Im folgenden Beispiel wird die Verwendung der IsInstanceOfType
-Methode gezeigt.
using namespace System;
public interface class IExample{};
public ref class BaseClass: IExample{};
public ref class DerivedClass: BaseClass{};
void main()
{
Type^ interfaceType = IExample::typeid;
BaseClass^ base1 = gcnew BaseClass;
Type^ base1Type = base1->GetType();
BaseClass^ derived1 = gcnew DerivedClass;
Type^ derived1Type = derived1->GetType();
array<Int32>^ arr = gcnew array<Int32>(11);
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 BaseClass? {0}.",
base1Type->IsInstanceOfType( base1 ) );
Console::WriteLine("Is myderivedclass an instance of BaseClass? {0}.",
base1Type->IsInstanceOfType( derived1 ) );
Console::WriteLine("Is myclass an instance of IExample? {0}.",
interfaceType->IsInstanceOfType( base1 ) );
Console::WriteLine("Is myderivedclass an instance of IExample? {0}.",
interfaceType->IsInstanceOfType( derived1 ) );
}
// The example displays the following output:
// Is int[] an instance of the Array class? True.
// Is base1 an instance of BaseClass? True.
// Is derived1 an instance of BaseClass? True.
// Is base1 an instance of IExample? True.
// Is derived1 an instance of IExample? True.
using System;
public interface IExample {}
public class BaseClass : IExample {}
public class DerivedClass : BaseClass {}
public class Example
{
public static void Main()
{
var interfaceType = typeof(IExample);
var base1 = new BaseClass();
var base1Type = base1.GetType();
var derived1 = new DerivedClass();
var derived1Type = derived1.GetType();
int[] arr = new int[11];
Console.WriteLine("Is int[] an instance of the Array class? {0}.",
typeof(Array).IsInstanceOfType(arr));
Console.WriteLine("Is base1 an instance of BaseClass? {0}.",
base1Type.IsInstanceOfType(base1));
Console.WriteLine("Is derived1 an instance of BaseClass? {0}.",
base1Type.IsInstanceOfType(derived1));
Console.WriteLine("Is base1 an instance of IExample? {0}.",
interfaceType.IsInstanceOfType(base1));
Console.WriteLine("Is derived1 an instance of IExample? {0}.",
interfaceType.IsInstanceOfType(derived1));
}
}
// The example displays the following output:
// Is int[] an instance of the Array class? True.
// Is base1 an instance of BaseClass? True.
// Is derived1 an instance of BaseClass? True.
// Is base1 an instance of IExample? True.
// Is derived1 an instance of IExample? True.
Public Interface IExample
End Interface
Public Class BaseClass : Implements IExample
End Class
Public Class DerivedClass : Inherits BaseClass
End Class
Public Module Example
Public Sub Main()
Dim interfaceType As Type = GetType(IExample)
Dim base1 As New BaseClass()
Dim base1Type As Type = base1.GetType()
Dim derived1 = New DerivedClass()
Dim derived1Type As Type = derived1.GetType()
Dim arr(10) As Integer
Console.WriteLine("Is int[] an instance of the Array class? {0}.",
GetType(Array).IsInstanceOfType(arr))
Console.WriteLine("Is base1 an instance of BaseClass? {0}.",
base1Type.IsInstanceOfType(base1))
Console.WriteLine("Is derived1 an instance of BaseClass? {0}.",
base1Type.IsInstanceOfType(derived1))
Console.WriteLine("Is base1 an instance of IExample? {0}.",
interfaceType.IsInstanceOfType(base1))
Console.WriteLine("Is derived1 an instance of IExample? {0}.",
interfaceType.IsInstanceOfType(derived1))
End Sub
End Module
' The example displays the following output:
' Is int[] an instance of the Array class? True.
' Is base1 an instance of BaseClass? True.
' Is derived1 an instance of BaseClass? True.
' Is base1 an instance of IExample? True.
' Is derived1 an instance of IExample? True.
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>
in MyGenericList(Of T)
Visual Basic).