Listing the Members of a Class


Listing the Members of a Class

Reflection is all about obtaining information about classes. With the reflection classes you can find out information about all the fields, properties, constructors, methods , delegates, events and even nested types of a class. If your program has enough security, you can even list all the private members of a class. (Security is beyond the scope of this book. For more information, consult MSDN for the subject of Code Access Security.)

To obtain the members of a class:

  1. Obtain a reference to a class's System.Type object. You can do that by listing all the classes in an assembly, like you saw in the section "Enumerating Through the Classes in an Assembly." Alternatively, Chapter 7, "Types," shows several mechanisms for obtaining System.Type objects.

  2. Type foreach(System.Reflection.MemberInfo mi in currtype.GetMembers()) .

  3. Type an open curly bracket { .

  4. Type Response.Write(mi. Name + "...");

  5. Type Response.Write(mi.MemberType + "<br>");

  6. Type a close curly bracket } ( Figure 12.15 ).

    Figure 12.15 Once you have a Type object, you can ask it to give you a list of all of the type's members. You could also ask it for more specific things like, "list all the properties." This is done with GetProperties instead of GetMembers. If you want all of the type's fields, there's a GetFields function, and so on.
     //using System.Reflection; Assembly ad = Assembly.LoadFrom(@"ordersMemory.DLL"); foreach (System.Type currtype in          ad.GetTypes() ) {    HttpContext.Current.Response.Write(    "<b>" + currtype.Name + "</b><br>");    foreach(MemberInfo mi in  currtype.GetMembers()  )    {       HttpContext.Current.       Response.Write(  mi.Name  + "...");       HttpContext.Current.Response.Write(  mi.MemberType  + "<br>");    } } 

graphics/tick.gif Tips

  • The previous steps output to a Web client the names and types of each member in each class in the ordersMemory assembly. These members include by default all public members inherited from base classes and all public members declared in the class itself. The list includes both instance and static members. It does not include non-public members, or interfaces.

  • There is an overloaded version of the GetMembers() function that enables you to specify BindingFlags.BindingFlags enable you to filter the member types you want to obtain from the GetMembers() function. To list all the members of a class including non-public members, and to limit the members to those declared in the type itself (not inherited members), type the following: currtype.GetMembers(BindingFlags.Decl aredOnly BindingFlags.NonPublic BindingFlags.Static BindingFlags.Instance ) ( Figure 12.16 ).

    Figure 12.16 The code here asks for all the members that were declared in the type itself (none of the members from the base) that are private. You can only do this if you have enough security, and by default you do.
     //using System.Reflection; Assembly ad = Assembly.LoadFrom(@"ordersMemory.DLL"); foreach (System.Type currtype in          ad.GetTypes() ) {    HttpContext.Current.Response.Write(    "<b>" + currtype.Name + "</b><br>");    foreach(MemberInfo mi in  currtype.GetMembers(   BindingFlags.DeclaredOnly   BindingFlags.NonPublic   BindingFlags.Static  BindingFlags.Instance))    {       HttpContext.Current.       Response.Write(mi.Name + "...");       HttpContext.Current.Response.Write(       mi.MemberType + "<br>");    } } 



C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

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