Type.DeclaringType 屬性

定義

取得宣告目前巢狀類型或泛型型別參數的類型。

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

屬性值

Type

若目前的類型是巢狀類型,即為表示封入類型的 Type 物件,若目前的類型是泛型類型的型別參數,則為泛型類型定義,而若目前的類型是泛型方法的型別參數,則為宣告泛型方法的類型,若以上皆否,便為 null

實作

範例

這個範例會顯示衍生類別中方法的宣告型別。

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

備註

如果目前的 Type 物件代表泛型型別的型別參數,則這個屬性會傳回泛型型別定義。

如果目前的 Type 物件代表泛型方法的型別參數,這個屬性會傳回包含泛型方法定義的型別。 如果類型為泛型,則會傳回泛型型別定義。 也就是說,下列程式碼會傳回泛型類別的泛型型別定義 List<T> ,其中包含 ConvertAll 泛型方法:

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

如果目前 Type 表示泛型型別或泛型方法定義中的類型參數, DeclaringType 和屬性會識別一般類型 DeclaringMethod 參數原本定義所在的泛型型別定義或泛型方法定義:

適用於

另請參閱