Type.DeclaringType Type.DeclaringType Type.DeclaringType Type.DeclaringType Property

定義

取得宣告目前巢狀類型或泛型型別參數的類型。Gets the type that declares the current nested type or generic type parameter.

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

屬性值

若目前的類型是巢狀類型,即為表示封入類型的 Type 物件,若目前的類型是泛型類型的型別參數,則為泛型類型定義,而若目前的類型是泛型方法的型別參數,則為宣告泛型方法的類型,若以上皆否,便為 nullA Type object representing the enclosing type, if the current type is a nested type; or the generic type definition, if the current type is a type parameter of a generic type; or the type that declares the generic method, if the current type is a type parameter of a generic method; otherwise, null.

實作

範例

這個範例會顯示衍生類別中方法的宣告型別。This example displays the declaring type of a method in a derived class.

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 );
}
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);
    }
}
Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

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的物件代表泛型型別的型別參數, 這個屬性會傳回泛型型別定義。If the current Type object represents a type parameter of a generic type, this property returns the generic type definition.

如果目前Type的物件代表泛型方法的型別參數, 則這個屬性會傳回包含泛型方法定義的型別。If the current Type object represents a type parameter of a generic method, this property returns the type that contains the generic method definition. 如果類型是泛型, 則會傳回泛型型別定義。If the type is generic, the generic type definition is returned. 也就是說, 下列程式碼會傳回List<T>泛型類別的泛型型別定義, 其中ConvertAll包含泛型方法:That is, the following code returns the generic type definition of the List<T> generic class, which contains the ConvertAll generic method:

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屬性會識別泛型型別定義或泛型方法定義, 其中的泛型型別參數原先已定義:If the current Type represents a type parameter in the definition of a generic type or generic method, the DeclaringType and DeclaringMethod properties identify the generic type definition or generic method definition where the generic type parameter was originally defined:

適用於

另請參閱