O'Reilly - .NET Framework Essentials, 2nd Edition

如果你想了解.NET Framework 的基础那么这本书值得 一看!

我觉得只看前三章就可以了,后面的可以选着看
 
前面三章看完了,你对.NET Framework 就有个大致的了解了。这样对于开发asp.net的我已经足够了!

下面是我自己的读书笔记:

 

开始读这本书,刚看完了第一章,觉得写的还不错,至少前言部分就已经吸引我了
早就开始知道一点.NET FrameWork,但是也只是知之皮毛,现在正好好好的学习一下
这将是一个系列的blog,我会把一步步的来完成。

第一章是简介,所以很多东西我都是跳着看,呵呵,由于本书是英文的,才发现偶尔会出现一个单词,不认识了,呵呵,一直来以为自己的英语足够应付了,呵呵,看来还得加强啊~~

今天学到三点东西,至少说应该丰富了三点东西:

1.net platform consists of 4 product group

Development Tools

A set of language: C#, VB.NET, J#, C++.NET
A set of develop tool: Visual Studio
A comprehensive of class Library for build web, windows app, web services
CLR: Common Language Runtime

Specialized servers

A set of enterprise servers:
SQL Server,
Exchange Server,
BizTalk Server…

Web services

XML, SOAP, WSDL, UDDI…

Devices

NET-enabled non-PC Device

 2 .NET PLATFORM main five components

Visual Studio.NET

.NET Enterprise Services

.Net Framework

.NET Building Block Services

Operation System

 3 .NET FRAMEWORK

WEB SERVICES

WEB FORMS

WINDOWS FORMS

DATA AND XML CLASS

FRAMEWORK BASE CLASS

COMMOM LANGUAGE RUNTIME


WINDOWS PLATFORM

 这样,你会对.NET FrameWork有一个比较直观的印象了。那么究竟什么是NET FrameWork?
我们来看看MS的解释:The .NET Framework is a development and execution environment that allows different programming languages & libraries to work together seamlessly to create Windows-based applications that are easier to build, manage, deploy, and integrate with other networked systems.
由此可见,它是一个环境,一个开发和执行的环境。它允许不同的编程语言和程序库一起良好的运行。使创建基于WINDOWS的应用程序易于构建、管理,集成,并且能(当然是创建的基于WINDOWS的应用程序)和其他网络系统一起结合。

MS给出的.Net Framework的组成介绍:
 

  • The Common Language Runtime (CLR)
    A language-neutral development & execution environment that provides services to help "manage" application execution
    提供有助于管理应用程序执行的服务的中间语言开发环境和执行环境
  • The Framework Class Libraries (FCL)
    A consistent, object-oriented library of prepackaged functionality
    一系列的,面向对象的按照功能封装的库


  • Support for standard networking protocols & specifications
  • Support for different programming languages
  • Support for programming libraries developed in different languages 
  • Support for different platforms 

从微软给出的解释,和我上面的第三点比较,看的出来,多了一个asp.net,也就是说asp.net=XML Web Services+Web Froms。


