Gets the type that declares the type represented by the current instance.
The Type object for the class that declares the type represented by the current instance. If the type is a nested type, this property returns the enclosing type; otherwise, returns the current instance.
Note: This property implements the abstract property inherited from Reflection.MemberInfo.If the current Type represents a type parameter of a generic type or method definition, the Type.DeclaringType and Type.DeclaringMethod properties identify the generic type definition or generic method definition where the generic type parameter was originally defined:
- If the Type.DeclaringMethod property returns a System.Reflection.MethodBase, that System.Reflection.MethodBase represents a generic method definition, and the current Type object represents a type parameter of that generic method definition.
- If the Type.DeclaringMethod property returns a null, then the Type.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.
For a type parameter of a generic method, this property returns the type that contains the generic method definition.
The following example demonstrates the Type.DeclaringType property.
C# Example
using System; using System.Reflection; public abstract class DeclaringTypeTest{ public abstract class MyClassA { public abstract int m(); } public abstract class MyClassB : MyClassA { } public static void Main() { Console.WriteLine("Declaring type of m is {0}", typeof(MyClassB).GetMethod("m").DeclaringType); } }The output is
Declaring type of m is DeclaringTypeTest+MyClassA
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0