Type.DeclaringType Property
Gets the type that declares the current nested type or generic type parameter.
Assembly: mscorlib (in mscorlib.dll)
Property Value
Type: System.TypeA 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.
If the current Type object represents a type parameter of a generic type, this property returns the generic type definition.
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. That is, the following code returns the generic type definition of the List(Of T) generic class, which contains the ConvertAll(Of TOutput) generic method:
Dim t As Type = GetType(List(Of String)).GetMethod("ConvertAll").GetGenericArguments()(0).DeclaringType
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:
If the DeclaringMethod property returns a MethodInfo, that MethodInfo represents a generic method definition, and the current Type object represents a type parameter of that generic method definition.
If the DeclaringMethod property returns null, then the DeclaringType property always returns a Type object representing a generic type definition, and the current Type object represents a type parameter of that generic type definition.
Getting the DeclaringType property on a type whose IsGenericParameter property is false throws an InvalidOperationException.
This example displays the declaring type of a method in a derived class.
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
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1