Introduction

This is a book about getting the best out of .NET. It is based on the philosophy that the best approach to writing good, high-performance, robust applications that take full advantage of the features of .NET is to understand what's going on deep under the hood. This means that there are chapters that explore the .NET internals and in particular Common Intermediate Language (CIL), as well as chapters with a very practical basis, covering how to use specific technologies such as threading, dynamic code generation, and WMI.

This book is not a purely theoretical book for geeks, nor is it one of those purely problem-solving books that tells you how to write some code to do something without explaining how and why it works. Rather, I have sought to combine the twin aspects of practical technology-specific applications and diving under the hood of the CLR, in the belief that the true advanced .NET developer needs both. So a lot of the book is devoted to diving deep under the covers of .NET. We go way beyond the MSDN documentation in places - and generally beyond most other .NET books presently available. But we never go into some abstract feature just for the sake of it. There is always a background focus on the fact that understanding this CLR implementation detail can in some way help you to write better code. And there are a lot of chapters that show you how to write better applications in specific areas such as Windows Forms, how to take advantage of .NET features such as security, or how to better debug your code or optimize it for performance.

Just as it says on the cover, this is a book about advanced .NET programming. It's a book for people who are already familiar with the principles of writing applications targeted at the .NET Framework and who want to understand more. This will perhaps be to get an idea of exactly what the JIT compiler is looking for when it assesses whether your code is type-safe. Perhaps to be able to look at the CIL emitted by your compiler so you can understand some of the optimizations and use the knowledge to write better performing code. Or perhaps because you need to use some of the more advanced features of .NET in your code, such as dynamic code generation, or you need more information about setting up security for or for deploying your code.

The advanced nature of this book is illustrated by the fact that the very first thing we do, in Chapters 1-2, is to start learning Intermediate Language (IL). There's no way out of that if you really want to get the best from this book - we need it so we can look at how certain high-level language features of VB, C#, and C++ work under the hood.

The fact that this is an advanced book means that I won't spend any time telling you any of the basics of .NET, other than occasionally in the form of a quick background review. So for example, if you don't yet know what a JIT compiler is, or what the difference between a value and reference type is, then this is not the book for you - because I assume you already know all that that. Similarly, I assume that you are fluent in at least one high-level .NET-compliant language, such as Managed C++, Visual Basic .NET, or C#. In order to read this book you should also be comfortable with the principles of implementation-inheritance-based object-oriented programming.

As an example, one of the areas we cover in the book is assemblies. If you want to understand why Microsoft introduced the concept of the assembly, what metadata is, or how assemblies solve versioning issues and enable all the information needed to use the assembly in one place, you won't find that information in this book. There are already lots of books on the market that will give you that kind of information. Since this is an advanced book, we assume you already know all that stuff. Instead, our chapter about assemblies starts by presenting the binary format for an assembly - reviewing how the IL code and metadata is laid out in it, and how this helps with performance. Then it goes on to look at how you can extract information and metadata from the assembly programmatically, following up with how you can use assemblies and resources in your assemblies to make sure your applications are correctly localized and ready for those world-wide sales you're looking for.

What this Book Covers

Here's what the book covers, chapter by chapter:

Intermediate Language (Chapters 1-2)

The first two chapters of the book introduce you to Intermediate Language. It isn't possible to cover the whole of Intermediate Language in just two chapters, but we go over the basic concepts, including the principles of programming using the evaluation stack, declaring and instantiating value and reference types, and interacting with unmanaged code. The emphasis in the chapters is on using Intermediate Language as a tool to enhance our understanding of the CLR and to enable us to improve the C++/C#/VB code that we write. We even finish off Chapters 1-2.

Inside the CLR (Chapter 3)

This chapter examines a number of aspects of the internal workings of the CLR that are often not covered by introductory .NET texts. In particular we consider:

  • The JIT Compiler - looking at the way the JIT compiler processes your code.

  • The ECMA Standards and their relationship to the Microsoft .NET implementation.

  • Type Safety - type safety is often seen a black box. Code either is or isn't type-safe, but few sources explain many of the factors behind what makes your code pass or fail verifiability. In this section we'll work through the algorithms used to verify code, explaining how the tests work.

  • Managed and Unmanaged Code - we discuss how managed and unmanaged code can work together, and what happens at the managed/unmanaged boundary. This section should help C++ developers to understand some of the restrictions that the C++ compiler imposes on mixing managed and unmanaged code, and how to get the best out of applications that cross the boundary.

Assemblies and Localization (Chapter 4)

We will look in some detail at assemblies, considering:

  • The basic structure of an assembly, and how the metadata and IL is embedded in it. Here we focus particularly on how assemblies have been designed to assist performance. We also examine public and private assemblies.

  • The relationship between assemblies and modules.

  • Programmatic manipulation of assemblies.

  • How resources are embedded in assemblies and how to localize your applications using resources. This means we also give an overview of the support for globalization in .NET.

