Type.DeclaringType 属性

定义

获取用来声明当前的嵌套类型或泛型类型参数的类型。

C#
public abstract Type DeclaringType { get; }
C#
public override Type? DeclaringType { get; }
C#
public override Type DeclaringType { get; }

属性值

Type

如果当前的类型是嵌套类型,则为表示封闭类型的 Type 对象;如果当前的类型是泛型类型的类型参数,则为泛型类型的定义;如果当前的类型是泛型方法的类型参数,则为用来声明泛型方法的类型;否则为 null

实现

示例

此示例显示派生类中方法的声明类型。

C#
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.
*/

注解

如果当前 Type 对象表示泛型类型的类型参数,则此属性返回泛型类型定义。

如果当前 Type 对象表示泛型方法的类型参数,则此属性返回包含泛型方法定义的类型。 如果类型为泛型,则返回泛型类型定义。 也就是说,下面的代码返回 List<T> 包含泛型方法的泛型类的泛型类型定义 ConvertAll

C#
Type t = typeof(List<string>).GetMethod("ConvertAll").GetGenericArguments()[0].DeclaringType;

如果当前 Type 表示泛型类型或泛型方法的定义中的类型参数,则 DeclaringTypeDeclaringMethod 属性标识最初定义泛型类型参数的泛型类型定义或泛型方法定义:

适用于

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

另请参阅