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 表示泛型型別或泛型方法定義中的類型參數, DeclaringType 和屬性會識別一般類型 DeclaringMethod 參數原本定義所在的泛型型別定義或泛型方法定義:

適用於

產品 版本
.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

另請參閱