- •Table of Contents
- •C# and the .NET Platform, Second Edition
- •Introduction
- •Part One: Introducing C# and the .NET Platform
- •Part Two: The C# Programming Language
- •Part Three: Programming with .NET Assemblies
- •Part Four: Leveraging the .NET Libraries
- •Part Five: Web Applications and XML Web Services
- •Obtaining This Book's Source Code
- •The .NET Solution
- •What C# Brings to the Table
- •The Role of the Assembly Manifest
- •Summary
- •Chapter 2: Building C# Applications
- •Summary
- •Chapter 3: C# Language Fundamentals
- •Defining Program Constants
- •Defining Custom Class Methods
- •C# Enumerations
- •Summary
- •The Second Pillar: C#'s Inheritance Support
- •Summary
- •Catching Exceptions
- •Finalizing a Type
- •Garbage Collection Optimizations
- •Summary
- •Chapter 6: Interfaces and Collections
- •Building Comparable Objects (IComparable)
- •Summary
- •Summary
- •Internal Representation of Type Indexers
- •Summary
- •An Overview of .NET Assemblies
- •Understanding Delayed Signing
- •Using a Shared Assembly
- •GAC Internals
- •Summary
- •Spawning Secondary Threads
- •A More Elaborate Threading Example
- •Summary
- •Summary
- •Object Persistence in the .NET Framework
- •The .NET Remoting Namespaces
- •Understanding the .NET Remoting Framework
- •All Together Now!
- •Terms of the .NET Remoting Trade
- •Testing the Remoting Application
- •Revisiting the Activation Mode of WKO Types
- •Deploying the Server to a Remote Machine
- •Summary
- •Control Events
- •The Form Class
- •Summary
- •Regarding the Disposal of System.Drawing Types
- •Understanding the Graphics Class
- •Summary
- •The TextBox Control
- •Working with Panel Controls
- •Configuring a Control's Anchoring Behavior
- •Summary
- •Chapter 16: The System.IO Namespace
- •The Static Members of the Directory Class
- •The Abstract Stream Class
- •Summary
- •The Role of ADO.NET Data Providers
- •The Types of System.Data
- •Selecting a Data Provider
- •The Types of the System.Data.OleDb Namespace
- •Working with the OleDbDataReader
- •Summary
- •Submitting the Form Data (GET and POST)
- •Some Benefits of ASP.NET
- •Creating an ASP.NET Web Application by Hand
- •The Composition of an ASP.NET Page
- •The Derivation of an ASP.NET Page
The .NET SolutionC# and the .NET Platform, Second Edition
by Andrew Troelsen |
ISBN:1590590554 |
So much for the brief history lesson. The bottom line is that life as a Windows programmer has been
Apress © 2003 (1200 pages)
tough. The .NET Framework is a rather radical and brute-force approach to making our lives easier. The
This comprehensive text starts with a brief overview of the
solution proposedC#bylanguage.NET is "Changeand then quicklyeverythingmovesfromto herek y technicalon out" (sorry,and you can't blame the messenger for the message)architectural. As you will iseesuesduringfor .NETthe dremaindervelopers.of this book, the .NET Framework is a completely new model for building systems on the Windows family of operating systems, and additional (nonMicrosoft) operating systems now and in the future. To set the stage, here is a quick rundown of some
Table of Contents
core features provided courtesy of .NET:
C# and the .NET Platform, Second Edition
Full interoperability with existing Win32 code. This is (of course) a good thing. Existing COM binaries
Introduction
can commingle (i.e., interop) with newer .NET binaries and vice versa. Also, PInvoke (Platform
Part One - Introducing C# and the .NET Platform
Invocation) allows you to invoke invoke raw C-based functions (such as the Win32 API) from
Chapter 1 - The Philosophy of .NET
managed code.
Chapter 2 - Building C# Applications
Part Two - The C# Programming Language
Complete and total language integration. Unlike classic COM, .NET supports cross-language
Chapter 3 - C# Language Fundamentals
inheritance, cross-language exception handling, and crosslanguage debugging.
Chapter 4 - Object-Oriented Programming with C#
ChapterAcommon5 - ExceptionsruntimeandengineObjectsharedLifetimeby all .NET-aware languages. One aspect of this engine is a well-
Chapterdefined6 - Interfacesset of typesandthatCollectionseach .NET-aware language "understands."
Chapter 7 - Callback Interfaces, Delegates, and Events
Abase class library that provides shelter from the complexities of raw API calls, and offers a
Chapter 8 - Advanced C# Type Construction Techniques
consistent object model used by all .NET-aware languages.
Part Three - Programming with .NET Assemblies
Chapter 9 - Understanding .NET Assemblies
No more COM plumbing! IClassFactory, IUnknown, IDispatch, IDL code, and the evil VARIANT-
Chapter 10 - Processes, AppDomains, Contexts, and Threads
compliant data types (BSTR, SAFEARRAY, and so forth) have no place in a native .NET binary.
Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming
Part FourAtruly- Leveragingsimplified deploymentthe .NET Librariesmodel. Under .NET, there is no need to register a binary unit into the
system registry. Furthermore, the .NET runtime allows multiple versions of the same *.dll to exist in
Chapter 12 - Object Serialization and the . Remoting Layer
harmony on a single machine (using an approach termed "side-by-side execution").
Chapter 13 - Building a Better Window (Introducing Windows Forms) Chapter 14 - A Better Painting Framework (GDI+)
Chapter 15 - Programming with Windows Forms Controls
Chapter 16 - The System.IO Namespace
Chapter 17 - Data Access with ADO.NET
Part Five - Web Applications and XML Web Services
Chapter 18 - ASP.NET Web Pages and Web Controls
Chapter 19 - ASP.NET Web Applications
Chapter 20 - XML Web Services
Index
List of Figures
List of Tables
The BuildingC# Blocksand the .NETofPlatform,the .NETSecondPlatformEditi n (CLR, CTS, and CLS)
by Andrew Troelsen |
ISBN:1590590554 |
Now that you have been given a peek into some of the benefits provided by .NET, let's preview three key
Apress © 2003 (1200 pages)
(and interrelated) entities that make it all possible: the CLR, CTS, and CLS. From a programmer's point of
This comprehensive text starts with a brief overview of the
view, .NET can beC#simplylanguageunderstoodand thenasquicklya newmovesruntimeto keyenvironmenttechnical andand a common base class library. The runtime layerarchitectuis properlyal referredissues forto.NETas thed veloperscommon. language runtime, or CLR. The primary role of the CLR is to locate, load, and manage .NET types on your behalf. The CLR also takes care of a number of low-level details such as automatic memory management, language integration, and ensuring type
Table of Contents safety.
C# and the .NET Platform, Second Edition
Another building block of the .NET platform is the Common Type System, or CTS. The CTS fully
Introduction
describes all possible data types and programming constructs supported by the runtime, specifies how
Part One - Introducing C# and the .NET Platform
these entities can interact with each other and details how they are represented in the .NET metadata
Chapter 1 - The Philosophy of .NET
format (more information on "metadata" later in this chapter).
Chapter 2 - Building C# Applications
Part Two - The C# Programming Language
Understand that a given .NET-aware language might not support each and every entity defined by the
Chapter 3 - C# Language Fundamentals
CTS. The Common Language Specification (CLS) is a set of rules that define a subset of common types
Chapter 4 - Object-Oriented Programming with C#
and programming constructs that all .NET programming languages can agree on. Thus, if you build .NET
Chapter 5 - Exceptions and Object Lifetime
types that only expose CLS-compliant features, you can rest assured that all .NET-aware languages could
Chapter 6 - Interfaces and Collections
make use of your types. Conversely, if you make use of a data type or programming construct that is
Chapter 7 - Callback Interfaces, Delegates, and Events
outside of the CLS, you cannot guarantee that every .NET programming language can interact with your
Chapterbinary code8 - Advlibrarynced. C# Type Construction Techniques
Part Three - Programming with .NET Assemblies
Chapter 9 - Understanding .NET Assemblies
Chapter 10 - Processes, AppDomains, Contexts, and Threads
Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming
Part Four - Leveraging the .NET Libraries
Chapter 12 - Object Serialization and the .NET Remoting Layer
Chapter 13 - Building a Better Window (Introducing Windows Forms)
Chapter 14 - A Better Painting Framework (GDI+)
Chapter 15 - Programming with Windows Forms Controls
Chapter 16 - The System.IO Namespace
Chapter 17 - Data Access with ADO.NET
Part Five - Web Applications and XML Web Services
Chapter 18 - ASP.NET Web Pages and Web Controls
Chapter 19 - ASP.NET Web Applications
Chapter 20 - XML Web Services
Index
List of Figures
List of Tables
The Role ofC#theand.NETthe .NETBasePlatform,ClassSecondLibrariesEdition
by Andrew Troelsen |
ISBN:1590590554 |
In addition to the CLR and CTS/CLS specifications, the .NET platform provides a base class library that is
Apress © 2003 (1200 pages)
available to all .NET programming languages. Not only does this base class library encapsulate various
This comprehensive text starts with a brief overview of the
primitives such asC#threads,languagefileandIO,thengraphicalquicklyrenderingmoves toandkeyinteractiontechnical andwith various hardware devices, but it also provides supportarchitecturalfor a numberissu s offorservices.NET devrequiredlopers. by most real-world applications.
For example, the base class libraries define types that facilitate database manipulation, XML integration,
Tableprogrammaticof Contentssecurity, and the construction of Web-enabled (as well as traditional desktop and console- C#based)and thefront.NETendsPlatform,. From Sea conceptuald Editionpoint of view, you can visualize the relationship between the .NET
runtime layer and the corresponding base class library as shown in Figure 1-1.
Introduction
Part One - Introducing C# and the .NET Platform
Chapter
Chapter
Part
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Part
Chapter 9 - Understanding .NET Assemblies
Figure 1-1: The CLR, CTS, CLS base class library relationship
Chapter 10 - Processes, AppDomains, Contexts, and Threads
Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming
Part Four - Leveraging the .NET Libraries
Chapter 12 - Object Serialization and the .NET Remoting Layer
Chapter 13 - Building a Better Window (Introducing Windows Forms)
Chapter 14 - A Better Painting Framework (GDI+)
Chapter 15 - Programming with Windows Forms Controls
Chapter 16 - The System.IO Namespace
Chapter 17 - Data Access with ADO.NET
Part Five - Web Applications and XML Web Services
Chapter 18 - ASP.NET Web Pages and Web Controls
Chapter 19 - ASP.NET Web Applications
Chapter 20 - XML Web Services
Index
List of Figures
List of Tables
What C# Brings to the Table |
|
C# and he .NET Pl tform, Second Edition |
|
by Andrew Troelsen |
ISBN:1590590554 |
Given that .NET isApresssuch©a2003radical(1200departurepages) |
from the current thoughts of the day, Microsoft has developed |
a new programming language (C#) specifically for this new platform. C# is a programming language that
This comprehensive text starts with a brief overview of the
looksvery similarC#(butlanotguageidentical)and thento thequicklysyntaxmovesof Javato key. Fortechnicalexample,andlike Java, a C# class definition is contained within architecturalsingle-sourceissuescodeforfile.NET(*.cs)developratherrsthan. the C++-centric view of splitting a class definition into discrete header (*.h) and implementation (*.cpp) files. However, to call C# a Java rip-off is inaccurate. Both C# and Java are based on the syntactical constructs of C++. Just as Java is in many
Table of Contents
ways a cleaned-up version of C++, C# can be viewed as a cleaned-up version of Java—after all, they are
C# aind the .NET Platform, Second Edition all the same family of languages.
Introduction
The truth of the matter is that many of C#'s syntactic constructs are modeled after various aspects of
Part One - Introducing C# and the .NET Platform
Visual Basic and C++ itself. For example, like Visual Basic, C# supports the notion of formal class
Chapter 1 - The Philosophy of .NET
properties as well the declaration of methods that take a varying number of arguments (via parameter
Chapter 2 - Building C# Applications
arrays). Like C++, C# allows you to overload operators on your custom types, as well as create structures
Part Two - The C# Programming Language
(value types) and enumerations (as you may know, Java lacks all of these features).
Chapter 3 - C# Language Fundamentals
Chapter 4 - Object-Oriented Programming with C#
Given that C# is a hybrid of numerous languages, the result is a product that is as syntactically clean (if not
Chapter 5 - Exceptions and Object Lifetime
cleaner) than Java, just about as simple as Visual Basic 6.0, and provides just about as much power and
Chapter 6 - Interfaces and Collections
flexibility as C++ (without the associated ugly bits). In a nutshell, the C# languages offers the following
Chapter 7 - Callback Interfaces, Delegates, and Events
features (many of which are shared by other .NET-aware programming languages):
Chapter 8 - Advanced C# Type Construction Techniques
No pointers required! C# programs typically have no need for direct pointer manipulation (although
Part Three - Programming with .NET Assemblies
you are free to drop down to that level if you desire, as seen in Chapter 8).
Chapter 9 - Understanding .NET Assemblies
Chapter 10 - Processes, AppDomains, Contexts, and Threads
Automatic memory management. Given this, C# does not support a "delete" keyword.
Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming
Part FourFormal- Leveragingsyntactic theconstructs.NET Librariesfor enumerations, structures, and class properties.
Chapter 12 - Object Serialization and the .NET Remoting Layer
The C++-like ability to overload operators for a custom type, without the complexity (i.e., making sure
Chapter 13 - Building a Better Window (Introducing Windows Forms)
to "return *this to allow chaining" is not your problem).
Chapter 14 - A Better Painting Framework (GDI+)
Chapter 15 - Programming with Windows Forms Controls
Full support for interface-based programming techniques. However, unlike classic COM, the interface
Chapter 16 - The System.IO Namespace
isnot the only way to manipulate types between binaries. .NET supports true object references that
Chapter 17 - Data Access with ADO.NET
can be passed between boundaries (by reference or by value).
Part Five - Web Applications and XML Web Services
ChapterFull18support- ASP.NETfor aspectWeb Pages-basedandprogrammingWeb Controlstechniques via attributes. This brand of development Chapterallows19 -youASPto.NETassignWebcharacteristicsApplications to types and their members (much like COM IDL) to further qualify
the behavior of a given entity.
Chapter 20 - XML Web Services
Index
Perhaps the most important point to understand about the C# language shipped with the .NET platform is
List of Figures
that it can only produce code that can execute within the .NET runtime (you could never use C# to build a
List of Tables
classic COM server or a traditional Win32 API application). Officially speaking, the term used to describe the code targeting the .NET runtime is managed code. The binary unit that contains the managed code is termed an assembly (more details in just a bit).
Additional .C#NETand-theAware.NET PlatfProgrammingrm, Second EditionLanguages
by Andrew Troelsen |
ISBN:1590590554 |
On a related language-centric note, understand that C# is not the only language targeting the .NET
Apress © 2003 (1200 pages)
platform. When the .NET platform was first revealed to the general public during the 2000 Professional
This comprehensive text starts with a brief overview of the
Developers ConferenceC# language(PDC),andseveralth n quicklyvendorsmovesannouncedto key technicalthey wereandbusy building .NET-aware versions of their respectivearchitectucompilersal. Atissuesthe fortime.NETof thisdeveloperswriting,.dozens of different languages are slated to undergo (or have undergone) .NET enlightenment. In addition to the five languages that ship with Visual Studio .NET Professional (C#, J#, Visual Basic .NET, "Managed C++," and JScript .NET), be on the
Table of Contents
lookout for .NET-aware compilers targeting Smalltalk (S#), COBOL, Pascal, Python, and Perl (as well as
C#manyd the .NET Platform, Second Edition
others).
Introduction
Although this book focuses (almost) exclusively on C#, Table 1-1 lists a number of .NET-enabled
Part One - Introducing C# and the .NET Platform
programming languages and where to learn more about them (do note that the exact URLs are subject to
Chapter 1 - The Philosophy of .NET
|
change). |
|
|
|
||
Chapter 2 - Building C# Applications |
|
|
|
|||
Part Two - The C# Programming Language |
|
|
|
|||
|
Table 1-1: A Sampling of .NET-Aware Programming Languages |
|
|
|
||
|
Chapter 3 - C# Language Fundamentals |
|
|
|
||
|
|
|
|
|
|
|
|
|
Chapter 4 |
- Object-Oriented Programming with C# |
|
Meaning in Life |
|
|
|
.NET-Centric Web Link |
|
|
||
|
|
Chapter 5 |
- Exceptions and Object Lifetime |
|
|
|
|
Chapterhttp://www6 - Interfaces.oberonand.ethzCollections.ch |
|
Homepage for |
|
||
|
Chapter 7 |
- Callback Interfaces, Delegates, and Events |
|
Active Oberon .NET |
|
|
|
|
|
|
|
||
|
|
Chapter 8 |
- Advanced C# Type Construction Techniques |
|
Homepage for |
|
|
|
http://www.dyadic.com |
|
|
||
Part Three - Programming with .NET Assemblies |
|
Dyalog APL .NET |
|
|||
|
|
|
|
|
|
Chapter 9 - Understanding .NET Assemblies
http://www.adtools.com
Chapter 10 - Processes, AppDomains, Contexts, and Threads
Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming
Parthttp://wwwFour - Leveraging.eiffelthe .NETcomLibraries
Chapter 12 - Object Serialization and the .NET Remoting Layer
Chapter 13 - Building a Better Window (Introducing Windows Forms) http://research.microsoft.com/projects/ilx/fsharp.htm
Chapter 14 - A Better Painting Framework (GDI+) Chapter 15 - Programming with Windows Forms Controls Chapterhttp://lahey16 - The System.com.IO Namespace
Chapter 17 - Data Access with ADO.NET
For those interested in COBOL .NET
For those interested in Eiffel .NET
Details of the F# language
For those interested in Fortran .NET
|
|
|
|
Part Five - Web Applications and XML Web Services |
|
For those interested |
|
|
http://www.cs.inf.ethz.ch |
|
|
Chapter 18 - ASP.NET Web Pages and Web Controls |
|
in Oberon .NET |
|
|
|
|
|
|
Chapter 19 - ASP.NET Web Applications |
|
|
|
Chapterhttp://www20 - XML.WebactivestateServices .com |
|
Details regarding |
Index |
|
Perl .NET and |
|
List of Figures |
|
Python .NET |
|
|
|
|
|
List of Tables |
|
Yes, even Smalltalk |
|
|
http://smallscript.org |
|
|
|
|
|
.NET (S#) is |
|
|
|
available |
Also be aware that Table 1-1 is not exhaustive. Microsoft maintains a list of vendors who are currently building (or have built) .NET implementations for their respective compilers, so check http://msdn.microsoft.com/vstudio/partners/language for the most up-to-date listings
(again, the exact URL is subject to change).
Life in a Multi-Language World
As developers first come to understand the language-agnostic nature of .NET, numerous questions arise. The most prevalent of these questions would have to be, "If all .NET languages compile down to managed code, why do we need more than one compiler?" There are a number of ways to answer this question.
First, we programmers are a very particular lot when it comes to our choice of programming languages (myself included). Some of us prefer languages full of semicolons, curly brackets, and as few language
keywords as possible. Others enjoy a language that offers more "human-readable" syntactic tokens (such
C# and the .NET Platform, Second Edition
as VB .NET). Still others may want to leverage their mainframe skills while moving to the .NET platform
by Andrew Troelsen ISBN:1590590554
(via COBOL .NET).
Apress © 2003 (1200 pages)
Now, be honest: IfThisMicrosoftomprehensivewere to textbuildstartsa singlewith"official"a brief overview.NET languageof the that was derived from the BASIC family of languages,C# lan uagecandyouthenreallyquicklysaymovesall programmersto key t chnicalwouldandbe happy with this choice? Or, if the
architectural issues for .NET developers.
single official managed language was based on C syntax, imagine all the folks out there who would ignore
.NET altogether. Because the .NET runtime could care less where a block of managed code originated,
.NET programmers can stay true to their syntactic preferences, and share the compiled assemblies
Table of Contents
among teammates, departments, and external organizations (regardless of which .NET language others
C# and the .NET Platform, Second Edition
choose to use).
Introduction
PartAnotherOne -excellentIntroducingbyproductC# and oftheintegrating.NET Platformvarious .NET languages into a single unified software solution is
Chapterthe simple1 -factThethatPhilosoall programminghy of .NET languages have their own sets of strengths and weaknesses. For Chapterexample,2 some- BuildingprogrammingC# Applicationslanguages offer excellent intrinsic support for advanced mathematical Paprocessingt Two - The. OthersC# Programmingoffer superiorLanguagesupport for graphical rendering, financial calculations, logical
calculations, interaction with mainframe computers and so forth. When you take the strengths of a
Chapter 3 - C# Language Fundamentals
particular programming language and then incorporate the benefits provided by the .NET platform,
Chapter 4 - Object-Oriented Programming with C#
everybody wins.
Chapter 5 - Exceptions and Object Lifetime
Chapter 6 - Interfaces and Collections
Of course, in reality the chances are quite good that you will spend much of your time building software
Chapter 7 - Callback Interfaces, Delegates, and Events
using your .NET language of choice (which is great!) However, as you may be expecting, once you learn
Chapter 8 - Advanced C# Type Construction Techniques
the syntax of one .NET language, it is very easy to master another. This is also quite beneficial, especially
Part Three - Programming with .NET Assemblies |
|
to the consultants of the world. If your language of choice happens to be C#, but you are placed at a client |
|
Chapter 9 |
- Understanding .NET Assemblies |
site that has committed to VB .NET, you should be able to parse the existing code body almost instantly |
|
Chapter 10 |
- Processes, AppDomains, Cont xts, and hre ds |
(honest!) while still continuing to leverage the .NET Framework. Enough said. |
|
Chapter 11 |
- Type Reflection, Late Binding, and Attribute-Based Programming |
Part Four - Leveraging the .NET Libraries
Chapter 12 - Object Serialization and the .NET Remoting Layer
Chapter 13 - Building a Better Window (Introducing Windows Forms)
Chapter 14 - A Better Painting Framework (GDI+)
Chapter 15 - Programming with Windows Forms Controls
Chapter 16 - The System.IO Namespace
Chapter 17 - Data Access with ADO.NET
Part Five - Web Applications and XML Web Services
Chapter 18 - ASP.NET Web Pages and Web Controls
Chapter 19 - ASP.NET Web Applications
Chapter 20 - XML Web Services
Index
List of Figures
List of Tables
An OverviewC#ofand.NETthe .NETBinariesPlatform, Second(aka EditionAssemblies)
by Andrew Troelsen |
ISBN:1590590554 |
Regardless of which .NET language you choose to program with, understand that despite the fact that
Apress © 2003 (1200 pages)
.NET binaries take the same file extension as classic COM binaries (*.dll or *.exe), they have absolutely no
This comprehensive text starts with a brief overview of the
internal similaritiesC#. Forlanguageexample,and *then.dll .NETquicklybinariesmovesdoto notkey exporttechnicalmethodsand to facilitate communications with the classic COMarchitecturalruntimeissu(givens forthat.NET.NETdevelopersis not COM). . Furthermore, .NET binaries are not described using IDL and are not registered into the system registry. Perhaps most important, unlike classic COM servers, .NET binaries do not contain platform-specific instructions, but rather platform-agnostic
Table of Contents
"intermediate language" (IL). Conceptually, Figure 1-2 shows the big picture of the story thus far.
C# and the .NET Platform, Second Edition
Introduction
Part
Chapter
Chapter
Part
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter 8 - Advanced C# Type Construction Techniques
Figure 1-2: All .NET-aware compilers emit IL instructions and metadata.
Part Three - Programming with .NET Assemblies
Chapter 9 - Understanding .NET Assemblies
Note There is one point to be made regarding the acronym "IL." During the development of .NET, the
Chapter 10 - Processes, AppDomains, Contexts, and Threads
official term for "IL" was Microsoft intermediate language (MSIL). However with the final release
Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming
of .NET, the (new) official term is common intermediate language (CIL). Thus, as you read the
Part Four - Leveraging the .NET Libraries
.NET literature (including the first edition of this text), understand that IL, MSIL and CIL are all
Chapter 12 - Object Serialization and the .NET Remoting Layer
describing the same exact entity. In keeping with the current terminology, I will use the term CIL
Chapter 13 - Building a Better Window (Introducing Windows Forms)
throughout this text from here on out.
Chapter 14 - A Better Painting Framework (GDI+)
Chapter 15 - Programming with Windows Forms Controls
When a *.dll or *.exe has been created using a .NET-aware compiler, the resulting module is bundled into
Chapter 16 - The Systexamine.IO Namespace
an "assembly." You numerous details of .NET assemblies in Chapter 9. However to facilitate the Chapterdiscussion17 -ofDatatheAccess.NET runtimewith ADOenvironment,.NET you do need to examine some basic properties of this new
Partfile Fiveformat- Web. Applications and XML Web Services
Chapter 18 - ASP.NET Web Pages and Web Controls
As mentioned, an assembly contains CIL code, which is conceptually similar to Java byte code in that it is
Chapter 19 - ASP.NET Web Applications
not compiled to platform-specific instructions until absolutely necessary. Typically "absolutely necessary"
Chapter 20 - XML Web Services
is the point at which a block of CIL instructions (such as a method implementation) are referenced for use
Index
by the .NET runtime engine.
List of Figures
List of Tables
In addition to CIL instructions, assemblies also contain metadata that describes in vivid detail the characteristics of every "type" living within the binary. For example, if you have a class named Car contained within a given assembly, the type metadata describes details such as Car's base class, which interfaces are implemented by Car (if any), as well as a full description of each member supported by the Car type.
In many respects, .NET metadata is a dramatic improvement to classic COM type information. As you may already know, classic COM binaries are typically described using an associated type library (which is little more than a binary version of IDL code). The problems with COM type information are that it is not guaranteed to be present, and the fact that IDL code has no way to catalog externally referenced servers that are required for the correct operation of the contained coclasses (a.k.a. 'COM class'). In contrast,
.NET metadata is always present and is automatically generated by a given .NET-aware compiler.
Finally, in addition to CIL and type metadata, assemblies themselves are also described using metadata, which is officially termed a manifest. The manifest contains information about the current version of the assembly, culture information (used for localizing string and image resources), and a list of all externally
referenced assemblies that are required for proper execution. You examine various tools that can be used
C# and the .NET Platform, Second Edition
to examine an assembly's underlying CIL, type metadata and manifest information later in this chapter.
by Andrew Troelsen |
ISBN:1590590554 |
Apress © 2003 (1200 pages) |
|
Single File and Multifile Assemblies
This comprehensive text starts with a brief overview of the
C# language and then quickly moves to key technical and
In a great number of cases, there is a simple one-to-one correspondence between a .NET assembly and architectural issues for .NET developers.
the underlying *.dll or *.exe binary. Thus, if you are building a .NET *.dll, it is safe to consider that the binary and the assembly are one and the same. Likewise, if you are building a .NET *.exe desktop Tableapplication,of C tentsthe *.exe can simply be referred to as the assembly itself. As seen in Chapter 9 however, this
is not completely accurate. Technically speaking, if an assembly is composed of a single *.dll or *.exe
C# and the .NET Platform, Second Edition
module, you have a "single file assembly." Single file assemblies contain all the necessary CIL, metadata
Introduction
and associated manifest in an autonomous, single, well-defined package.
Part One - Introducing C# and the .NET Platform
Chapter 1 - The Philosophy of .NET
Multifile assemblies, on the other hand, are composed of numerous .NET binaries, each of which is
Chapter 2 - Building C# Applications
termed a module. When building a multifile assembly, one of these modules (termed the primary module)
Part Two - The C# Programming Language
must contain the assembly manifest (and possibly CIL instructions and metadata for various types). The
Chapter 3 - C# Language Fundamentals
other related modules contain a module level manifest, CIL and type metadata. As you might suspect, the
Chapter 4 - Object-Oriented Programming with C#
primary module documents the set of required secondary modules within the assembly manifest.
Chapter 5 - Exceptions and Object Lifetime
ChapterSo, why6 would- Interfacesyou chooseand Ctollectionscreate a multifile assembly? When you partition an assembly into discrete
modules, you end up with a more flexible deployment option. For example, if a user is referencing a
Chapter 7 - Callback Interfaces, Delegates, and Events
remote assembly that needs to be downloaded onto his or her machine, the runtime will only download
Chapter 8 - Advanced C# Type Construction Techniques
the required modules. Therefore, you are free to construct your assembly in such a way that less
Part Three - Programming with .NET Assemblies
frequently required types (such as a type named HardDriveReformatter) are kept in a separate stand-
Chapter 9 - Understanding .NET Assemblies
alone module.
Chapter 10 - Processes, AppDomains, Contexts, and Threads
Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming
In contrast, if all your types were placed in a single file assembly, the end user may end up downloading a
Part Four - Leveraging the .NET Libraries
large chunk of data that is not really needed (which is obviously a waste of time). Thus, as you can see, an
Chapter 12 - Object Serialization and the .NET Remoting Layer
assembly is really a logical grouping of one or more related modules that are intended to be deployed and
Chapter 13 - Building a Better Window (Introducing Windows Forms) versioned as a single unit.
Chapter 14 - A Better Painting Framework (GDI+)
Chapter 15 - Programming with Windows Forms Controls
Chapter 16 - The System.IO Namespace
Chapter 17 - Data Access with ADO.NET
Part Five - Web Applications and XML Web Services
Chapter 18 - ASP.NET Web Pages and Web Controls
Chapter 19 - ASP.NET Web Applications
Chapter 20 - XML Web Services
Index
List of Figures
List of Tables
The Role ofC#theandCommonthe .NET Platform,IntermediateSecond EditionLanguage
by Andrew Troelsen |
ISBN:1590590554 |
Now that you have a better feel for .NET assemblies, let's examine the role of the common intermediate
Apress © 2003 (1200 pages)
language (CIL) in a bit more detail. CIL is a language that sits above any particular platform-specific
This comprehensive text starts with a brief overview of the
instruction set. RegardlessC# languageof whichand then.NETquickly-awaremoveslanguageto keyyoutechnicalchooseand(C#, Visual Basic .NET, Eiffel
.NET, and so forth),architthecturalassociatedissues compilerf r .NET developersemits CIL.instructions. For example, the following C# namespace definition models a trivial calculator (which is only capable of returning the sum of 10 and 84...). Don't concern yourself with the exact syntax for the time being, but do notice the signature of the
Table of Contents
Add() method:
C# and the .NET Platform, Second Edition
Introduction
// We will examine namespaces later in the chapter...
Part One - Introducing C# and the .NET Platform
using System;
Chapter 1 - The Philosophy of .NET
Chapter 2 - Building C# Applications
namespace Calculator
Part Two - The C# Programming Language
{
Chapter 3 - C# Language Fundamentals
// This class contains the app's entry point.
Chapter 4 - Object-Oriented Programming with C# public class CalcApp
Chapter {5 - Exceptions and Object Lifetime
Chapter 6 - Interfacespublic staticand Collectivonsid Main()
Chapter 7 - Callback{ Interfaces, Delegates, and Events
Calc c = new Calc();
Chapter 8 - Advanced C# Type Construction Techniques
int ans = c.Add(10, 84);
Part Three - Programming with .NET Assemblies
Console.WriteLine("10 + 84 is {0}.", ans);
Chapter 9 - Understanding .NET Assemblies
Console.ReadLine(); // Wait for user key-press.
Chapter 10 - Processes, AppDomains, Contexts, and Threads
}
Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming
}
Part Four - Leveraging the .NET Libraries
// The C# calculator.
Chapter 12 - Object Serialization and the .NET Remoting Layer
public class Calc
Chapter 13 - Building a Better Window (Introducing Windows Forms)
{
Chapter 14 - A Better Painting Framework (GDI+)
// A single method.
Chapter 15 - Programming with Windows Forms Controls public int Add(int x, int y)
Chapter 16 - The System.IO Namespace
{ return x + y; }
Chapter 17 - Data Access with ADO.NET
}
Part Five - Web Applications and XML Web Services
}
Chapter 18 - ASP.NET Web Pages and Web Controls
Chapter 19 - ASP.NET Web Applications
ChaptOncerthe20 C#- XMLcompilerWeb Services(csc.exe) compiles this source code file, you end up with a single file *.exe
assembly that contains a manifest, CIL instructions, and metadata describing each aspect of the Calc and
Index
CalcApp classes. For example, if you peek inside this binary using ildasm.exe (examined a little later in this
List of Figures
chapter) you find the Add() method is represented using CIL such as the following:
List of Tables
.method public hidebysig instance int32 Add(int32 x, int32 y) cil managed
{
// Code size |
8 (0x8) |
.maxstack 2 |
|
.locals init ([0] int32 CS$00000003$00000000)
IL_0000: |
ldarg.1 |
IL_0001: |
ldarg.2 |
IL_0002: |
add |
IL_0003: |
stloc.0 |
IL_0004: |
br.s IL_0006 |
IL_0006: |
ldloc.0 |
IL_0007: |
ret |
} // end of |
method Calc::Add |
C# and the .NET Platform, Second Edition
Don't worry if you are unable to make heads or tails of the resulting CIL for this method. The point to
by Andrew Troelsen ISBN:1590590554 concentrate on is that the C# compiler emits CIL, not platform specific instructions.
Apress © 2003 (1200 pages)
Now, recall that thisThisiscomprehensivetrue of all .NETtext-awarestartscompilerswith a brief. Toverviewillustrate,ofassumethe you created the Calc
C# language and then quickly moves to key technical and application using Visual Basic .NET, rather than C#:
architectural issues for .NET developers.
' The VB .NET calculator...
Class Calc
Table of Contents
Public Function Add(ByVal x As Integer, ByVal y As Integer) As Integer
C# and the .NET Platform, Second Edition
Introduction ' Yes! VB .NET (finally) supports a 'Return' keyword.
Return x + y
Part One - Introducing C# and the .NET Platform
End Function
Chapter 1 - The Philosophy of .NET
End Class
Chapter 2 - Building C# Applications
Part Two - The C# Programming Language
' A VB .NET 'Module' is a class that only contains
Chapter 3 - C# Language Fundamentals
' static members.
Chapter 4 - Object-Oriented Programming with C#
Module CalcApp
Chapter 5 - Exceptions and Object Lifetime
Sub Main()
Chapter 6 - Interfaces and Collections
Dim ans As Integer
Chapter 7 - Callback Interfaces, Delegates, and Events
Dim c As New Calc
Chapter 8 - Advanced C# Type Construction Techniques ans = c.Add(10, 84)
Part Three - Programming with .NET Assemblies
Console.WriteLine("10 + 84 is {0}.", ans)
Chapter 9 - Understanding .NET Assemblies
Console.ReadLine()
ChapterEnd10 -SubProcesses, AppDomains, Contexts, and Threads
ChapterEnd Module11 - Type Reflection, Late Binding, and Attribute-Based Programming
Part Four - Leveraging the .NET Libraries
Chapter 12 - Object Serialization and the .NET Remoting Layer
If you examine the CIL for the Add() method, you find the same sort of instructions (slightly tweaked by the
Chapter 13 - Building a Better Window (Introducing Windows Forms)
VB .NET compiler):
Chapter 14 - A Better Painting Framework (GDI+)
Chapter 15 - Programming with Windows Forms Controls
.method public instance int32 Add(int32 x, int32 y) cil managed
Chapter 16 - The System.IO Namespace
{
Chapter 17 - Data Access with ADO.NET
// Code size 9 (0x9)
Part Five - Web Applications and XML Web Services
.maxstack 2
Chapter 18 - ASP.NET Web Pages and Web Controls
.locals init ([0] int32 Add)
Chapter 19 - ASP.NET Web Applications
IL_0000: |
nop |
|
Chapter 20 - XML Web Services |
|
|
IL_0001: |
ldarg.1 |
|
Index |
ldarg.2 |
|
IL_0002: |
|
|
List of Figures |
add.ovf |
|
IL_0003: |
|
|
List of Tables |
stloc.0 |
|
IL_0004: |
IL_0007 |
|
IL_0005: |
br.s |
|
IL_0007: |
ldloc.0 |
|
IL_0008: |
ret |
|
} // end of |
method Calc::Add |
SOURCE |
The CSharpCalculator and VBCalculator applications are both included under the |
CODE |
Chapter 1 subdirectory. |
Benefits of CIL
At this point, you might be wondering exactly what benefits are gained by compiling source code into CIL (with the associated metadata) rather than directly to a specific instruction set. One benefit of compiling to CIL (with the associated metadata) is language integration. As you have already seen, each .NET-aware
language produces the same underlying CIL. Therefore, all languages are able to interact within a well-
C# and the .NET Platform, Second Edition defined binary arena.
by Andrew Troelsen ISBN:1590590554
Furthermore, givenApressthat©CIL2003is(1200platformpages)agnostic, the .NET runtime is poised to become a platformindependent architecture,This comprehensiveproviding thetextsamestartsbenefitswith a briefJavaoverdevelopersiew of thehave grown accustomed to (i.e., the potential of aC#singlelanguagecode baseand thenrunningquicklyon movesnumerousto keyoperatingt chnicalsystems)and . In fact, there is an
architectural issues for .NET developers.
international standard for a large subset of the .NET platform and implementations already exist on a few non-Windows operating systems. For example, you can run basic C# programs on both FreeBSD Unix
and Apple (under OS X) platforms already. Unlike Java however, .NET allows you to build applications in
Table of Contents
a language-independent fashion. Thus, .NET has the potential to allow you to develop an application in
C# and the .NET Platform, Second Edition
any language and have it run on any platform.
Introduction
Part OneNote- IntroducingI'll commentC#a bitandmorethe .NETon thePlatformplatform-independent nature of .NET at the conclusion of this
Chapter 1 chapter- The Philosophy. of .NET
Chapter 2 - Building C# Applications
Part Two - The C# Programming Language
Chapter 3 - C# Language Fundamentals
Chapter 4 - Object-Oriented Programming with C#
Chapter 5 - Exceptions and Object Lifetime
Chapter 6 - Interfaces and Collections
Chapter 7 - Callback Interfaces, Delegates, and Events
Chapter 8 - Advanced C# Type Construction Techniques
Part Three - Programming with .NET Assemblies
Chapter 9 - Understanding .NET Assemblies
Chapter 10 - Processes, AppDomains, Contexts, and Threads
Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming
Part Four - Leveraging the .NET Libraries
Chapter 12 - Object Serialization and the .NET Remoting Layer
Chapter 13 - Building a Better Window (Introducing Windows Forms)
Chapter 14 - A Better Painting Framework (GDI+)
Chapter 15 - Programming with Windows Forms Controls
Chapter 16 - The System.IO Namespace
Chapter 17 - Data Access with ADO.NET
Part Five - Web Applications and XML Web Services
Chapter 18 - ASP.NET Web Pages and Web Controls
Chapter 19 - ASP.NET Web Applications
Chapter 20 - XML Web Services
Index
List of Figures
List of Tables
The Role ofC#.NETand theType.NET Platform,MetadataSecond Edition
by Andrew Troelsen |
ISBN:1590590554 |
COM programmers are without a doubt familiar with the Interface Definition Language (IDL). IDL is a
Apress © 2003 (1200 pages)
"metalanguage" that is used to describe the types contained within a given COM server. IDL is compiled
This comprehensive text starts with a brief overview of the
into a binary formatC#(termedlanguageaandtypethenlibrary)quicklyusingmovesthe midlto key.exetechnicalcompiler,andwhich can then be used by a COMaware language toarchitecturalmanipulateissuthescontainedfor .NET developerstypes. .
In addition to describing the types within a COM binary, IDL has minimal support to describe characteristics
TableaboutoftheContentsCOM binary itself, such as its current version (e.g., 1.0, 2.0, or 2.4) and intended locale (e.g., C#English,and theGerman,.NET Platform,Urdu, Russian)Second Edition. The problem with COM metadata is that it may or may not be present
and it is often the role of the programmer to ensure the underlying IDL accurately reflects the internal
Introduction
types.
Part One - Introducing C# and the .NET Platform
Chapter 1 - The Philosophy of .NET
The .NET Framework makes no use of IDL whatsoever. However, the spirit of describing the types residing within a particular binary lives on. In addition to the underlying CIL instructions, a .NET assembly
contains full, complete and accurate metadata. Like COM IDL, .NET metadata describes each and every
Chapter 3 - C# Language Fundamentals
type (class, structure, enumeration, and so forth) defined in the binary, as well as the members of each
Chapter 4 - Object-Oriented Programming with C# type (properties, methods, events, and so on).
Chapter 5 - Exceptions and Object Lifetime
ChapterFurthermore,6 - Interfacesthe .NETandmanifestCollections(which as you recall is metadata describing the assembly itself) is far Chaptmorercomplete7 - CallbackthanInterfaces,IDL, in thatDelegates,it also describesand Eventseach externally referenced assembly that is required by
the executing assembly to operate correctly. Because .NET metadata is so wickedly meticulous,
Chapter 8 - Advanced C# Type Construction Techniques
assemblies are completely selfdescribing entities. So much so in fact, .NET binaries have no need to be
Part Three - Programming with .NET Assemblies
registered into the system registry (more on that little tidbit later in the text).
Chapter 9 - Understanding .NET Assemblies
Chapter 10 - Processes, AppDomains, Contexts, and Threads
ChapterA Quick11 - TypeMetadataReflection, LateExampleBinding, and Attribute-Based Programming
Part Four - Leveraging the .NET Libraries
To illustrate the format of .NET metadata, let's take a look at the metadata that has been generated for
Chapter 12 - Object Serialization and the .NET Remoting Layer
the Add() method of the C# Calculator class you examined previously (the metadata generated for the VB
Chapter 13 - Building a Better Window (Introducing Windows Forms)
.NET Add() method is similar). Using the ildasm.exe utility (examined at the end of this chapter) you can
Chapter 14 - A Better Painting Framework (GDI+)
view your assembly's metadata by hitting the Ctrl+M keystroke. Within the resulting "MetaInfo" window, you
Chapter 15 - Programming with Windows Forms Controls
will find a description of the Add() method looking something like the following::
Chapter 16 - The System.IO Namespace
Chapter 17 |
- Data Access with ADO.NET |
PartMethodFive - Web#2 Applications and XML Web Services |
|
Chapter-------------------------------------------------------18 - ASP.NET Web Pages and Web Controls |
|
ChapterMethodName:19 - ASP.NETAdd Web(06000002)Applications |
|
ChapterFlags20 |
- XML: Web[Public]Services[HideBySig] [ReuseSlot] (00000086) |
RVA : 0x00002064
Index
ImplFlags : [IL] [Managed] (00000000)
List of Figures
CallCnvntn: [DEFAULT]
List of Tables
hasThis
ReturnType: I4
2 Arguments
Argument #1: I4
Argument #2: I4
2Parameters
(1)ParamToken : (08000001) Name : x flags: [none] (00000000)
(2)ParamToken : (08000002) Name : y flags: [none] (00000000)
Here you can see that the Add() method, return type, and method arguments have been fully described by the C# compiler. Needless to say, metadata is used by numerous aspects of the .NET runtime environment, as well as by various development tools. For example, the IntelliSense feature provided by Visual Studio .NET is made possible by reading an assembly's metadata at design time. Metadata is also used by various object browsing utilities, debugging tools, and even the C# compiler itself. To be sure, metadata is the backbone of numerous .NET technologies such as .NET Remoting, reflection services,
and object serialization.
C# and the .NET Platform, Second Edition
by Andrew Troelsen |
ISBN:1590590554 |
Apress © 2003 (1200 pages)
This comprehensive text starts with a brief overview of the C# language and then quickly moves to key technical and architectural issues for .NET developers.
Table of Contents
C# and the .NET Platform, Second Edition
Introduction
Part One - Introducing C# and the .NET Platform
Chapter 1 - The Philosophy of .NET
Chapter 2 - Building C# Applications
Part Two - The C# Programming Language
Chapter 3 - C# Language Fundamentals
Chapter 4 - Object-Oriented Programming with C#
Chapter 5 - Exceptions and Object Lifetime
Chapter 6 - Interfaces and Collections
Chapter 7 - Callback Interfaces, Delegates, and Events
Chapter 8 - Advanced C# Type Construction Techniques
Part Three - Programming with .NET Assemblies
Chapter 9 - Understanding .NET Assemblies
Chapter 10 - Processes, AppDomains, Contexts, and Threads
Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming
Part Four - Leveraging the .NET Libraries
Chapter 12 - Object Serialization and the .NET Remoting Layer
Chapter 13 - Building a Better Window (Introducing Windows Forms)
Chapter 14 - A Better Painting Framework (GDI+)
Chapter 15 - Programming with Windows Forms Controls
Chapter 16 - The System.IO Namespace
Chapter 17 - Data Access with ADO.NET
Part Five - Web Applications and XML Web Services
Chapter 18 - ASP.NET Web Pages and Web Controls
Chapter 19 - ASP.NET Web Applications
Chapter 20 - XML Web Services
Index
List of Figures
List of Tables