Type.BaseType Property
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets the type from which the current Type directly inherits.
public:
abstract property Type ^ BaseType { Type ^ get(); };
public abstract Type? BaseType { get; }
public abstract Type BaseType { get; }
member this.BaseType : Type
Public MustOverride ReadOnly Property BaseType As Type
The Type from which the current Type directly inherits, or null
if the current Type
represents the Object class or an interface.
The following example demonstrates using the BaseType property.
using namespace System;
void main()
{
Type^ t = int::typeid;
Console::WriteLine( "{0} inherits from {1}.", t, t->BaseType );
}
using System;
class TestType
{
public static void Main()
{
Type t = typeof(int);
Console.WriteLine("{0} inherits from {1}.", t,t.BaseType);
}
}
let t = typeof<int>
printfn $"{t} inherits from {t.BaseType}."
Class TestType
Public Shared Sub Main()
Dim t As Type = GetType(Integer)
Console.WriteLine("{0} inherits from {1}.", t, t.BaseType)
End Sub
End Class
The following example uses recursion to list the complete inheritance hierarchy of each class found in an assembly. The example defines a class named C
that derives from a class named B
, which, in turn, derives from a class named A
.
using System;
public class Example
{
public static void Main()
{
foreach (var t in typeof(Example).Assembly.GetTypes()) {
Console.WriteLine("{0} derived from: ", t.FullName);
var derived = t;
do {
derived = derived.BaseType;
if (derived != null)
Console.WriteLine(" {0}", derived.FullName);
} while (derived != null);
Console.WriteLine();
}
}
}
public class A {}
public class B : A
{}
public class C : B
{}
// The example displays the following output:
// Example derived from:
// System.Object
//
// A derived from:
// System.Object
//
// B derived from:
// A
// System.Object
//
// C derived from:
// B
// A
// System.Object
type A() = class end
type B() = inherit A()
type C() = inherit B()
module Example =
[<EntryPoint>]
let main _ =
for t in typeof<A>.Assembly.GetTypes() do
printfn $"{t.FullName} derived from: "
let mutable derived = t
while derived <> null do
derived <- derived.BaseType
if derived <> null then
printfn $" {derived.FullName}"
printfn ""
0
// The example displays the following output:
// Example derived from:
// System.Object
//
// A derived from:
// System.Object
//
// B derived from:
// A
// System.Object
//
// C derived from:
// B
// A
// System.Object
Public Class Example
Public Shared Sub Main()
For Each t In GetType(Example).Assembly.GetTypes()
Console.WriteLine("{0} derived from: ", t.FullName)
Dim derived As Type = t
Do
derived = derived.BaseType
If derived IsNot Nothing Then
Console.WriteLine(" {0}", derived.FullName)
End If
Loop While derived IsNot Nothing
Console.WriteLine()
Next
End Sub
End Class
Public Class A
End Class
Public Class B : Inherits A
End Class
Public Class C : Inherits B
End Class
' The example displays the following output:
' Example derived from:
' System.Object
'
' A derived from:
' System.Object
'
' B derived from:
' A
' System.Object
'
' C derived from:
' B
' A
' System.Object
The base type is the type from which the current type directly inherits. Object is the only type that does not have a base type, therefore null
is returned as the base type of Object.
Interfaces inherit from zero or more base interfaces; therefore, this property returns null
if the Type
object represents an interface. The base interfaces can be determined with GetInterfaces or FindInterfaces.
If the current Type represents a constructed generic type, the base type reflects the generic arguments. For example, consider the following declarations:
generic<typename U> ref class B { };
generic<typename T> ref class C : B<T> { };
class B<U> { }
class C<T> : B<T> { }
type B<'U>() = class end
type C<'T>() = inherit B<'T>()
Class B(Of U)
End Class
Class C(Of T)
Inherits B(Of T)
End Class
For the constructed type C<int>
(C(Of Integer)
in Visual Basic), the BaseType property returns B<int>
.
If the current Type represents a type parameter of a generic type definition, BaseType returns the class constraint, that is, the class the type parameter must inherit. If there is no class constraint, BaseType returns System.Object.
This property is read-only.
Produit | Versions |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
Commentaires sur .NET
.NET est un projet open source. Sélectionnez un lien pour fournir des commentaires :