次の方法で共有


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 オブジェクト。現在の型がジェネリック型の型パラメーターの場合はジェネリック型定義。現在の型がジェネリック メソッドの型パラメーターの場合はジェネリック メソッドを宣言する型。それ以外の場合は null

実装

次の使用例は、派生クラスのメソッドの宣言型を表示します。

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.
*/
[<AbstractClass>]
type MyClassA() =
    abstract m: unit -> int

[<AbstractClass>]
type MyClassB() =
    inherit MyClassA()

printfn $"""The declaring type of m is {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 オブジェクトがジェネリック メソッドの型パラメーターを表す場合、このプロパティはジェネリック メソッド定義を含む型を返します。 型がジェネリックの場合は、ジェネリック型の定義が返されます。 つまり、次のコードは、ConvertAll ジェネリック メソッドを含む List<T> ジェネリック クラスのジェネリック型定義を返します。

Type t = typeof(List<string>).GetMethod("ConvertAll").GetGenericArguments()[0].DeclaringType;
let t = typeof<ResizeArray<string>>.GetMethod("ConvertAll").GetGenericArguments().[0].DeclaringType
Dim t As Type = GetType(List(Of String)).GetMethod("ConvertAll").GetGenericArguments()(0).DeclaringType

現在の Type がジェネリック型またはジェネリック メソッドの定義で型パラメーターを表す場合、 DeclaringType プロパティと DeclaringMethod プロパティは、ジェネリック型パラメーターが最初に定義されたジェネリック型定義またはジェネリック メソッド定義を識別します。

  • DeclaringMethod プロパティがMethodInfoを返す場合、そのMethodInfoはジェネリック メソッド定義を表し、現在のType オブジェクトはそのジェネリック メソッド定義の型パラメーターを表します。

  • DeclaringMethod プロパティがnullを返す場合、DeclaringType プロパティは常にジェネリック型定義を表すType オブジェクトを返し、現在のType オブジェクトはそのジェネリック型定義の型パラメーターを表します。

  • IsGenericParameter プロパティがfalseInvalidOperationExceptionをスローする型のDeclaringType プロパティを取得します。

適用対象

こちらもご覧ください