Get the type that declares the current nested type or generic type parameter in CSharp

Description

The following code shows how to get the type that declares the current nested type or generic type parameter.

Example


//  w  ww .  j a  v a 2  s  .co m
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 code above generates the following result.





















Home »
  C# Tutorial »
    Reflection »




Array
Constructor
Event
Field
Interface
Method
Properties
Type