How get inner class info by Reflection

public abstract class ClassBase
{
public abstract class InnerClassBase
{
}

public void CheckInfo()
{
Type currentType = this.GetType();
//How to get method info of this inner Class
}
}

public class TestClass:ClassBase
{
public new class InnerClassBase
{
public void Test()
{
}
}
}

//Testing code
TestClass test = new TestClass();
test.CheckInfo();




Answer this question

How get inner class info by Reflection

  • Muhammad Ali Inayat

    Jaison LB wrote:

    if (memberinfo.Length > 0)
    {

    Type embededType = Type.GetType(memberinfo[0].DeclaringType.FullName + "+" + memberinfo[0].Name);

    A better way to do that is simply

    Type embededType = (Type)memberinfo[0];



  • Dorie Hannan

    I think this code is helpful to you.....

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Reflection;

    namespace EmbededClass
    {
    class Program
    {

    static void Main(string[] args)
    {
    Derived objDerived = new Derived();
    objDerived.PrintMessage();
    }

    }

    public abstract class Parent
    {

    private abstract class Embeded
    {
    public Embeded()
    {
    }

    public void PrintMessageFromEmbeded()
    {
    Console.WriteLine("Hello World From : " + this.GetType().Name);
    Console.ReadLine();
    }

    }

    public void PrintMessage()
    {

    Type currentType = this.GetType().BaseType;
    System.Reflection.MemberInfo[] memberinfo = currentType.FindMembers(MemberTypes.NestedType,BindingFlags.NonPublic,
    new System.Reflection.MemberFilter(SearchCriteria), "Embeded");

    if (memberinfo.Length > 0)
    {

    Type embededType = Type.GetType(memberinfo[0].DeclaringType.FullName + "+" + memberinfo[0].Name);

    //here you can access all the members of embededType
    }
    }

    public static bool SearchCriteria(MemberInfo objMemberInfo, Object objSearch)
    {

    if (objMemberInfo.Name.ToString() == objSearch.ToString())
    return true;
    else
    return false;

    }
    }

    public class Derived : Parent
    {

    }

    }



  • DKR2006

    I'm not sure I understand the problem. You can't get a MethodInfo for a class.

    I'm also not sure what the purpose of the two InnerClassBase classes are. Classes aren't polymorphic so what's your reason for declaring a new InnerClassBase in TestClass



  • TobyRickett

    Yes

  • johnsmith2005

    If you have the Type of the inner (nested) class, Type.DeclaringType will give you the outer enclosing type.



  • msafi311

    thanks...

  • ujjubee

    Are you asking for a way to find the System.Type object for the outer class from the inner class automatically


  • beginner.net

    Thank you for your reply.
    Your answer does not fit for me. My question is that I know each derived ClassBase must derived InnerClassBase. I want to create a method to get all inforamtion from outer class.



  • Annonnymus

    My answer will be divided in two:
    1. To get a type's full name, use this line:
      string fullname = typeof(ClassBase.InnerClassBase).FullName;
      The string you will receive will be: ClassBase+InnerClassBase.
      To get the type's reflection descriptor, use the following line:
      Type type = Type.GetType(
      "ClassBase+InnerClassBase");
    2. You are trying to derive from an inner class by deriving from the outer class. Your code is, therefore, incomplete. This code will do what you want it to do:
      public class TestClass : ClassBase
      {
      public class InnerClassDerived : InnerClassBase
      {
      public void Test()
      {
      }
      }
      }
      This way, both the outer class will derive from the base outer class and the inner class will derive from the inner class (you had to use the new keyword before, because inner types in derived outer types do no inherit inner types from the base outer type, just in the same way that a method that is not defined as virtual can never be overridden).
    If I have been able to answer your question, please mark it as complete. Otherwise, please provide me with more information about what you need and I'll be more than happy to help. :)


  • How get inner class info by Reflection