.Net Framework 筆記

為了不要傻傻分不清楚 .Net 是什麼東西而做的筆記~

原文出自:http://vb.net-informations.com/framework/framework_tutorials.htm

What is Microsoft .Net Framework

The Microsoft .Net Framework is a platform that provides tools and technologies you need to build Networked Applications as well as Distributed Web Services and Web Applications. The .Net Framework provides the necessary compile time and run-time foundation to build and run any language that conforms to the Common Language Specification (CLS).The main two components of .Net Framework are Common Language Runtime (CLR) and .Net Framework Class Library (FCL).

.NET Framework 最主要的兩部份為:CLR 與 FCL。

The Common Language Runtime (CLR) is the runtime environment of the .Net Framework , that executes and manages all running code like a Virtual Machine. The .Net Framework Class Library (FCL) is a huge collection of language-independent and type-safe reusable classes. The .Net Framework Class Libraries (FCL) are arranged into a logical grouping according to their functionality and usability is called Namespaces. In the following sections describes how to .Net Framework manages the code in compile time and run time .

CLR 是 .Net Framework 的 runtime 環境,它像 VM 一樣執行並管理所有執行中的程式碼。FCL 則是一個巨大且可重複使用的 class。FCL 使用了 namespace 的方式來劃分各個功能。

How to Microsoft .Net Framework

Microsoft .Net Languages Source Code are compiled into Microsoft Intermediate Language (MSIL) . MSIL we can call it as Intermediate Language (IL) or Common Intermediate Language (CIL). Microsoft Intermediate Language (MSIL) is a CPU independent set of instructions that can be converted to the native code. Metadata also created in the course of compile time with Microsoft Intermediate Language (MSIL) and stored it with the compiled code . Metadata is completely self-describing . Metadata is stored in a file called Manifest, and it contains information about the members, types, references and all the other data that the Common Language Runtime (CLR) needs for execution .

.Net 的原始碼會被編譯成中介語言 MSIL,它是一個不依賴處理器的指令集,可以被轉換成 native code。Metadata 也在編譯時與 MSIL 同時被產生,存在一個叫作 manifest 的檔案。它包含了 members/types/references 以及其它 CLR 執行時需要的資訊。

The Common Language Runtime (CLR) uses metadata to locate and load classes, generate native code, provide security, and execute Managed Code. Both Microsoft Intermediate Language (MSIL) and Metadata assembled together is known as Portable Executable (PE) file. Portable Executable (PE) is supposed to be portable across all 32-bit operating systems by Microsoft .Net Framework.

CLR 使用 metadata 來找到並載入 class、產生 native code、提供安全機制、並執行 managed code。MSIL 與 metadata 合起來就是一個 portable 執行檔。Portable 執行檔基本上能在所有擁有 .Net Framework 的 32-bit 機器上執行。

During the runtime the Common Language Runtime (CLR)'s Just In Time (JIT) compiler converts the Microsoft Intermediate Language (MSIL) code into native code to the Operating System. The native code is Operating System independent and this code is known as Managed Code , that is, the language's functionality is managed by the .NET Framework . The Common Language Runtime (CLR) provides various Just In Time (JIT) compilers, and each works on a different architecture depends on Operating Systems, that means the same Microsoft Intermediate Language (MSIL) can be executed on different Operating Systems. In the following section you can see how Common Language Runtime (CLR) functions .

程式執行時,CLR 裡的 JIT 會將 MSIL 轉換為該 OS 下的 managed code -- 這意思是,該程式碼是被 .Net Framework 所管理。CLR 提供了很多不同的 JIT,確保在每個 OS 下都能正常使用。這意謂著 MSIL 可以在不同 OS 上執行。

Common Language Runtime

The Common Language Runtime (CLR) is an Execution Environment . It works as a layer between Operating Systems and the applications written in .Net languages that conforms to the Common Language Specification (CLS). The main function of Common Language Runtime (CLR) is to convert the Managed Code into native code and then execute the Program. The Managed Code compiled only when it needed, that is it converts the appropriate instructions when each function is called . The Common Language Runtime (CLR) 's Just In Time (JIT) compilation converts Intermediate Language (MSIL) to native code on demand at application run time.

CLR 是一個執行環境,它位於 OS 與 .Net 應用程式間,遵守 CLS 的規範。其最主要的功能就是轉換 managed code 成為 native code,並且執行程式。Managed code 只有在被呼叫時才會被轉換。換句話說,這轉換是在執行時才會發生。轉換的工作由 CLR 底下的 JIT 來完成。

During the execution of the program ,the Common Language Runtime (CLR) manages memory, Thread execution, Garbage Collection (GC) , Exception Handling, Common Type System (CTS), code safety verifications, and other system services. The CLR ( Common Language Runtime ) defines the Common Type System (CTS), which is a standard type system used by all .Net languages . That means all .NET programming languages uses the same representation for common Data Types , so Common Language Runtime (CLR) is a language-independent runtime environment . The Common Language Runtime (CLR) environment is also referred to as a managed environment, because during the execution of a program it also controls the interaction with the Operating System. In the coming section you can see what are the main functions of Common Language Runtime (CLR).

程式執行期間,CLR 管理記憶體、thread 執行、garbage 收集、例外處理、CTS、程式碼安生性檢查、以及其它系統服務。CLR 定義了 CTS,它是被所有 .Net 語言使用的標準型別系統。這代表所有 .Net 語言對於共用的資料型態,使用了相同表示法,因此 CLR 是一個與程式語言無關的執行環境。CLR 有時也被叫作 managed environment,因為在程式執行時,CLR 也掌控它與 OS 間的溝通。

Just In Time Compiler