更多微软关于Net Framework的信息,你可以参考这里(http://msdn.microsoft.com/netframework/default.aspx
关于我看的着本书的信息,可以参考这里(http://www.oreilly.com/catalog/dotnetfrmess2



 Chapter 2
 
CLR: manage and execute code written in .NET languages and is the basis of the .NET architecture.

2.1 CLR Environment  
How does the CLR work?      
CLR is the runtime engine that loads required classes, performs just-in-time complication on needed methods, enforces security checks, and accomplishes a bunch of other runtime functionalities.  
2.2 CLR Executables  .NET executables:
They carry not only code and data, but also metadata ----------VS-------  Typical windows executables: only code and data  The format of one .NET PE File

 
 2.3 Metadata:
是机器可以识别的关于源文件的信息,或者可以翻译成“关于数据的数据”。 在.NET中,元数据包括:定义类型、版本信息、内部组件资料、以及其他标准化信息。 在.NET中,元数据就是在COM中的Type Library
元数据的作用

CLR CLR uses metadata for verification, security enforcement, cross-context marshaling, memory layout, and execution
Class Loader The Class Loader uses metadata to find and load .NET classes
JIT Complier JIT complier use metadata to compile Microsoft Intermediate Language(IL) code
Tools Tools use metadata to support integration.

2.3.3 Examine the metadata by a tool named ildasm.exe 2.3.3 Inspecting and Emitting Metadata       
System. Reflection Reflection API: Use those collectively classes to load and      insoect a .NET assembly to determine what types it supports.
System.Reflection.Emit
2.3.4 Interoperability Support
    Introduce 4 tools: the .NET assembly registration utility (RegAsm.exe); the type library exporter (tlbexp.exe); the type library importer (tlbimp.exe); the XML schema definition tool (xsd.exe)
RegAsm.exe To register a .NET assembly into the registry so COM clients can make use of it
tlbexp.exe Generate a type library file (.tlb) when you pass it a .NET assembly
tlbimp.exe Make a .NET assembly look like a COM component
xsd.exe 1 To convert an XML schema into a C# class, and vice versa. 2 can also take a .NET assembly and generate an XML schema definition (XSD) that represents the types within the .NET assembly
 
2.4 Assemblies and Manifests
       Assemblies: deployable units
       Manifests: the metadata that describes the assemblies
      Difference of the two definition: an assemble is a logic DLL or EXE, and a manifest is a detail description (metadata) of an assembly, including its version, what other assemblies it uses, and so on.
   2.4.1 Assemblies versus Components

       在COM时代, 微软的文档中经常出现 component , 有时指a COM Class , 有时指a COM module (DLL或EXE), 迫使读者或开发人员在遇见她的时候去根据上下文去理解是哪个意思. 在..NET中通过引入assembly这个新词来区别这个混淆.
       Assembly: a software component that supports plug-and-play, much like a hardware component. Theoretically, a .NET assembly is approximately equivalent to a COM module, In practice, an assembly can contain or refer to a number of types and physical files (including bitmap files, .NET PE files, and so forth) that are needed at runtime for successful execution. In addition to hosting IL code, an assembly is a basic unit of versioning, deployment, security management, side-by-side execution, sharing, and reuse.  
2.4.2 Unique Identities
        In COM: Given the number of GUID In COM, development and deployment can be tedious because you must use these magic numbers in your code and elsewhere all the time.
       In .NET: you refer to a specific type by its readable name and its namespace. Since a .NET readable name and its namespace are not enough to be globally unique, .NET guarantees uniqueness by using unique public/private key pairs. All assemblies that are shared by multiple applications must be built with a public/private key pair. Public/private key pairs are used in public-key cryptography. Since public-key cryptography uses asymmetric encryption, an assembly creator can sign an assembly with a private key, and anyone can verify that digital signature using the assembly creator’s public key.


  2.4.3 IL Code
  2.4.4 Versioning
           1 Static assemblies
              2 Dynamic assemblies
              3 Private assemblies
              4 Public or Shared assemblies
 2.4.5 Deployment: you have no longer to use registry to store activation and marshaling hints as in COM
  2.4.6 Security: an assembly itself has a code identity, which includes information such as the assembly’s shared name, version number, culture, and public key. Using this concept, the CLR cam verify whether an assembly is permitted to access system resources or make calls to other assemblies
 2.4.7 Side-by-Side Execution
              Side-by-side execution: the CLR allows any versions of the same, shared DLL to execute at the same time, on the same system, and even in the same oricess.
              MS .NET accomplishes side-by-side execution by using the versioning and deployment features that are innate to all shared assemblies. This concept allows you to install ant versions of the same, shared assembly on the same machine, without versioning conflicts or DLL Hell. The only caveat is that your assemblies must be public or shared assemblies, meaning that you must register them against the GAC using a tool such as the .NET Global Assembly Cache Utility. Once you have registered different versions of the same shared assembly into the AC, the human-readable name of the assembly no longer matters—what’s important is the information provided by .Net’s versioning and deployment features.
              Recall: the shared assembly’s version information + 8-byte hash of the shared assembly’s public key. Use the two information, the CLR can find the exact shared assembly that your application uses, and it will even verify that your 8-byte hash is indeed equivalent to that of the shared assembly.
  2.4.8 Sharing and Reuse  
       when you want to share your assembly with others, your assembly must have a shared or strong name, and you must register in the GAC.
  2.4.9 Manifests: Assembly Metadata
  an assembly manifests is metadata that describes everything about the assembly, including its identity, a list of files belonging to the assembly, references to external assemblies, exported types, exported resources, and permission requests. In short, it describes all the details that are required for component plug-and-play.

Use a class

In COM

In .NET

1 the COM library looks up in the registry to find the COM  component that exposes that class.
2 loads the component
3 tell the component to give it an instance of that class
4 return a reference to this instance

1 CLR peers right into the assembly manifest
2 determines which external assembly is needed
3 loads the exact assembly that’s required by your application
4 creates an instance of the target class

2.4.10 Creating Assemblies
you can create a single-module assembly or a multi-module assembly.
a single-module assembly: everything in a build is clumped into one exe or dll, which is easy created because a complier takes care of creating the single-module assembly for you.
a multi-module assembly, one that contains many modules and resource files, you have a few choices. One option is to use the Assembly Linker (al.exe). this tool takes one or more IL or resource files and spits out a file with an assembly manifest, also, we can use visual studio

2.4.11 Using Assemblies
To use an assembly, first import the assembly into your code, the syntax of which is dependent upon the language that you use. In c#, there is a simple
    using system;
when you build you application, you must tell the complier that you are referencing an external assembly.
2.5 Intermediate Language (IL)
Abstraction: we often use abstraction to hide the complexity of system or application services, providing instead a simple interface to the consumer. As long as we can keep the interface the same, we can change the hideous internals, and different consumers can use the same interface.
In language advances, scientists introduced different incarnations of language-abstraction layers, such as p-code and bytecode.
Produced by the Pascal-P complier, p-code is an intermediate language that supports procedural programming.
Generated by Java compliers, bytecode is an intermediate language that supports object-oriented programming. Bytecode is a language abstraction that allows Java code to run on different operating platforms, as long as the platforms have a Java Virtual Machine to execute bytecode
MS calls its own language-abstraction layer the Common Intermediate Language (CIL). Similar to bytecode, IL supports all object-oriented features, including data abstraction, inheritance, polymorphism, and useful concepts such as exceptions and events. In addition to these features, IL supports other concepts, such as properties, fields, and enumeration. Any .NET language may be converted into IL., so .NET supports multiple languages and perhaps multiple platforms in the future.

2.6 The CTS and CLS
Both the CTS and CLS ensure language compatibility, interoperability, and intefration.
2.6.1 CTS
all language should be equal in .NET. In order to make language integration a reality, MS has specified a common type system to which every .NET language must abide. MS supports a rich set of types, and there introduce some important, including value types, reference types, classes, interfaces, and delegates.
2.6.1.1 Value types: represent values allocated on the stack. They cannot be null and must always contain some data. When value types are passed into a function, they are passed by value, meaning that a copy of the values is made prior to function execution. This implies that the original value won’t change, no matter what happens to the copy during the function call. Since intrinsic types are small in size and don’t consume much memory, the resource cost of making a copy is negligible and outweighs the performance drawbacks of object management and garbage collection. Values types include primitives, structures, and enumerations; example:

int i;                            // primitives
struct point{int x, y}               // structures
enum State{off, on}             // enumerations


you can also create a value type by deriving a class from System.ValueType. One thing to note is that a value type is sealed; meaning that once you have derived a class from System.ValueType, no one else can derive from your class.

2.6.1.2 Reference types
if a type consumes significant memory resources, then a reference type provides more benefits over a value type. Reference types are so called because they contain references to heap-based objects and can be null. These types are passed by reference, meaning that when you pass such an object into a function, an address of or pointer to the object is passed—not a copy of the object, as in the case of a value type. Since you are passing a reference, the caller will see whatever the called function does to your object. The first benefit here is that a reference type can be used as an output parameter, but the second benefit is that you don’t waste extra resources because a copy is not made. If your object is large, then reference types are a better choice. In .NET, one drawback of a reference type is that it must be allocated on the managed heap, which means it requires more CPU cycles because it must be managed and garbage-collected by the CLR. In .NET, the closest concept to destruction is that finalization, but unlike destructors in C++, finalization is nondeterministic. In other words, you don’t know when finalization will happen because it occurs when the garbage collector executes. Since finalization is nondeterministic. Another drawback of reference types is that if reference-type objects hold on to expensive resources that will be released during finalization, system performances will degrade because the resources won’t released until these objects are garbage-collection. Reference types include classes, interfaces, arrays, and delegates. example:

如果一个类型消耗重要的内存资源,此时引用类型比值类型提供更多的好处。引用类型这样的叫它是因为他们包含很多基于堆栈的对象而且他们不能为空. 这些类型通过引用传输,这意味着当你传输这样的一个对象到一个函数的时候,这个对象的一个地址或指针被传输,而不是一个对象被复制,这与值类型一样。当你传输一个引用时,调用者将看到,无论这个调用是否针对你的对象。 这样做的第一个好处是引用类型可以作为对外输出的参数,但第二个好处是你不会浪费多余的资源因为并没有产生一个复制。 如果你的对象比较大,那么引用类型是您的一个很好的选择。 .NET中引用类型的一个缺点是它必须由堆栈来管理分配,这意味着它会要求更度的CPU循环因为它由CLR来管理和进行垃圾回收,在.NET中析构的最接近的意思是最终化,但和C++不同,最终化是不确定的,换句话说,你并不知道什么时候最终化发生,因为它是有垃圾回收开始开始执行的时候而发生。因此最终化是不确定的。另外一个缺点就是引用类型是如果一个引用类型对象在最终化的时候继续消耗即将释放的资源,系统性能将会下降因为这个资源不会被释放知道这些对象被垃圾回收站回收。引用类型包括类,接口,数组和代理。例: 

class car {}               //class
interface Istellting {}       //interface
int [] a = new int[5];        //array
delegate void process()      //delegate

2.6.1.3 Boxing and unboxing
MS .NET supports values types for performance reasons, but everything in .NET is ultimately an object. In fact, all primitive types have corresponding classes in the .NET Framework. For example, int is in fact an alias of System.Int32, and System.Int32 happens to derive form System.ValueType, meaning that it is a value type. Value types are allocated on the stack by default, but they can always be converted into heap-based reference-type object, called boxing. The following code snippet shows that we can create a box and copy the value of i into it:
基于性能的原因,MS .NET支持各种值类型,但是所有的东西最终都是对象。实际上,所有的私有类型都有一个想关联的类。举例来说,int实际上是System.Int32的另一种叫法,System.Int32是从System.ValueType中派生出来的。这意味着它是一个值类型。值类型是从堆栈中默认分配,但它们经常被转换为基于堆的引用类型对象,这就叫做装箱。
[说白了:就是将一个值类型转换成一个object类型!方法:声明一个值类型,再将这个值类型隐式的转换成一个object类型,并将这个复制给该object类型]
int i = 1;
Object box=i;            //隐式
Object box= Object (i);    //
显式

when you box a value, you get an object upon which you can invoke methods, properties, and events. For example, once you have converted the integer into an object, as shown in this code snippet, you can call methods that are defined in System.Object, including ToString(), Equals(), and so forth.
[
装箱还有一个方面:就是你一个把值类型转换成一个object类型,便可以调用object类型的方法,属性和事件。]
装箱过程: 


The reverse of boxing is of course unboxing, which means that you can convert a heap-based reference-type object into its alue-type equivalent, as shown here;
int j = (int)box;
拆箱过程: 
 

2.6.1.4 Class, Properties, indexers
see detail in the next chapter
2.6.1.5 Interfaces
Interfaces support exactly the same concept as a C++ abstract base class(ABC) with only prure virtual functions. An ABC is a class that declares one or more pure functions and thus cannot be instantiated. You specify them, but you don’t implement them. A class that derives from you interface must implement you interface. An interface may contain methods, properties, indexers, and events. In .NET, a class can derive from multiple interfaces.


2.6.1.6 Delegates
Delegate
声明用于定义一种引用类型,该类型可用于将方法用特定的签名封装。委托实例可以封装静态方法或实例方法。委托大致类似C++中的函数指针;但是委托是类型安全和可靠的
2.6.2 The Common Language Specification (CLS)
The CLS specifies a series of basic rules that are required for language integration. Since Ms provides the CLS that spells out the minimum requirements for being a .NET language, compiler vendors can build their compilers to the specification and provide languages that target .NET. Besides compilers writers, application developers should read the CLS and use its rules to guarantee interoperation.

CLS是一个标准,若要写自己的.NET编译器,或者开发相关程序,需要遵循这个标准。因为这个标准定义了不同语言被编译成.NET语言的基本规范


2.7 CLR Execution

The major component of the CLR include the class loader, verifier, JIT compilers, and other execution support, such as code management, security management, garbage collection, exception management, debug management, marshaling management, thread management, and so on.

2.7.1 Class Loader
The class Loader loads .NET classes into memory and prepares them for execution. Before it can successfully do this, it must locate the target class. To find the target class, the class loader looks in several different places, including the application configuration file (.config) in the current directory, the GAC, and the metadata that is part of the PE file, specially the manifest. The information that is provided by one or more of these items is crucial to locating the correct target class. Recall that a class can be scoped to a particular namespace, a namespace can be scoped to a particular assembly, and an assembly can be scoped to a specific version. Given this, two classes, both named Car, are treated as different types even if the version information of their assemblies are the same.
类加载器把类加载到内存,并让它们执行的准备。在完成这个之前,类加载器必须先发现目标类。为了发现目标类,类加载器在几个不同的地方进行查找,包括在当前目录下的应用程序配置文件,GACPE文件中的元数据,特别是声明部分。在这些的一个或者多个重中找到的信息对成功加载目标类是非常重要的。调用能发生在下面的情况下:在一个命名空间中调用一个类,在一个组件中调用一个命名空间,在一个确定的版本中调用一个组件。在这种条件下,两个类都叫“汽车”,将被看作不同的类型,即使它们组件的版本信息是一样的。

Function of the class loader:
Once the class loader has found and loaded the target class, it caches the types information for the class so that in doesn’t have to load the class again for the duration of this process. By caching this information, it will later determine how much memory is needed to allocate for the newly created instance of this class. Once the target class is loaded, the class loader injects a small stub, like a function prolog, into every single method of the loaded class. This stub is used for two purposes: to denote the status of JIT compilation and to transition between managed and unmanaged code. At this point, if the loaded class references other classes, the class loader will also try to load the referenced types. However, if the referenced types have already been loaded, the class loader has to do nothing. Finally, the class loader uses the appropriate metadata to initialize the static variables and instantiate an object of the loaded class for you.

2.7.2 Verifier.
What is? is the component that executes at runtime to verify that the code is type safe.
When verify? After the class loader has loaded a class and before a piece of IL code can execute.
What verify?
    Main: The metadata is well formed; The IL code is type safe.
    Other: performs rudimentary control-flow analysis of the code to ensure that the code is using types correctly.

Note: 1 it kicks in only when a method is being invoked, not when a class or assembly is loaded.
     2 verification is an optional step because trusted code will never be verified but will be immediately directed to the JIT compiler for compilation.

2.7.3 JIT Compilers.
What is it?  Just In Time Compiler
What Function? Convert IL to (managed) native code so that it can execute on the target operation system.
How perform? JIT compilation occurs only the first time a method is invoked. Recall that the class loader adds a stub each method during class loading. At the first method invocation, the VES reads the information in this stub, which tells it that the code for the method has not been JIT-compiled. At this indication, the JIT compiler complies the method and injects the address of the managed native method into this stub. During subsequent invocations to the same methods, no JIT compilation is needed because each time the VES goes to read information in the stub, is sees the address of the native method. Because the JIT compiler only performs its magic the first time a method is invoked, the methods you don’t need at runtime will never be JIT-complied.

Other: if you want to avoid the cost of JIT compilation at runtime, you can use a special tool called ngen, which compilers your IL during installation and setup time.

2.7.4 Execution Support and Management
    1 Garbage collection
    2 Exception handling
    3 Security support: code is safe to execute, declarative and imperative security checks.
    4 Debug support: Controlling program execution, breakpoints, exceptions, control flow …
    5 Interoperation support: managed (CLR) and unmanaged (no CLR) worlds



3.1 common programming model

3.1.1 System.Object
Every type in .NET is an object, meaning that it must derive directly or indirectly form the Object class. The Object class exposes the public methods listed in table 3-1, which you can invoke on any given .NET object at runtime.

Methods

Description

Equals()

比较两个类,确定它们是否一致

ReferenceEquals()

比较两个类,确定它们是否引用了相同的类

GetHashCode()

获取对象的哈希代码

GetType()

获取对象的类型

ToString()

将对象转换为字符


3.1.2 Major Namespaces

Namespace

Description

System

See detail at MSDN

System. IO

System.Collections

System.Threading

System.Reflection

System.Security

System.Net

System.Data

System.Web.Services

System.Web.UI

System..Windows.Forms


3.2 Core Features and Languages
 Namespace
 Interface
 Encapsulation
 Inheritance
 Polymorphism?
 Exception handling


3.2.1 Managed C++ Code
(……)


3.2.2 VB Code

3.2.3 C# Code

using System
namespace  lang       //define a namespace
{
interface ISteering      //define interface
{
void TurnLeft()
void TurnRight()
}


abstract class vehicle : Isteering       //define abstract class to implement interface.
{
   
public void TurnLeft()

{
Console.WriteLine(“Left”);
}


public void TurnRight()
{
Console.WriteLine(“Right”);
}

public abstract void ApplyBrakes();
}
 

class Car:Vehicle                //inherits a base class
{
 
public override void ApplyBrakes()   //override a methods

{
 Conslose.WriteLine(“Stop”);
 Throw 
new Exception(“stop false”);
}

}

}


 

3.2.4 Common Intermediate Language Code
(…….)

3.3 Language Integration


第三章涉及到很多代码的结合讲解,所以我就不详细列举了!


posted @ 2005-11-04 13:11  On the road....  阅读(1517)  评论(4编辑  收藏  举报