BindingFlags is used to declare how to fetch information from a type.
Common BindingFlags members:
1- Instance
2- Static
3- Public
4- NonPublic
5- IgnoreCase
There are many Methods available in a type to fetch Info:
1- GetConstructor
2- GetConstructors
3- GetField
4- GetFields
5- GetMethod
6- GetMethods
7- GetEvent
8- GetEvents
9- GetProperty
10- GetProperties
Example:
Imports System Imports System.IO
Imports System.Reflection
Class ListMembers
{
Public void Main()
{
' Specifies the class.
Dim t As Type = GetType(System.IO.BufferedStream);
Console.WriteLine("Listing all the members (public and non public) of the {0} type", t);
' Lists static fields first.
FieldInfo[] fi = t.GetFields((BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public));
Console.WriteLine("// Static Fields");
PrintMembers(fi);
' Static properties.
PropertyInfo[] pi = t.GetProperties((BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public));
Console.WriteLine("// Static Properties");
PrintMembers(pi);
' Static events.
EventInfo[] ei = t.GetEvents((BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public));
Console.WriteLine("// Static Events");
PrintMembers(ei);
' Static methods.
MethodInfo[] mi = t.GetMethods((BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public));
Console.WriteLine("// Static Methods");
PrintMembers(mi);
ConstructorInfo[] ci = t.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public));
Console.WriteLine("// Constructors");
PrintMembers(ci);
fi = t.GetFields((BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public));
Console.WriteLine("// Instance Fields");
PrintMembers(fi);
pi = t.GetProperties((BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public));
Console.WriteLine("// Instance Properties");
PrintMembers(pi);
ei = t.GetEvents((BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public));
Console.WriteLine("// Instance Events");
PrintMembers(ei);
mi = t.GetMethods((BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public));
Console.WriteLine("// Instance Methods");
PrintMembers(mi);
Console.WriteLine(ControlChars.CrLf & "Press ENTER to exit.");
Console.Read();
}
Public void PrintMembers(MemberInfo[] ms)
{
MemberInfo m ;
foreach (m in ms)
Console.WriteLine("{0}{1}", " ", m);
Console.WriteLine();
}
}
Design Patterns Used by Reflection classes
in Reflection there are some patterns we use to fetch and filter lists of Type
1- FindXXX()
2- GetXXX(<parameters>)
3- GetXXXs()
4- GetXXXs(<parameters>)