| for RuBoard |
Even if .NET fixed all the problems of the past, it would not be enough. One of the unchanging facts of programming life is that the boundaries of customer demand are always being expanded.
The growth of the Internet has made it imperative that applications work seamlessly across network connections. Components have to be able to expose their functionality to other machines. Programmers do not want to write the underlying plumbing code, they want to solve their customers' problems.
| for RuBoard |
| for RuBoard |
To solve all these problems .NET must provide an underlying set of services that is available to all languages at all times. It also has to understand enough about an application to be able to provide these services.
Serialization provides a simple example. Every programmer at some time or another has to write code to save data. Why should every programmer have to reinvent the wheel of how to persist nested objects and complicated data structures? Why should every programmer have to figure out how to do this for a variety of data stores? .NET can do this for the programmer. Programmers can also decide to do it
To see how this is done, look at the
Serialize
sample associated with this chapter. For the moment ignore the programming details of C# which will be covered in the
[Serializable] class Customer
{
public string name;
public long id;
}
class Test
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
Customer cust = new Customer();
cust.name = "Charles Darwin";
cust.id = 10;
list.Add(cust);
cust = new Customer();
cust.name = "Isaac Newton";
cust.id = 20;
list.Add(cust);
foreach (Customer x in list)
Console.WriteLine(x.name + ": " + x.id);
Console.WriteLine("Saving Customer List");
FileStream s = new FileStream("cust.txt",
FileMode.Create);
SoapFormatter f = new SoapFormatter();
f.Serialize(s, list);
s.Close();
Console.WriteLine("Restoring to New List");
s = new FileStream("cust.txt", FileMode.Open);
f = new SoapFormatter();
ArrayList list2 = (ArrayList)f.Deserialize(s);
s.Close();
foreach (Customer y in list2)
Console.WriteLine(y.name + ": " + y.id);
}
}
We have defined a Customer class with two fields: a name and an id . The program first creates an instance of a collection class that will be used to hold instances of the Customer class. We add two Customer objects to the collection and then print out the contents of the collection. The collection is then saved to disk. It is restored to a new collection instance and printed out. The results printed out will be identical to those printed out before the collection was saved. [1]
[1] The sample installation should have already built an instance that you can run. If not, double-click on the Visual Studio.NET solution file that has the .sln suffix. When Visual Studio comes up, hit Control-F5 to build and run the sample.
We wrote no code to
The Customer class was annotated with the Serializable attribute in the same way the public attribute annotates the name field. If you do not want your objects to be serializable, do not apply the attribute to your class. If an attempt is then made to save your object, an exception will be thrown and the program will fail. [2]
[2] Comment out the Serializable attribute in the program (you can use the C/C++/* */ comment syntax) and see what happens.
Attribute-based programming is used extensively throughout .NET to describe how the Framework should treat code and data. With attributes you do not have to write any code; the Framework takes the appropriate action based on the attribute. Security can be set through attributes. You can use attributes to have the Framework handle multithreading synchronization. Remoting of objects becomes straightforward through the use of attributes.
The compiler adds this Serializable attribute to the metadata of the Customer class to indicate that the Framework should save and restore the object. Metadata is additional information about the code and data within a .NET application. Metadata, a feature of the Common Language Runtime, provides such information about the code as:
Version and locale information
All the types
Details about each type, including name, visibility, and so on
Details about the
Attributes
Since metadata is stored in a programming-language-independent fashion with the code, not in a central store such as the Windows Registry, it makes .NET applications
In our example, the Framework can query the metadata to discover the structure of the Customer object in order to be able to save and restore it.
TypesTypes are at the heart of the programming model for the CLR. A type is analogous to a class in most object-oriented programming languages, providing an abstraction of data and behavior, grouped together. A type in the CLR contains:
There are also built-in primitive types, such as integer and floating point numeric types, string, etc. We will discuss types under the guise of classes and value types when we cover C#. |
The Formatter and FileStream classes are just two of more than 2500 classes in the .NET Framework that provide plumbing and system services for .NET applications. Some of the functionality provided by the .NET Framework includes:
Base class library (basic functionality such as strings, arrays, and formatting)
Networking
Security
Remoting
Diagnostics
I/O
Database
XML
Web services that allow us to expose component interfaces over the Internet
Web programming
Windows
Suppose you want to encrypt your data and therefore do not want to rely on the Framework's serialization. Your class can inherit from the ISerializable interface and provide the appropriate implementation. (We will discuss how to do this in a later chapter.) The Framework will then use your methods to save and restore the data.
How does the Framework know that you implemented the
ISerializable
interface? It can query the metadata
Interface-based programming is used in .NET to allow your objects to provide
So if a type has metadata, the runtime can do all kinds of wonderful things. But does everything in .NET have metadata? Yes! Every type, whether it is user defined (such as Customer ) or part of the Framework (such as FileStream ), is a .NET object. All .NET objects have the same base class, the system's Object class. Hence everything that runs in .NET has a type and therefore has metadata.
In our example, the serialization code can walk through the ArrayList of customer objects and save each one as well as the array it belongs to, because the metadata allows it to understand the object's type and its logical structure.
The .NET Framework has to make some assumptions about the nature of the types that will be passed to it. These assumptions are the Common Type System (CTS). The CTS defines the rules for the types and operations that the Common Language Runtime will support. It is the CTS that limits .NET classes to single implementation inheritance. Since the CTS is defined for a wide range of languages, not all languages need to support all features of the CTS.
The CTS makes it possible to guarantee type safety, which is critical for writing reliable and secure code. As we noted in the previous section, every object has a type and therefore every reference to an object points to a defined memory layout. If arbitrary pointer operations are not allowed, the only way to access an object is through its public methods and fields. Hence it's possible to verify an object's safety by analyzing the object. There is no need to know or analyze all the users of a class.
How are the rules of the CTS enforced? The Microsoft Intermediate Language (MSIL or IL) defines an instruction set that is used by all .NET compilers. This intermediate language is platform independent. The MSIL code can later be converted to a platform's native code. Verification for type safety can be done once based on the MSIL; it need not be done for every platform. Since everything is defined in terms of MSIL, we can be sure that the .NET Framework classes will work with all .NET languages. Design no longer dictates language choice; language choice no longer constrains design.
MSIL and the CTS make it possible for multiple languages to use the .NET Framework since their compilers produce MSIL. This one of the most visible differences between .NET and Java, which in fact share a great deal in philosophy.
The Microsoft Intermediate Language Disassembler (ILDASM) can display the metadata and MSIL instructions associated with .NET code. It is a very useful tool both for debugging and for increasing your understanding of the .NET infrastructure. You can use ILDASM to examine the .NET Framework code itself. [3] Figure 2-1 shows a fragment of the MSIL code from the Serialize example, where we create two new customer objects and add them to the list. [4] The newobj instruction creates a new object reference using the constructor parameter. [5] Stloc stores the value in a local variable. Ldloc loads a local variable. [6] It is strongly recommended that you play with ILDASM and learn its features.
[3] ILDASM is installed on the Tools menu in Visual Studio.NET. It is also found in the Microsoft.NET\FrameworkSDK\Bin subdirectory. You can invoke it by double-clicking on its Explorer entry or from the command line. If you invoke it from the command line (or from VS.NET) you can use the /ADV switch to get some advanced options.
[4] Open Serialize.exe and Click on the plus (+) sign next to Test. Double-click on Main to bring up the MSIL for the Main routine.
[5] Technically it is not a parameter. IL is a stack-based language, and the constructor is a metadata token previously
pushed on the stack.
[6] You can read all about MSIL in the ECMA documents,
specifically the Partition III CIL Instruction Set.
Having all language compilers use a common intermediate language and common base class make it
possible
for languages to
The
Common Language Specification
(CLS) defines a subset of the CTS representing the basic functionality that all .NET languages should implement if they are to interoperate with each other. This specification enables a class written in Visual Basic.NET to inherit from a class written in COBOL.NET or C#, or to make interlanguage debugging possible. An example of a CLS rule is that method calls need not support a variable number of arguments, even though such a construct can be
CLS compliance applies only to
Microsoft itself is providing several CLS-compliant languages: C#, Visual Basic.NET, and C++ with Managed Extensions. Third parties are providing additional languages (there are over a
In the serialization example a second instance of the Customer object was assigned to the same variable (
cust
) as the first instance without freeing it. None of the allocated storage in the example was ever deallocated. .NET uses automatic garbage collection to
By having automatic memory management the system has eliminated memory leakage, which is one of the most common programming errors. In most cases, memory allocation is much faster with garbage collection than with classic heap allocation schemes. Note that
Garbage collection is one of several services provided by the Common Language Runtime (CLR) to .NET programs. [7] Data that is under the control of the CLR garbage collection process is called managed data. Managed code is code that can use the services of the CLR. .NET compilers that produce MSIL can produce managed code.
[7] Technically, metadata, the CTS, the CLS, and the Virtual Execution System (VES) are also part of the CLR. We are using CLR here in the sense that it is commonly used. The VES loads and runs .NET programs and supports late binding. For more details refer to the Common Language Infrastructure (CLI) Partition I: Concepts and Architecture document submitted to ECMA. This document is loaded with the .NET Framework SDK.
Managed code is not automatically type safe. C++ provides the classic example. You can use the __gc attribute to make a class garbage collected. The C++ compiler will prevent such classes from using pointer arithmetic. Nonetheless, C++ cannot be reliably
[8] The most immediate reason for this is that the C Runtime Library (CRT) that is the start-up code for C++ programs was not converted to run under .NET because of time constraints. Even if this were to be done, however, there are two other obstacles to verifying C++ code. First, to ensure that the verification process can complete in a reasonable amount of time, the CLR language specifications require certain IL language patterns to be used and the managed C++ compiler would have to be changed to accommodate this. Second, after disallowing the C++ constructs that inhibit verification (like taking the address of a variable on the stack, or pointer arithmetic), you would wind up with a close
approximation to the C# language.
Code is typically verified for type safety before compilation. This step is optional and can be
[9] It would not be correct to say that code written in MSIL is managed code. The CTS
permits MSIL to have unmanaged pointers in order to work with unmanaged data in legacy code. The reverse is not true; unmanaged code cannot access managed data. The CLS prohibits unmanaged pointers.
Type safe code cannot be subverted. A buffer overwrite is not able to corrupt other data structures or programs. Methods can only start and end at
[10] This is discussed in more detail in Chapter 12.
[11] See the discussion of Application Domains in Chapter 8.
Another function of the CLR is to load and run .NET programs.
.NET programs are deployed as assemblies. An assembly is one or more EXEs or DLLs with associated metadata information. The metadata about the entire assembly is stored in the assembly's manifest. The manifest contains, for example, a list of the assemblies upon which this assembly is dependent.
In our Serialize example there is only file in the assembly, serialize.exe . That file contains the metadata as well as the code. Since the manifest is stored in the assembly and not in a separate file (like a type library or registry), the manifest cannot get out of sync with the assembly. Figure 2-2 shows the metadata in the manifest for this example. [12] Note the assembly extern statements that indicate the dependencies on the Framework assemblies mscorlib and System.Runtime.Formatters.SOAP . These statements also indicate the version of those assemblies that serialize.exe depends on.
[12] Open serialize.exe in ILDASM and double-click on the MANIFEST item.
Assemblies can be versioned, and the version is part of the name for the assembly. To version an assembly it needs a unique name. Public/private encryption keys are used to generate a unique (or strong) name.
Assemblies can be deployed either privately or publicly. For private deployment all the assemblies that an application needs are
[13] This is discussed in much more detail in Chapter 7.
Assembly deployment with language interoperability makes component development almost effortless.
Before executing on the target machine, MSIL has to be translated into the machine's native code. This can either be done before the application is called, or at runtime. At runtime, the translation is done by a just-in-time (JIT) compiler. The Native Image Generator (Ngen.exe)
The advantage of pretranslation is that optimizations can be performed. Optimizations are
The advantage of JIT is that it
In the first release of .NET, the Native Image Generator and the JIT compiler use the same compiler. No optimizations are done for Ngen, its only current advantage is faster start-up. For this reason we do not discuss Ngen in this book.
You may like the safety and ease-of-use features of managed code but you might be
The CLR is designed with high performance in mind. With JIT compilation, the first time a method is
You do pay a penalty when security checks have to be made that require a stack walk as we will explain in the Security chapter.
Web pages use compiled code, not interpreted code. As a result ASP.NET is much faster than ASP.
For 98% of the code that programmers write, any small loss in performance is far outweighed by the gains in reliability and ease of development. High performance server applications might have to use technologies such as ATL Server and C++.
| for RuBoard |