Reflection in C# - Part 2

By omid eidivandi Oct 08 2010
This blog demonstrates the dynamic usage of reflection in .NET using C# language. The examples in this blog shows how to get members information of a type dynamically.
    • Like
    • Love It
    • Awesome
    • Interesting
    • It's Okay
    • Thumbs Down
  • 7.4k
  • 0

Once you obtain a Type, there are many ways you can discover information about the members of that type. For example, you can find out about all the type's members by calling the Type.GetMembers method, which obtains an array of MemberInfo objects describing each of the members of the current type.

You can also use methods on the Type class to retrieve information about one or more constructors, methods, events, fields, or properties that you specify by name. For example,Type.GetConstructors encapsulates a specific constructor of the current class.

If you have a Type, you can use the Type.Module property to obtain an object that encapsulates the module containing that type. Use the ModuleAssembly property to locate an object that encapsulates the assembly containing the module. You can obtain the assembly that encapsulates the type directly by using the Type.Assembly property.


using System;
using System.Reflection;

Class ListMembers
     public void Main()
        Type t = typeof(String);
        Console.WriteLine("Listing all the public constructors of the {0} type", t);
        ' Constructors.
        ConstructorInfo[] ci = t.GetConstructors((BindingFlags.Public || BindingFlags.Instance))
      public void PrintMembers(MemberInfo[] ms)
          foreach( m in ms)
              Console.WriteLine("{0}{1}", "     ", m);


using System;
using System.IO;
using System.Reflection;

Class Mymemberinfo
Public void Main()
        Console.WriteLine (
' Gets the Type and MemberInfo.
        Type MyType = Type.GetType("System.IO.File");
Type[] Mymemberinfoarray = MyType.GetMembers();
' Gets and displays the DeclaringType method.
        Console.WriteLine("\nThere are {0} members in {1}.",
            Mymemberinfoarray.Length, MyType.FullName);
"{0}.", MyType.FullName);
If (MyType.IsPublic)
"{0} is public.", MyType.FullName);



using System ;
using System.Reflection;
Class MyMethodInfo
Public void Main()
' Gets and displays the Type.
        Type MyType= Type.GetType("System.Reflection.FieldInfo");
' Specifies the member for which you want type information.
        MethodInfo Mymethodinfo = MyType.GetMethod("GetValue");
        Console.WriteLine((MyType.FullName &
"." & Mymethodinfo.Name));
' Gets and displays the MemberType property.
        MemberInfo Mymembertypes = Mymethodinfo.MemberType;
f (MemberTypes.Constructor = Mymembertypes )
            Console.WriteLine("MemberType is of type All");
lseIf(MemberTypes.Custom = Mymembertypes)
            Console.WriteLine("MemberType is of type Custom");
lseIf (MemberTypes.Event = Mymembertypes)
            Console.WriteLine("MemberType is of type Event");
lseIf ( MemberTypes.Field = Mymembertypes)
            Console.WriteLine("MemberType is of type Field");
lseIf (MemberTypes.Method = Mymembertypes)
            Console.WriteLine("MemberType is of type Method");
lseIf (MemberTypes.Property = Mymembertypes)
            Console.WriteLine("MemberType is of type Property");
lseIf (MemberTypes.TypeInfo = Mymembertypes)
            Console.WriteLine("MemberType is of type TypeInfo");

Practice and share your ideas  and questions on reflection here.