Garbage Collection (Chapter 5)

You are no doubt familiar with the basic principles of garbage collection in .NET. In this chapter we go into some detail about the way the garbage collector works. We examine how the garbage collection algorithm has been designed with performance in mind, and how the garbage collector interacts with the threads in your code. We also take a quick look at some more advanced topics related to garbage collection such as weak references.

Improving Performance (Chapter 6)

Performance is an important consideration when designing code, and much of the material in other chapters touches on the performance implications of the architecture of .NET. In Chapter 6, we take a chance to focus exclusively on performance, looking both at some of the ways that performance has been designed into the .NET Framework, and how you can take advantage of this to write higher-performance code. In particular we investigate:

  • The performance implications for writing managed as opposed to unmanaged code. Generally speaking, managed code does score extremely well on performance and that's going to get even better in the future. However, there are some issues that Microsoft's .NET publicity doesn't tell you, and we look at some of these.

  • JIT Optimizations. We discuss what optimizations are provided at JIT compilation time and how to control these in code.

  • Tips for improving performance when writing your code.

Profiling (Chapter 7)

Optimizing performance goes hand in hand with being able to measure the performance of your code. In Chapter 7, we cover how to profile managed code. We cover:

  • .NET-related performance counters and the PerfMon tool.

  • Writing and using custom performance counters.

  • Advanced use of the Task Manager and other profiling tools.

Dynamic Code Generation (Chapter 8)

Let's get one thing straight here: dynamic code generation is not just for compiler writers. In some situations this technique can be a useful tool for improving performance. For example, it is used extensively in the System.Text.RegularExpressions classes to give high-performance regular expression analysis. It's also quite easy to do. In this chapter, we cover the basic principles of code generation, looking at:

  • The System.CodeDom and System.CodeDom.Compiler namespaces, and how to use them to control generation and compilation of source code. We also look at the facilities available for specific language compilers.

  • The System.Reflection.Emit namespaces and dynamic generation of assemblies.

Threading (Chapter 9)

In some ways, threading has become a lot easier with managed code, since there are now classes available that implement such things as thread pools, which were important but very hard to implement with unmanaged code. In another way, however, threading has become more complicated, since there are more choices available for threading models - particularly with the easy availability of thread pools or asynchronous execution of methods. In this chapter, we discuss the options available for writing multi-threaded managed code and for controlling communication and synchronization between threads.

Management Instrumentation (Chapter 10)

.NET offers powerful facilities for interacting with both the operating system and the actual hardware that your code is running on. These facilities come through two sources: various miscellaneous framework base classes on the one hand, and the System.Management and System.Management.Instrumentation namespaces that allow you to connect .NET applications to WMI providers. Use of the various framework base classes to access information about the environment is relatively simple, so we review that area very briefly. However, WMI is a very powerful facility that is often poorly understood and therefore little used by developers. We devote the bulk of Chapter 10 to this topic. We cover in particular:

  • The concepts behind WMI - how WMI providers and consumers are architectured, and the range of tasks you can achieve using WMI, from finding out about database connections on your system to programmatically ejecting a CD.

  • How the relevant .NET base classes interact with WMI and how to code up managed applications that use WMI to find out about or even control the operation of your hardware.

Advanced Windows Forms (Chapter 11)

We've all written basic applications using Windows Forms by putting a few controls on a form, maybe even using GDI+ to perform custom drawing. This chapter takes you further on. We look at:

  • The underlying Windows Message architecture that underpins Windows Forms and Windows Forms events. We show how you can use an understanding of this architecture to access some useful events that are not directly accessible through the usual Windows Forms event mechanism.

  • More advanced windowing to impress your users, such as non-rectangular forms and owner-drawn controls.

  • Performance issues for drawing with GDI+.

Security (Chapter 12)

One of the big promises of .NET is the way that its enhanced security features will give you better control over what code is allowed to do, allowing you to be more confident about the code that you choose to download and run. In this chapter we show you how to take advantage of .NET evidence-based security. We look at:

  • How evidence-based security works.

  • How .NET security interacts with W2K/XP security.

  • Controlling your security settings using tools such as mscorcfg and caspol.

Cryptography (Chapter 13)

Cryptography is about secure communication, especially across the Internet: making sure that unauthorized users aren't able to view or tamper with your data. Microsoft has provided a rich set of classes that allow you to use cryptographic services, such as hashing, message authentication, and public-private key generation. In this chapter we examine both how these concepts work in principle, and how to use the classes in the System.Security.Cryptography and related namespaces to work with cryptography facilities.



Advanced  .NET Programming
Advanced .NET Programming
ISBN: 1861006292
EAN: 2147483647
Year: 2002
Pages: 124

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