The .Net languages , which is conforms to the Common Language Specification (CLS), uses its corresponding runtime to run the application on different Operating Systems . During the code execution time, the Managed Code compiled only when it is needed, that is it converts the appropriate instructions to the native code for execution just before when each function is called. This process is called Just In Time (JIT) compilation, also known as Dynamic Translation . With the help of Just In Time Compiler (JIT) the Common Language Runtime (CLR) doing these tasks.

.Net 遵守 CTS 規範,使用它對應的 runtime,在不同 OS 下執行程式。程式執行時,只有被使用到的 managed code 才會被轉成 native code。這個步驟稱作 JIT 編譯,也稱作 dynamic translation。

The Common Language Runtime (CLR) provides various Just In Time compilers (JIT) and each works on a different architecture depending on Operating System. That is why the same Microsoft Intermediate Language (MSIL) can be executed on different Operating Systems without rewrite the source code. Just In Time (JIT) compilation preserves memory and save time during application initialization. Just In Time (JIT) compilation is used to run at high speed, after an initial phase of slow interpretation. Just In Time Compiler (JIT) code generally offers far better performance than interpreters.

CLR 提供許多可以工作於不同 OS 下的 JIT。這就是為什麼同一個 MSIL,不用修改程式碼,就能在不同 OS 下執行的原因。

Microsoft Intermediate Language

MSIL stands for Microsoft Intermediate Language. We can call it as Intermediate Language (IL) or Common Intermediate Language (CIL). During the compile time , the compiler convert the source code into Microsoft Intermediate Language (MSIL) .Microsoft Intermediate Language (MSIL) is a CPU-independent set of instructions that can be efficiently converted to the native code. During the runtime the Common Language Runtime (CLR)'s Just In Time (JIT) compiler converts the Microsoft Intermediate Language (MSIL) code into native code to the Operating System.

編譯時,compiler 將程式原始碼轉成 MSIL。它是一個與處理器無關的指令集,可以很有效率地被轉換成 native code。執行時 CLR 裡的 JIT 會將 MSIL 轉換成 OS 的 native code。

When a compiler produces Microsoft Intermediate Language (MSIL), it also produces Metadata. The Microsoft Intermediate Language (MSIL) and Metadata are contained in a portable executable (PE) file . Microsoft Intermediate Language (MSIL) includes instructions for loading, storing, initializing, and calling methods on objects, as well as instructions for arithmetic and logical operations, control flow, direct memory access, exception handling, and other operations

它 compiler 產生 MSIL 時,也同時產生 metadata。MSIL 與 metadata 組合起來就稱作 portable 執行檔。MSIL 包括了物件載入、儲存、初始化與呼叫方式的指令。

Managed Code

Managed Code in Microsoft .Net Framework, is the code that has executed by the Common Language Runtime (CLR) environment. On the other hand Unmanaged Code is directly executed by the computer's CPU. Data types, error-handling mechanisms, creation and destruction rules, and design guidelines vary between managed and unmanaged object models.

Managed code 就是需要 CLR 才能跑的 code,而 unmanaged code 就是直接被處理器執行的 code。

The benefits of Managed Code include programmers convenience and enhanced security . Managed code is designed to be more reliable and robust than unmanaged code , examples are Garbage Collection , Type Safety etc. The Managed Code running in a Common Language Runtime (CLR) cannot be accessed outside the runtime environment as well as cannot call directly from outside the runtime environment. This makes the programs more isolated and at the same time computers are more secure . Unmanaged Code can bypass the .NET Framework and make direct calls to the Operating System. Calling unmanaged code presents a major security risk.

使用 managed code 的優點包含了 programmer 的便利性及加強的安全性。Managed code 被設計來比 unmanaged code 更可靠與健全。Managed code 不能被 CLR 外部存取及呼叫。這使得程式更獨立。Unmanaged code 可以略過 .Net Frmaework 並直接呼叫 OS,這是比較有安全疑慮的方式。

.Net Metadata

Metadata in .Net is binary information which describes the characteristics of a resource . This information include Description of the Assembly , Data Types and members with their declarations and implementations, references to other types and members , Security permissions etc. A module's metadata contains everything that needed to interact with another module.

Metadata 描述程式資源特性,包含了 assembly 描述、資料型別與成員的宣告與實作、其它型別或成員的參考、安全權限…等等。一個 module 的 metadata 包含所有與其它 module 溝通的資訊。

During the compile time Metadata created with Microsoft Intermediate Language (MSIL) and stored in a file called a Manifest . Both Metadata and Microsoft Intermediate Language (MSIL) together wrapped in a Portable Executable (PE) file. During the runtime of a program Just In Time (JIT) compiler of the Common Language Runtime (CLR) uses the Metadata and converts Microsoft Intermediate Language (MSIL) into native code. When code is executed, the runtime loads metadata into memory and references it to discover information about your code's classes, members, inheritance, and so on. Moreover Metadata eliminating the need for Interface Definition Language (IDL) files, header files, or any external method of component reference.

.Net Namespaces

Namespaces are the way to organize .NET Framework Class Library into a logical grouping according to their functionality, usability as well as category they should belong to, or we can say Namespaces are logical grouping of types for the purpose of identification.

Namespace 是 .Net Framekwork 用來組織 FCL 的方式。

The .NET Framework Class Library (FCL ) is a large collection of thousands of Classes. These Classes are organized in a hierarchical tree. The System Namespaces is the root for types in the .NET Framework. We can uniquely identify any Class in the .NET Framework Class Library (FCL ) by using the full Namespaces of the class .In .Net languages every program is created with a default Namespaces . Programmers can also create their own Namespaces in .Net languages.

留言

熱門文章