Team Fly 

Page 214

of any programming language: looping, branching, moving and editing data, and so on, along with low-level techniques such as stack management):

 ILang.Emit(OpCodes.Ldarg_1) 'load 1 or 0 from the stack ILang.Emit(OpCodes.Ret) 

The .NET compiler translates your VB.NET source code into MSIL (Microsoft intermediate language), a sort of Esperanto that can be converted to CPU-specific native code later, prior to execution in a specific environment. The .NET CLR (Common Language Runtime) includes JIT compilers for every supported environment. MSIL is converted to native code just in time to be executed.

In addition to MSIL code, emission (like the .NET compiler) also produces associated metadata—type definitions, signatures (parameter lists) for members, and so on. MSIL code, combined with its metadata, is sent to a file—a ''portable executable" (PE) file.

Emission is, well, a rather specialized technique, to say the least. (I expect some of you may even consider it a bit twee.)

You can generate types during runtime using the Builder classes within the Emit namespace. These classes, including MethodBuilder and AssemblyBuilder, emit MSIL code.

If you are interested in code that generates code, you have to explain to people why. How would you use it? There are some security applications I can think of (whenever you move toward greater abstraction, you decrease the number of people who can figure out what you're doing). Also note that VB.NET itself uses code-generating-code in various ways. You find it in wizards, in ADO.NET (for example, to transform a DataSet into a serialized XML version), and in the text processing available via regular expressions, among other locations in the framework.

If this interests you, you can find many tools (such as the ILDasm, an MSIL Disassembler) and lots of documentation in the .NET help system and in the SDK. Look in C:\Program Files\ Microsoft Visual Studio .NET 2003\SDK\v1.1\Tool Developers Guide\docs\StartDocs.htm. You'll find extensive documentation there covering the Common Language Infrastructure (CLI), and the huge IL it contains.


In this chapter you discovered what the technique of reflection does, and how it does it. You also saw how reflection interacts with security issues and how to manage the results sent back when reflection is used against various kinds of targets.

You saw how to filter reflections and how to access loaded assemblies. Also covered was the somewhat perplexing terminology currently in use to distinguish the various kinds of types (arrays, modules, enumerations, structures, interfaces, and value types). Beyond that, another level of abstraction and categorization was discussed (the differences between the terms assembly, solution, project, module, and namespace).

Finally, you learned about discovered code, CreateInstance, and Invoke. And the idea of code emission was discussed. If you suspect that reflection might be a technique of use in your programming, this chapter's example code and discussions should have provided you with a good launching pad to further exploration of this intriguing new technique.

Team Fly 

Visual Basic  .NET Power Tools
Visual Basic .NET Power Tools
ISBN: 0782142427
EAN: 2147483647
Year: 2003
Pages: 178

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: