Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Type.DeclaringType Property

Gets the type that declares the current nested type or generic type parameter.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public override Type DeclaringType { get; }

Property Value

Type: System.Type
A 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.

Implements

_Type.DeclaringType
_MemberInfo.DeclaringType

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<T> generic class, which contains the ConvertAll<TOutput> generic method:

Type t = typeof(List<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.

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);
    }
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

Show:
© 2014 Microsoft