Partager via


Type.DeclaringType Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
Public Overrides ReadOnly Property DeclaringType As Type
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, nulla null reference (Nothing in Visual Basic).

Remarks

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.

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 nulla null reference (Nothing in Visual Basic), 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.

Examples

This example displays the declaring type of a method in a derived class.

Imports System.Reflection

Public MustInherit Class Example

   Public MustInherit Class MyClassA
      Public MustOverride Function m() As Integer
   End Class

   Public MustInherit Class MyClassB
      Inherits MyClassA
   End Class

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      outputBlock.Text += String.Format("The declaring type of m is {0}.", _
         GetType(MyClassB).GetMethod("m").DeclaringType) + vbCrLf
   End Sub
End Class
using System;
using System.Reflection;

public abstract class Example
{

   public abstract class MyClassA
   {
      public abstract int m();
   }

   public abstract class MyClassB : MyClassA
   {
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      outputBlock.Text += String.Format("The declaring type of m is {0}.",
          typeof(MyClassB).GetMethod("m").DeclaringType) + "\n";
   }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.