1 / 24

.NET 框架 2.0 新特性综述

.NET 框架 2.0 新特性综述. 卢斌 Software Development Engineer Microsoft Corporation. BCL 新功能. Serial port Compression Strongly typed resources Console support Threading Diagnostics Networking 。。。. 演 示. Strongly Typed Resources. Generics “ 泛型 ”. C#. public class List {

pnovello
Download Presentation

.NET 框架 2.0 新特性综述

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. .NET框架2.0新特性综述 卢斌 Software Development Engineer Microsoft Corporation

  2. BCL 新功能 • Serial port • Compression • Strongly typed resources • Console support • Threading • Diagnostics • Networking • 。。。

  3. 演 示 Strongly Typed Resources

  4. Generics “泛型” C# public class List { private object[] elements; private int count; public void Add(object element) { if (count == elements.Length) Resize(count * 2); elements[count++] = element; } public object this[int index] { get { return elements[index]; } set { elements[index] = value; } } public int Count { get { return count; } } } public class List<T> { private T[] elements; private int count; public void Add(T element) { if (count == elements.Length) Resize(count * 2); elements[count++] = element; } public T this[int index] { get { return elements[index]; } set { elements[index] = value; } } public int Count { get { return count; } } } List intList = new List(); intList.Add(1); intList.Add(2); intList.Add(“3"); int i = (int)intList[0]; List intList = new List(); intList.Add(1); // Argument is boxed intList.Add(2); // Argument is boxed intList.Add(“3"); // Should be an error int i = (int)intList[0]; // Cast required List<int> intList = new List<int>(); intList.Add(1); // No boxing intList.Add(2); // No boxing intList.Add(“3"); // Compile-time error int i = intList[0]; // No cast required

  5. Generics “泛型” • 为什么要Generics? • 编译时类型验证 • 高性能 (不用装箱box, 不用转换downcasts) • 减少代码累赘 (typed collections) • VB, C#, MC++ 编写和运用 generics • Generics国际标准化

  6. Generics in VB VB Public Class List(Of ItemType)      Private elements() As ItemType      Private elementcount As Integer      Public Sub Add(ByVal element As ItemType)           If elementcount = elements.Length Then Resize(elementcount * 2)           elements(elementcount) = element           count += 1      End Sub      Public Default Property Item(ByVal index As Integer) As ItemType           Get               Return elements(index)           End Get           Set (ByVal Value As ItemType)              elements(index) = Value           End Set      End Property      Public ReadOnly Property Count As Integer           Get               Return elementcount           End Get      End Property End Class Dim intList As New List(Of Integer) intList.Add(1)           ‘ No boxing intList.Add(2)           ‘ No boxing intList.Add(“3")     ‘ Compile-time error Dim i As Integer = intList(0)  ’ No cast required

  7. Generics in C++ C++ generic<typename T> public ref class List {    array<T>^ elements;    int count; public:    void Add(T element) {       if (count == elements->Length) Resize(count * 2);       elements[count++] = element;    }    property T default [int index] { T get() { return elements[index]; }       void set(T value) { elements[index] = value; }    }    property int Count {       int get() { return count; }    } }; List<int>^ intList = gcnew List<int>(); intList->Add(1);           // No boxing intList->Add(2);           // No boxing intList->Add(“3");     // Compile-time error int i = intList[0];        // No cast required

  8. 基础类库泛类 (Generics in BCL) • System.Collections.Generic classes List<T> Dictionary<K, V> Stack<T> Queue<T> • System.Collections.Generic interfaces IList<T> IDictionary<K, V> IDictionary<K, V> ICollection<T> IEnumerable<T> IEnumerator<T> IComparable<T> IComparer<T> • Nullable(Of T) • EventHandler<T> Dim intVal as Nullable(Of Integer) = 5 If intVal.HasValue Then ‘ checks for a value delegate voidEventHandler<T>(Object sender, T e) where T : EventArgs;

  9. 提高 CLR 性能 长远目标: CLR性能指标与非托管本机代码一致 • 减少多个托管进程的marginal cost • 减少托管应用程序起动时间和工作集 NGEN • 将 IL 编译成本机代码, 然后存盘 • 好处: 无需反复将 IL 编译成本机代码, 类的布局也已定型, 起动时间更短 • CLR 2.0:显著地减少了private, non-shareable工作集 • ngen install, ngen update, ngen /queue

  10. 使.NET框架成为更快的编程平台 提高现有API的性能 • 降低应用程序域(AppDomain)开销,应用程序域之间的方法调用 (加快 ~1.1倍-50倍) • 委托(delegate)创建 (~10倍) 和调用 (~2倍) • UTF8Encoding: translation (~2.5倍) 新的高性能 API • 更快的资源查找API • Lightweight CodeGen: 只生成关键代码 (与 Reflect Emit 对比)

  11. 演 示 TryParse

  12. 走向 64 位 • 64 位 Windows 服务器和工作站 • 支持 IA64 和 X64 (AMD64) • Windows Server 2003 SP1和未来的64 位 Windows Client • 支持 WoW64 • 32位应用程序可在 64位机上运行 • CLR1.0 和1.1应用程序在 WoW64上运行 • VS: 作为32位应用程序运行 • 可以开发, 调试和部署32位和64位应用程序 • 新的CLR 64位实时编译, 垃圾回收和程序调试服务

  13. YES YES NO PE32+ NO ILONLY 64bit NO WOW64 YES 32BITREQUIRED • C# Compiler /platform: • <AnyCPU>: ILONLY, NOT 32BITREQUIRED • <x86>: 32BITREQUIRED • <Itanium> or <x64>: PE32+

  14. 演 示 Space Invaders

  15. CLR安全功能 • 支持新的加密服务(cryptography) • 支持 PKI and PKCS7 • 支持 XML 加密 • 更好地支持 X509 certificates • DPAPI (Data Protection API) • 支持托管ACL • 提高应用程序安全 • Permission 计算器 (PermCalc) • Debug-In-Zone

  16. 演 示 SecureString

  17. RAD程序调试 • 编辑-继续能力 (Edit and Continue) 允许的编辑: (例子) • 给类加新的private字段 • 给类加新的private非虚拟方法 • 改写方法内的代码 不允许的编辑: (例子) • 除去字段或方法 • 编辑泛类 (generics) • 序列化(Serialization)不认知新的字段 • Display Attributes 控制调试器显示

  18. SQL Server 集成 • 将开发工具 Visual Studio 与数据管理平台 SQL Server 集成在一起 • 将中间层的.NET框架编程模式和开发技能应用到数据层 • 用托管代码写 Stored Procedures, Triggers, data types • CLR安全系统(类型安全和代码访问安全)

  19. VB, C#, … Build 文件集: geo.dll SQL 数据: create assembly … create function … create procedure … create trigger … create type … SQL Server SQL Queries: SELECT name FROM Supplier WHERE Location::Distance ( @point ) < 3 SQL CLR 功能 VS .NET 项目 定义 Location.Distance() SQL Server 承载 CLR

  20. Sql 编程模式 分割字符串 (Splitting a string) T-SQL • 老方法 (T-SQL)…. declare @str varchar(200) select @Str = 'Microsoft Corporation|SQL Server|2003|SQL-CLR|2002-08-20|11:32:00|Document|3.b.3' SELECT substring(@Str + '|', 0 + 1, charindex('|', @Str + '|', 0 + 1) - 0 - 1 ), substring(@Str + '|', charindex('|', @Str + '|') + 1, charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) - charindex('|', @Str + '|') - 1 ), substring(@Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) + 1, charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) + 1) - charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) - 1 ), substring(@Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) + 1) + 1, charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) + 1) + 1) - charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) + 1) - 1 ), substring(@Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) + 1) + 1) + 1, charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) + 1) + 1) + 1) - charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) + 1) + 1) - 1 ), substring(@Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) + 1) + 1) + 1) + 1, charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) + 1) + 1) + 1) + 1) - charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) + 1) + 1) + 1) - 1 ), substring(@Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) + 1) + 1) + 1) + 1) + 1, charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) + 1) + 1) + 1) + 1) + 1) - charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) + 1) + 1) + 1) + 1) - 1 ), substring(@Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) + 1) + 1) + 1) + 1) + 1) + 1, charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) + 1) + 1) + 1) + 1) + 1) + 1) - charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) + 1) + 1) + 1) + 1) + 1) - 1 ) declare @str varchar(200) select @Str = 'Microsoft Corporation|SQL Server|2003|SQL-CLR|2002-08-20|11:32:00|Document|3.b.3' SELECT substring(@Str + '|', 0 + 1, charindex('|', @Str + '|', 0 + 1) - 0 - 1 ), substring(@Str + '|', charindex('|', @Str + '|') + 1, charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) - charindex('|', @Str + '|') - 1 ), substring(@Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) + 1, charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) + 1) - charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) - 1 ), substring(@Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) + 1) + 1, charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) + 1) + 1) - charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) + 1) - 1 ), substring(@Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) + 1) + 1) + 1, charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) + 1) + 1) + 1) - charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) + 1) + 1) - 1 ), substring(@Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) + 1) + 1) + 1) + 1, charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) + 1) + 1) + 1) + 1) - charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) + 1) + 1) + 1) - 1 ), substring(@Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) + 1) + 1) + 1) + 1) + 1, charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) + 1) + 1) + 1) + 1) + 1) - charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) + 1) + 1) + 1) + 1) - 1 ), substring(@Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) + 1) + 1) + 1) + 1) + 1) + 1, charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) + 1) + 1) + 1) + 1) + 1) + 1) - charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|', charindex('|', @Str + '|') + 1) + 1) + 1) + 1) + 1) + 1) - 1 )

  21. Sql 编程模式 分割字符串 (Splitting a string) VB 新方法… Public Shared Sub SplitString() Dim s As String s = "Microsoft Corporation|SQL Server|2003|SQL-CLR|2002-08- 20|11:32:00|Document|3.b.3" Dim myArray() As String = Split(s, "|") End Sub

  22. 更多资源 • MSDN Dev Center http://msdn.microsoft.com/developercenters/ • GotDotNet http://www.gotdotnet.com • Microsoft Technical Communities http://www.microsoft.com/communities/default.mspx • CLR Application Compatibility Lab www.gotdotnet.com/team/changeinfo/compatibility/

  23. Your Feedbackis Important!

More Related