Reflection


Unless you have a grounding in Java, reflection is probably a new topic, so I will spend a couple of pages defining it and showing how it can be used.

Reflection allows you to programmatically inspect and get information about an assembly, including all object types contained within it. This information includes the attributes you have added to those types. The reflection objects reside within the System.Reflection namespace.

In addition to reading the types defined within a given assembly, you can also generate (emit) your own assemblies and types using the services of System.Reflection.Emit or System.CodeDom. This topic is a little too hectic for a beginning book on C#, but if you are interested, then MSDN contains some information on emitting dynamic assemblies.

The first example in this section inspects an assembly and displays a list of all attributes defined on the assembly — this should produce a list similar to that shown earlier.

Note

In this chapter, I'm going to be a bit more relaxed about the format of the code examples, since if you've gotten to this point in the book you must be pretty confident of what you're doing! All of the code can be found in the Chapter27 folder of the code download — some examples in this chapter might show you only the most important parts of the code, so don't forget to look through the downloaded code to see the whole picture.

This first example can be found in the Chapter27\FindAttributes directory. The entire source file is reproduced here:

 // Import types from the System and System.Reflection assemblies using System; using System.Reflection; namespace FindAttributes { class Program { /// <summary> /// Main .exe entry point /// </summary> /// <param name="args">Command line args - the name of an assembly</param> static void Main(string[] args) { // Output usage information if necessary. if (args.Length == 0) Usage(); else if ((args.Length == 1) && (args[0] == "/?")) Usage(); else { // Load the assembly. string assemblyName = null; // Loop through the arguments passed to the console application. // I'm doing this as if you  // spaces you end up with several arguments -  // them back together again to make one filename... foreach (string arg in args) { if (assemblyName == null) assemblyName = arg; else assemblyName = string.Format("{0} {1}", assemblyName, arg); } try { // Attempt to load the named assembly. Assembly a = Assembly.LoadFrom(assemblyName); // Now find the attributes on the assembly. // The parameter is ignored, so I chose true. object[] attributes = a.GetCustomAttributes(true); // If there were any attributes defined... if (attributes.Length > 0) { Console.WriteLine("Assembly attributes for  assemblyName); // Dump them out... foreach (object o in attributes) Console.WriteLine("  {0}", o.ToString()); } else Console.WriteLine("Assembly {0} contains no Attributes.",  assemblyName); } catch (Exception ex) { Console.WriteLine("Exception thrown loading assembly {0}...",  assemblyName); Console.WriteLine(); Console.WriteLine(ex.ToString()); } } } /// <summary> /// Display usage information for the .exe. /// </summary> static void Usage() { Console.WriteLine("Usage:"); Console.WriteLine("  FindAttributes <Assembly>"); } } } 

Now, build the executable in Visual Studio 2005, or if you prefer use the command-line compiler:

>csc FindAttributes.cs

This will compile the file and produce a console executable, which you can then call.

To run the FindAttributes application, you need to supply the name of an assembly to inspect. For now, you can use the FindAttributes.exe assembly itself, which is shown in Figure 27-3.

image from book
Figure 27-3

The example code first checks the parameters passed to the command line — if none are supplied, or if the user types FindAttributes /? then the Usage() method will be called, which will display a simple command usage summary:

if (args.Length == 0)   Usage (); else if ((args.Length == 1) && (args[0] ==  "/?"))   Usage ();

Next, reconstitute the command-line arguments into a single string. The reason for this is that it's common to have spaces in directory names, such as Program Files, and the space would cause it to be considered as two arguments. So, iterate through all the arguments, stitching them back into a single string, and use this as the name of the assembly to load:

foreach (string arg in args) {   if (assemblyName == null)     assemblyName = arg;   else     assemblyName = string.Format ("{0} {1}" , assemblyName , arg); }

Then attempt to load the assembly and retrieve all custom attributes defined on that assembly with the GetCustomAttributes() method:

Assembly a = Assembly.LoadFrom (assemblyName);     // Now find the attributes on the assembly. object[] attributes = a.GetCustomAttributes(true);

Any attributes found are output to the console. When you tested the program against the FindAttributes.exe file, an attribute called DebuggableAttribute was displayed. Although you have not specified the DebuggableAttribute, it has been added by the C# compiler, and you will find that most of your executables have this attribute.

You return to reflection later in the chapter, to see how to retrieve attributes on classes and methods defined within an assembly.




Beginning Visual C# 2005
Beginning Visual C#supAND#174;/sup 2005
ISBN: B000N7ETVG
EAN: N/A
Year: 2005
Pages: 278

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