Type.DeclaringType 属性

定义

获取用来声明当前的嵌套类型或泛型类型参数的类型。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.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对象表示泛型类型的类型参数,则此属性返回泛型类型定义。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表示泛型类型或泛型方法的定义中的类型参数,则DeclaringTypeDeclaringMethod属性标识泛型类型定义或泛型方法定义,其中泛型类型参数最初定义: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:

适用于

另请参阅