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.
Example1:
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))
Console.WriteLine("//Constructors");
PrintMembers(ci);
}
public void PrintMembers(MemberInfo[] ms)
{
MemberInfo m;
foreach( m in ms)
Console.WriteLine("{0}{1}", " ", m);
Console.WriteLine();
}
}
Example2:
using System;
using System.IO;
using System.Reflection;
Class Mymemberinfo
{
Public void Main()
{
Console.WriteLine ("\nReflection.MemberInfo");
' 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);
Console.WriteLine("{0}.", MyType.FullName);
If (MyType.IsPublic)
Console.WriteLine("{0} is public.", MyType.FullName);
}
}
Example3:
using System ;
using System.Reflection;
Class MyMethodInfo
{
Public void Main()
Console.WriteLine("Reflection.MethodInfo");
' 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;
if (MemberTypes.Constructor = Mymembertypes )
Console.WriteLine("MemberType is of type All");
elseIf(MemberTypes.Custom = Mymembertypes)
Console.WriteLine("MemberType is of type Custom");
elseIf (MemberTypes.Event = Mymembertypes)
Console.WriteLine("MemberType is of type Event");
elseIf ( MemberTypes.Field = Mymembertypes)
Console.WriteLine("MemberType is of type Field");
elseIf (MemberTypes.Method = Mymembertypes)
Console.WriteLine("MemberType is of type Method");
elseIf (MemberTypes.Property = Mymembertypes)
Console.WriteLine("MemberType is of type Property");
elseIf (MemberTypes.TypeInfo = Mymembertypes)
Console.WriteLine("MemberType is of type TypeInfo");
}
}
}
Practice and share your ideas and questions on reflection here.