Type.DeclaringType Właściwość

Definicja

Pobiera typ, który deklaruje bieżący typ zagnieżdżony lub parametr typu ogólnego.

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

Wartość właściwości

Type

Obiekt reprezentujący typ otaczający, jeśli bieżący typ jest typem zagnieżdżony; lub definicja typu ogólnego, jeśli bieżący typ jest parametrem typu ogólnego; lub typ, który deklaruje metodę ogólną, jeśli bieżący typ jest parametrem typu metody ogólnej; w przeciwnym razie Type null .

Implementuje

Przykłady

W tym przykładzie jest wyświetlany deklarowany typ metody w klasie pochodnej.

using namespace System;
using namespace System::Reflection;

public ref class dtype abstract
{
public:
   ref class MyClassA abstract
   {
   public:
      virtual int m() = 0;
   };

   ref class MyClassB abstract: public MyClassA{};
};

int main()
{
   Console::WriteLine( "The declaring type of m is {0}.", dtype::MyClassB::typeid->GetMethod( "m" )->DeclaringType );
}
/* The example produces the following output:

The declaring type of m is dtype+MyClassA.
*/
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.
*/
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

Uwagi

Jeśli bieżący obiekt reprezentuje parametr typu typu ogólnego, ta właściwość Type zwraca definicję typu ogólnego.

Jeśli bieżący obiekt reprezentuje parametr typu metody ogólnej, ta właściwość zwraca typ, który Type zawiera definicję metody ogólnej. Jeśli typ jest ogólny, zwracana jest definicja typu ogólnego. Oznacza to, że poniższy kod zwraca definicję typu ogólnego klasy List<T> ogólnej, która zawiera ConvertAll metodę ogólną:

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

Jeśli bieżący reprezentuje parametr typu w definicji typu ogólnego lub metody ogólnej, właściwości i identyfikują definicję typu ogólnego lub definicję metody ogólnej, w której pierwotnie zdefiniowano parametr Type DeclaringType typu DeclaringMethod ogólnego:

Dotyczy

Zobacz też