Sdílet prostřednictvím


Type.DeclaringType Vlastnost

Definice

Získá typ, který deklaruje aktuální vnořený typ nebo obecný typ parametr.

public:
 abstract property Type ^ DeclaringType { Type ^ get(); };
public:
 virtual property Type ^ DeclaringType { Type ^ get(); };
public abstract Type DeclaringType { get; }
public override Type? DeclaringType { get; }
public override Type DeclaringType { get; }
member this.DeclaringType : Type
Public MustOverride ReadOnly Property DeclaringType As Type
Public Overrides ReadOnly Property DeclaringType As Type

Hodnota vlastnosti

Objekt Type představující ohraničující typ, pokud je aktuální typ vnořený typ; nebo definice obecného typu, je-li aktuálním typem parametr typu obecného typu; nebo typ, který deklaruje obecnou metodu, pokud je aktuální typ parametrem typu obecné metody; jinak . null

Implementuje

Příklady

Tento příklad zobrazuje deklarující typ metody v odvozené třídě.

using System;
using System.Reflection;

public abstract class dtype
{

    public abstract class MyClassA
    {
        public abstract int m();
    }

    public abstract class MyClassB : MyClassA
    {
    }

    public static void Main(string[] args)
    {
        Console.WriteLine("The declaring type of m is {0}.",
            typeof(MyClassB).GetMethod("m").DeclaringType);
    }
}
/* The example produces the following output:

The declaring type of m is dtype+MyClassA.
*/
[<AbstractClass>]
type MyClassA() =
    abstract m: unit -> int

[<AbstractClass>]
type MyClassB() =
    inherit MyClassA()

printfn $"""The declaring type of m is {typeof<MyClassB>.GetMethod("m").DeclaringType}."""
(* The example produces the following output:

The declaring type of m is dtype+MyClassA.
*)
Imports System.Reflection

Public MustInherit Class dtype

    Public MustInherit Class MyClassA
        Public MustOverride Function m() As Integer
    End Class

    Public MustInherit Class MyClassB
        Inherits MyClassA
    End Class

    Public Shared Sub Main()
        Console.WriteLine("The declaring type of m is {0}.", _
           GetType(MyClassB).GetMethod("m").DeclaringType)
    End Sub
End Class

Poznámky

Pokud aktuální Type objekt představuje parametr typu obecného typu, vrátí tato vlastnost definici obecného typu.

Pokud aktuální Type objekt představuje typ parametru obecné metody, tato vlastnost vrátí typ, který obsahuje definici obecné metody. Pokud je typ obecný, vrátí se definice obecného typu. To znamená, že následující kód vrátí definici List<T> obecného typu obecné třídy, která obsahuje obecnou metodu ConvertAll :

Type t = typeof(List<string>).GetMethod("ConvertAll").GetGenericArguments()[0].DeclaringType;
let t = typeof<ResizeArray<string>>.GetMethod("ConvertAll").GetGenericArguments().[0].DeclaringType
Dim t As Type = GetType(List(Of String)).GetMethod("ConvertAll").GetGenericArguments()(0).DeclaringType

Pokud aktuální Type představuje parametr typu v definici obecného typu nebo obecné metody, a DeclaringMethod vlastnosti identifikují definici obecného typu nebo definici obecné metody, DeclaringType kde byl parametr obecného typu původně definován:

Platí pro

Viz také