Reflection lets you examine an assembly and discover what types the assembly defines. You can also get more information about a type at runtime by examining that type in some detail. We'll take a look at how to discover a type first. Discovering TypesYou can discover defined types by loading an assembly with the Assembly.Load method, which creates an Assembly object, and using the GetTypes method of that object, which returns an array of Type objects: public static void Main() { Assembly assembly = Assembly.Load("ch14_03"); Type[] types = assembly.GetTypes(); . . . After you have an array of Type objects, you can loop over each type and display it using a foreach loop, as you see in ch14_04.cs, Listing 14.4, where we're discovering the types defined in the ch14_03.exe assembly. Listing 14.4 Discovering Types (ch14_04.cs)using System; using System.Reflection; public class ch14_04 { public static void Main() { Assembly assembly = Assembly.Load("ch14_03"); Type[] types = assembly.GetTypes(); System.Console.WriteLine("{0} types discovered.", types.Length); foreach(Type type in types) { System.Console.WriteLine("Type = {0}", type); } } } Here's what you see when you run ch14_04.cs: C:\>ch14_04 3 types discovered. Type = Author Type = Displayer Type = ch14_03 Examining TypesYou can also use the Type.Getmembers method to get all the members of a type, which is invaluable if you're trying to discover more about a type at runtime. For example, you can see how to get all the members of the System.Console class, and their types (method, property, and so on) in ch14_05.cs, Listing 14.5. Listing 14.5 Examining Types (ch14_05.cs)using System; using System.Reflection; public class ch14_05 { public static void Main() { Type type = Type.GetType("System.Console"); MemberInfo[] memberInfos = type.GetMembers(); foreach (MemberInfo member in memberInfos) { System.Console.WriteLine("{0}: {1}", member.MemberType, member); } } } And here's what you see when you run ch14_05.cs, listing the types in System.Console : C:\>ch14_05 Method: Int32 GetHashCode() Method: Boolean Equals(System.Object) Method: System.String ToString() . . . Method: Void WriteLine(UInt64) Method: Void WriteLine(System.Object) Method: Void WriteLine(System.String) Method: Void WriteLine(System.String, System.Object) . . . Property: System.IO.TextWriter Error Property: System.IO.TextReader In Property: System.IO.TextWriter Out Note that you get not only the type of members this way, such as Method or Property , but you get the argument types you pass to methods and their return types as well. If you want to see only the methods of a type, you can call type.GetMethods instead of type.GetMembers . Some assemblies have hundreds of members, and you can use the Type.FindMembers method to filter only the ones you want. For example, you can find all members beginning with "Write" in System.Console using this method. Here's how you use this method formally : public virtual MemberInfo[] FindMembers( MemberTypes memberType , BindingFlags bindingAttr , MemberFilter filter , object filterCriteria ) Here are the parameters you pass to this method:
Here are the members of the BindingsFlag enumeration you can OR together for the bindingAttr parameter:
For example, you can see how to find all public, static methods of System.Console that start with "Write" in ch14_06.cs, Listing 14.6. Note in particular that we're using the Type.FilterName predefined delegate to indicate that we're going to supply a filter name, which members much match; in this example that filter name is "Write*" . Listing 14.6 Filtering Members (ch14_06.cs)using System; using System.Reflection; public class ch14_05 { public static void Main() { Type type = Type.GetType("System.Console"); MemberInfo[] memberInfos = type.FindMembers(MemberTypes.Method, BindingFlags.Public BindingFlags.Static, Type.FilterName, "Write*"); foreach (MemberInfo member in memberInfos ) { Console.WriteLine("{0}: {1}", member.MemberType, member); } } } Here's what you see when you run ch14_06, listing all the members that begin with "Write" in System.Console : C:\>ch14_06 Method: Void WriteLine() Method: Void WriteLine(Boolean) Method: Void WriteLine(Char) Method: Void WriteLine(Char[]) Method: Void WriteLine(Char[], Int32, Int32) Method: Void WriteLine(System.Decimal) Method: Void WriteLine(Double) Method: Void WriteLine(Single) Method: Void WriteLine(Int32) Method: Void WriteLine(UInt32) Method: Void WriteLine(Int64) Method: Void WriteLine(UInt64) Method: Void WriteLine(System.Object) Method: Void WriteLine(System.String) Method: Void WriteLine(System.String, System.Object) Method: Void WriteLine(System.String, System.Object, System.Object) Method: Void WriteLine(System.String, System.Object, System.Object, System.Object) Method: Void WriteLine(System.String, System.Object, System.Object, System.Object, System.Object, ...) Method: Void WriteLine(System.String, System.Object[]) Method: Void Write(System.String, System.Object) Method: Void Write(System.String, System.Object, System.Object) Method: Void Write(System.String, System.Object, System.Object, System.Object) Method: Void Write(System.String, System.Object, System.Object, System.Object, System.Object, ...) Method: Void Write(System.String, System.Object[]) Method: Void Write(Boolean) Method: Void Write(Char) Method: Void Write(Char[]) Method: Void Write(Char[], Int32, Int32) Method: Void Write(Double) Method: Void Write(System.Decimal) Method: Void Write(Single) Method: Void Write(Int32) Method: Void Write(UInt32) Method: Void Write(Int64) Method: Void Write(UInt64) Method: Void Write(System.Object) Method: Void Write(System.String) |