1. 程式人生 > >讀書筆記: C# 7.0 in a nutshell (第 五 章 Framework Overview)

讀書筆記: C# 7.0 in a nutshell (第 五 章 Framework Overview)

內容:

第五章 框架總覽

  1. Overview
  2. .NET Standard 2.0
  3. CLR 和核心框架
  4. 應用技術

1. Overview

幾乎所有 .NET 框架的作用都通過一系列的 Managed Types暴露出來。 這些 types 組織在一層層的 namespace中,然後別打包進assembly, 連通 CLR 一起構成 .NET 平臺。

有些.NET 型別是直接被CLR使用的,對管理 hosting environment有重要作用。這些程式碼都在mscorlib.dll這個程式集中, 包括C#的內建類集合類流處理類序列化反射執行緒

native interoperability

mscorlib 是 Multi-language Standard Common Object Runtime Library

再往上一層,是基於這些類的來提供更多的功能的一些類,包括XML, 網路LINQ。 它們在System.dllSystem.Core.dll,System.Xml.dll中,連通mscorlib一起提供更豐富的程式設計環境。 .NET框架其他的功能都是基於這些之上的。

其他的功能主要都是包括一些Apllied API, 這其中大多數包含了下面3方面的功能:

  1. UI技術
  2. 後端技術
  3. 分散式系統技術
C# version CLR version .NET Framework versions
1.0 1.0 1.0
1.2 1.1 1.1
2.0 2.0 2.0, 3.0
3.0 2.0 (SP2) 3.5
4.0 4.0 4.0
5.0 4.5 (Patched CLR 4.0) 4.5
6.0 4.6 (Patched CLR 4.0) 4.6
7.0 4.6/4.7 (Patched CLR 4.0) 4.6/4.7

大部分的核心型別都定義在以下這些程式集中:

  • mscorlib.dll
  • System.dll
  • System.Core.dll

第一個mscorlib.dll包含了執行時環境需求的型別; 而後面2個包含了其他程式設計師需要的型別。

The reason the latter two are separate is historical: when Microsoft introduced Framework 3.5, they made it additive insofar as it ran as a layer over the existing CLR 2.0. Therefore, almost all new core types (such as the classes supporting LINQ) went into a new assembly that Microsoft called System.Core.dll.

1.1 What’s New in .NET 4.6

  • The Garbage Collector (GC) offers more control over when (not) to collect via new methods on the GC class. There are also more fine-tuning options when calling GC.Collect.
  • There’s a brand-new faster 64-bit JIT compiler.
  • The System.Numerics namespace now includes hardware-accelerated matrix, vector types, BigInteger and Complex.
  • There’s a new System.AppContext class, designed to give library authors a consistent mechanism for letting consumers switch new API features in or out.
  • Tasks now pick up the current thread’s culture and UI culture when created.
  • More collection types now implement IReadOnlyCollection.
  • WPF has further improvements, including better touch and high-DPI handling.
  • ASP.NET now supports HTTP/2 and the Token Binding Protocol in Windows 10

1.2 What’s New in .NET 4.7

  • The System.ValueTuple struct is part of Framework 4.7, so you can use tuples in C# 7 without referencing the System.ValueTuple.dll assembly.
  • WPF has better touch support.
  • Windows Forms has better support for high-DPI monitors.

2 .NET Standard 2.0

Chap1中,描述了 .NET框架其他的3個為了跨平臺開發的 alternatives:

  • UWP for Windows 10 device/desktop
  • .NET Core/ASP.NET Core for Windows, Linux and MacOS
  • Xamarin for mobile devices (iOS, Android, and Windows 10 devices)

好訊息是隨著 .NET Core 2.0,這些框架包括.NET Framework 4.6.1以及之後的版本, 都已經融合進了它的核心功能中,並且都提供了一個base class library(BCL) ,包含類似的 type 和member。這種共同性被正式稱為 .NET Standard 2.0

當使用 VS 2017寫library的時候,可以選擇目標為 .NET Standard 2.0,而不是指定某個特別的框架。 這樣這個library就是 可移植的,這個程式集就可以再各個不同的平臺執行。

2.1 老的 .NET Standard

2.0以前也有.NET Standard:

If you target… You also support…
Standard 1.6 .NET Core 1.0
Standard 1.3 Above plus .NET 4.6.0
Standard 1.2 Above plus .NET 4.5.1, Windows Phone 8.1, WinRT for Windows 8.1
Standard 1.1 Above plus .NET 4.5.0, Windows Phone 8.0, WinRT for Windows 8.0

數字更大的標準,是一個更嚴格的超集。

2.2 Reference Assemblies

寫程式的時候,需要在編譯的時候,引入程式中用到的assemblies. 但是引入的assemblies, 只需要在編譯的時候存在,並不一定要求和執行時的一致。所以可以建立一個空的程式集,裡面沒有被編譯的程式碼。

