The .NET Framework designers separated the framework
into two parts, namely
- Common Language Runtime(CLR)
- Framework Class Library(FCL)
The CLR
handles code execution and all of the tasks associated with it: compilation,
memory management, security, thread management, and enforcement of type safety
and use. Code that runs under the CLR is referred to as managed code. This is
to distinguish it from unmanaged code that does not implement the requirements
to run in the CLR—such as COM or Windows API based components.
The Framework
Class Library is a reusable code library of types (classes, structures, and
so on) available to applications running under .NET. The FCL includes the
classes for database access, graphics, security, and both Web and Windows
forms. All languages that target the .NET Framework use this common class
library.
Common
Language Runtime
The Common Language Runtime manages the entire life
cycle of an application: it locates code, compiles it, loads associated
classes, manages its execution, and ensures automatic memory management.
Moreover, it supports cross language integration to permit code generated by
different languages to interact seamlessly.
Compiling
.NET Code
Compilers that are compliant with the CLR generate
code that is targeted for the runtime. This code, known variously as Common
Intermediate Language (CIL), Intermediate Language (IL), or Microsoft
Intermediate Language (MSIL), is an assembler-type language that is packaged in
an EXE or DLL file. These are not standard executable files and require that
the runtime's Just-in-Time (JIT) compiler convert the IL in them to a
machine-specific code when an application actually runs. Because the Common
Language Runtime is responsible for managing this IL, the code is known as
managed code. The following figure shows
the CLR functions:
The CLR does not know in which language an application
is created. Its interaction is with the
language-independent IL. So the output
of one compiler can be integrated with the code produced by a different
compiler. The JIT compiler actually
creates the machine code. The IL produced on one platform can be run on any
other platform that has its own framework and a JIT compiler that emits its own
machine code.
The compiler producing IL must emit metadata into
every code module. The metadata is a set of tables that allows each code module
to be self-descriptive. The tables contain information about the assembly
containing the code, as well as a full description of the code itself. This
information includes what types are available, the name of each type, type
members, the scope or visibility of the type, and any other type features.
Metadata has many uses:
- The JIT compiler gathers all the type information it needs for compiling directly from the metacode. For example, the JIT ensures that a method is called correctly by comparing the calling parameters with those defined in the method's metadata.
- Metadata is used in the Garbage Collection process (memory management). The garbage collector (GC) uses metadata to know when fields within an object refer to other objects so that the GC can determine what objects can and can't have their memory reclaimed.
- .NET provides a set of classes that provide the functionality to read metadata from within a program. This functionality is known collectively as reflection. It is a powerful feature that permits a program to query the code at runtime and make decisions based on its discovery. As we will see later in the book, it is the key to working with custom attributes, which are a C#-supported construct for adding custom metadata to a program.
Framework
Class Library
The Framework
Class Library (FCL) is a collection of classes and other types (enumerations,
structures, and interfaces) that are available to managed code written in any
language. The resources within the FCL
are organized into logical groupings called namespaces. For example, types used
for graphical operations are grouped into the System.Drawing and
System.Drawing.Drawing2D namespaces; types required for file I/O are members of the System.IO
namespace.
The FCL comprises hundreds of
assemblies (DLLs), and each assembly may contain multiple namespaces. In
addition, a namespace may span multiple assemblies. The following figure shows the namespaces
and types that comprise an assembly
No comments:
Post a Comment