13.5 Reflecting Over a Type Hierarchy

Once you have retrieved a Type instance, you can navigate the application hierarchy described earlier, accessing the metadata via types that represent members , modules, assemblies, namespaces, AppDomain s, and nested types. You can also inspect the metadata and any custom attributes, create new instances of the types, and invoke members.

Here is an example that uses reflection to display the members in three different types:

 using System; using System.Reflection; class Test {   static void Main( ) {     object o = new Object( );     DumpTypeInfo(o.GetType( ));     DumpTypeInfo(typeof(int));     DumpTypeInfo(Type.GetType("System.String"));   }   static void DumpTypeInfo(Type t) {     Console.WriteLine("Type: {0}", t);        // Retrieve the list of members in the type     MemberInfo[ ] miarr = t.GetMembers( );        // Print out details on each of them     foreach (MemberInfo mi in miarr)       Console.WriteLine("  {0}={1}", mi.MemberType, mi);   } } 


C# in a Nutshell
C # in a Nutshell, Second Edition
ISBN: 0596005261
EAN: 2147483647
Year: 2005
Pages: 963

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net