This is how .NET Standard works: you add a reference assembly called netstandard.dll, which contains all of the allowable types and members in .NET Standard 2.0 (but no actual compiled code). Then, through assembly redirection attributes, the “real” assemblies are loaded at runtime. (The choice of “real” assemblies will depend on which framework the assembly eventually runs on.)

3. The CLR and Core Framework

3.1 System Types

System名稱空間包含大多數基本型別,包括了

  • C# built-in types
  • Exception基類
  • Enum
  • Array
  • Delegate基類
  • Nullable
  • Type
  • DateTime
  • TimeSpan
  • Guid
  • Math
  • Random
  • Convert
  • BItConverter
  • IDisposable

3.2 Text Processing

System.Text名稱空間包括

  • StringBuilder (string型別的 mutable cousin)
  • Encoding 及其子類

System.Text.RegularExpression包含正則

3.3 Collections

System.Collections // Nongeneric collections System.Collections.Generic // Generic collections System.Collections.Specialized // Strongly typed collections System.Collections.ObjectModel // Bases for your own collections System.Collections.Concurrent // Thread-safe collection (Chapter 23)

3.4 Queries

LINQ:

System.Linq// LINQ to Objects and PLINQ System.Linq.Expressions // For building expressions manually System.Xml.Linq// LINQ to XML System.Data.Linq // LINQ to SQL System.Data.Entity // LINQ to Entities (Entity Framework)

3.5 XML

Linq to XML, XMLReader, 老DOM, XSD, XML schema, stylesheet, XPATH

System.Xml// XmlReader, XmlWriter + the old W3C DOM System.Xml.Linq// The LINQ to XML DOM System.Xml.Schema // Support for XSD System.Xml.Serialization // Declarative XML serialization for .NET types System.Xml.XPath// XPath query language System.Xml.Xsl // Stylesheet support

3.6 Diagnostics

logging and assertion 功能, 描述了:

  • 和其他程序互動
  • 寫到Windows event log
  • use performance counters for monitoring

System.Diag nostics

3.7 Concurrency and Asynchrony

multithreading, task, async.

System.Threading System.Threading.Tasks

3.8 Streams and I/O

基於流的輸入輸出

System.IO .NET IO和Stream Windows.Storage WinRT file IO

3.9 Networking

System.Net System.Net.Http // HttpClient System.Net.Mail // For sending mail via SMTP System.Net.Sockets // TCP, UDP, and IP

上面的後2個對於Win8平臺沒法使用

Windows.Networking.Sockets WinRT的socket互動

3.10 Serialization

從二進位制存取, 對於分散式的技術非常有用,比如WCF, Web Services, and Remoting

  • data contract serializer
  • binary serializer
  • XML serializer
  • JSON serializer

System.Runtime.Serialization System.Xml.Serialization

3.11 Assemblies, Reflection, and Attributes

System System.Reflection System.Reflection.Emit // .NET Framework only

3.12 Dynamic Programming

System.Dynamic

3.13 Security

System.Security System.Security.Permissions System.Security.Policy System.Security.Cryptography

3.14 Advanced Threading

signaling constructs, thread-local storage, reader/writer locks, and so on

System.Threading

3.15 Parallel Programming

3.16 Application Domains

The CLR provides an additional level of isolation within a process, called an application domain

Creating separate application domains is not part of .NET Standard 2.0, although you can interact with the current domain via the AppDomain class in the System namespace.

3.17 Native and COM Interoperability

System.Runtime.InteropServices

4. Applied Technologies

User-Interface API

  • 瘦客戶端:
    • ASP.NET and ASP.NET Core
  • 胖客戶端:
    • WPF+Winform for Windows7/8/10
    • UWP for Windows desktop and device
    • Xamarin for mobile phone

* ASP.NET: System.Web.UI及其子名稱空間,包含在System.Web.dll中 * ASP.NET Core: 輕量,不用System.Web.dll * WPF (Windows Presentation Foundation): System.Windows及其子名稱空間,除了System.Windows.Forms名稱空間 * WinForm: System.Windows.FormsSystem.Drawing名稱空間,包含在System.Windows.Forms.dllSystem.Drawing.dll * Xamarin: * UWP (Universal Windows Platform):Windows.UI and Windows.UI.Xaml

Backend Technologies

  • ADO.NET
  • Entity Framework (.NET Framework only)
  • Entity Framework Core (.NET Framework and .NET Core)
  • LINQ to SQL (.NET Framework only)

後面3個基於 ADO.NET的Provider layer

  • Windows Workflow (.NET Framework only) : System.WorkFlowSystem.Activities
  • COM+ and MSMQ (.NET Framework only)

Distributed System Technologies

  • WCF (Windows Communication Foundation) : 用於替代Remoting和 (.ASMX) Web Service
  • Web API : 基於ASP.NET/ ASP.NET Core
  • Remoting and .ASMX Web Services (.NET Framework only)