Neues vom .net Framework 4 - download.microsoft.comdownload.microsoft.com/.../CeBIT2012_NeuesVomNETFramework4.5.pdf•Porting existing C# and VB code to the profile should be easy

Post on 12-Apr-2020

2 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

Transcript

Neues vom .net Framework 4.5

Patric Boscolo, Developer Evangelist, patbosc@microsoft.com, http://blogs.msdn.com/patricb, @patricsmsdn

Quick Reminder .net Framework Principles

Profile for Metro Style Apps

What else? Let‘s talk about the how

Windows Runtime Types

Managed Types

Mapped Types

Interop Helpers 99% of Windows* namespace

Tuple, List<T>, XDocument, DataContract, etc.

Int32, String, Boolean, IEnumerable<T>, IList<T>, Uri, etc.

IBuffer / Byte[] IAsync* / Task* InputStream, OutputStream, …

using Windows.Storage; using System; using System.IO; using System.Threading.Tasks; class Sample { static async Task WriteAsync(StorageFolder wrtfolder, string filename, string text) { var wrtFile = await wrtFolder.CreateFileAsync(filename); var wrtStream = await wrtFile.OpenAsync(FileAccessMode.ReadWrite); using (Stream stream = wrtStream.OpenWrite()) { using (var writer = new StreamWriter(stream)) { writer.WriteLine(text); } } } }

native type native method

managed type returned from a

seemingly native method

managed argument passed

to native API

native namespace

C# feature

AWESOME WINDWOS 8 WINDOWS RT .net for Metro Style

Apps

.net Profile for Metro Style Apps

Design Requirements

• Remove APIs not applicable to Metro style apps • e.g. Console, ASP.NET

• Remove dangerous, obsolete, and legacy APIs • Remove duplication (within .NET APIs and with WRT APIs)

• e.g. ArrayList (with List<T>) and XML DOM (with WR XML API)

• Remove APIs that are OS API wrappers • e.g. EventLog, Performance Counters

• Remove badly designed APIs • e.g. APIs that are confusing, don’t follow basic design guidelines, cause bad dependencies

Compatibility Requirements• Running existing .NET applications as-is on the .NET profile for

Metro style apps is not an objective

• Porting existing C# and VB code to the profile should be easy

• Existing .NET developers should feel at home with this profile

• Code compiled to the profile must be able to execute on .NET Framework 4.5

• Code compiled to the portable subset needs to be compatible with this profile

* Refers to implementation assemblies, not reference assemblies. More on this later.

.NET for Metro style apps

BCL

HTTP

XML

Serialization

WCF

MEF

Main Namespaces

System System.Collections* System.ComponentModel System.ComponentModel.Composition* System.Diagnostics* System.Dynamic System.Globalization System.IO System.Linq* System.Net* System.Reflection System.Runtime.InteropServices* System.Runtime.Serialization* System.ServiceModel* System.Text* System.Threading System.Threading.Tasks System.Xml.* System.Xml.Linq

System Namespace* Simplicity Action Action<...> Activator Array ArraySegment<T> AsyncCallback Attribute AttributeTargets AttributeUsageAttribute BitConverter Boolean Byte Char CLSCompliantAttribute Convert DateTime DateTimeKind DateTimeOffset DayOfWeek Decimal Delegate Double Enum Environment EventArgs EventHandler EventHandler<T> Exception FlagsAttribute Func<...>

GC Guid IAsyncResult IComparable<T> ICustomFormatter IDisposable IEquatable<T> IFormatProvider IFormattable Int16 Int32 Int64 IntPtr IObservable<T> IObserver<T> Lazy<T> Math MidpointRounding MulticastDelegate Nullable Nullable<T> Object ObsoleteAttribute ParamArrayAttribute Predicate<T> Random RuntimeArgumentHandle RuntimeFieldHandle RuntimeMethodHandle RuntimeTypeHandle

SByte Single String StringComparer StringComparison StringSplitOptions ThreadStaticAttribute TimeSpan TimeZoneInfo Tuple Tuple<...> Type TypedReference UInt16 UInt32 UInt64 UIntPtr Uri UriBuilder UriComponents UriFormat UriKind ValueType Version Void WeakReference WeakReference<T>

* excluding exceptions

Removed, Replaced, and Changed

• System.Web

• System.Data

• System.Runtime.Remoting

• System.Reflection.Emit

• Private reflection

• Application Domains

Removed, Replaced, and Changed

Existing Technology New Substitute

System.Windows Windows.UI.Xaml

System.Security.IsolatedStorage Windows.Storage.ApplicationData

System.Resources Windows.ApplicationModel.Resources

System.Net.Sockets Windows.Networking.Sockets

System.Net.WebClient Windows.Networking.BackgroundTransfer and System.Net.HttpClient

Existing Technology Changes

Serialization • Use Data Contract for general serialization needs • Use XML serialization for fine control of the XML

stream

Reflection • System.Type now represents a type reference • System.Reflection.TypeInfo is the new System.Type

XML • XML Linq is the main XML parser • Use XmlReader/Writer as a low level abstraction

Collections • Non generic collections gone • New collection abstractions added: IReadOnlyList<T>

Threading • Synchronization primitives mainly unchanged • Thread control in Windows.Foundation.Threading • Task.Run is the new high level way of doing

background processing

Async Model • !!!!

Removed, Replaced, and Changed

Porting Guide: Example Topic Reflection System.Type has become a type reference and only contains basic information. The majority of the reflection API's are now on System.Reflection.TypeInfo. You can get a TypeInfo from a Type by using the GetTypeInfo() extension method when you include a using statement for the System.Reflection namespace.

Existing .NET Code Replacement Code Notes type.Assembly type.GetTypeInfo().Assembly

type.GetMethods(BindingFlags.DeclaredOnly) type.GetTypeInfo().DeclaredMethods

type.GetMethod("MethodName", BindingFlags.DeclaredOnly)

type.GetTypeInfo().GetDeclaredMethod("MethodName")

These samples are the same for Properties, Events, and Fields.

type.GetMethod("MethodName") type.GetRuntimeMethod("MethodName") GetRuntimeMethod API in Beta.

type.GetMethods() type.GetRuntimeMethods() GetRuntimeMethods() API in Beta. type.GetMethods(BindingFlags.Instance|BindingFlags.Public)

type.GetRuntimeMethods().Where(m => !m.IsStatic && m.IsPublic)

GetRuntimeMethods() API in Beta.

BindingFlags.NonPublic !member.IsPublic You can also use member.IsAssembly => internal or member.IsFamily => protected for more flexibility

ASYNC

.net 4.5 & Visual Studio 11

Parallel Pattern Library Task Parallel Library

PLINQ

Managed Native Key:

Windows Operating System

Runtime

Programming Models

CLR ThreadPool

Task Scheduler

Resource Manager

Data Stru

ctures D

ata

Stru

ctu

res

Tools

Tooling

Parallel Debugger

Concurrency Visualizer

Async Agents Library

Stacks

Tasks

Watch

CPU

Threads

Cores

C#/VB/F#

Async Dataflow C++ AMP

DirectX

GPU

CPU GPU

ConcRT

Task Scheduler

Resource Manager

New Updated

Async Implementation

Task Representation as Feature in .net and Languages • Produce and Consume Async Operations

When you write a Async Method • You Producing a Task<T> or Results Task<T>

When you use Await • You await Task<T> or Results Task<T>

We all “know” sync methods are “cheap” – Years of optimizations around sync methods – Enables refactoring at will

Mental Model

public static void SimpleBody() { Console.WriteLine("Hello, Async World!"); }

.method public hidebysig static void SimpleBody() cil managed { .maxstack 8 L_0000: ldstr "Hello, Async World!" L_0005: call void [mscorlib]System.Console::WriteLine(string) L_000a: ret }

Not so for asynchronous methods

Mental Model, cont.

public static async Task SimpleBody() { Console.WriteLine("Hello, Async World!"); }

.method public hidebysig static class [mscorlib]System.Threading.Tasks.Task SimpleBody() cil managed { .custom instance void [mscorlib]System.Diagnostics.DebuggerStepThroughAttribute::.ctor() = ( 01 00 00 00 ) // Code size 32 (0x20) .maxstack 2 .locals init ([0] valuetype Program/'<SimpleBody>d__0' V_0) IL_0000: ldloca.s V_0 IL_0002: call valuetype [mscorlib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder [mscorlib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder::Create() IL_0007: stfld valuetype [mscorlib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder Program/'<SimpleBody>d__0'::'<>t__builder' IL_000c: ldloca.s V_0 IL_000e: call instance void Program/'<SimpleBody>d__0'::MoveNext() IL_0013: ldloca.s V_0 IL_0015: ldflda valuetype [mscorlib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder Program/'<SimpleBody>d__0'::'<>t__builder' IL_001a: call instance class [mscorlib]System.Threading.Tasks.Task [mscorlib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder::get_Task() IL_001f: ret }

.method public hidebysig instance void MoveNext() cil managed { // Code size 66 (0x42) .maxstack 2 .locals init ([0] bool '<>t__doFinallyBodies', [1] class [mscorlib]System.Exception '<>t__ex') .try { IL_0000: ldc.i4.1 IL_0001: stloc.0 IL_0002: ldarg.0 IL_0003: ldfld int32 Program/'<SimpleBody>d__0'::'<>1__state' IL_0008: ldc.i4.m1 IL_0009: bne.un.s IL_000d IL_000b: leave.s IL_0041 IL_000d: ldstr "Hello, Async World!" IL_0012: call void [mscorlib]System.Console::WriteLine(string) IL_0017: leave.s IL_002f } catch [mscorlib]System.Exception { IL_0019: stloc.1 IL_001a: ldarg.0 IL_001b: ldc.i4.m1 IL_001c: stfld int32 Program/'<SimpleBody>d__0'::'<>1__state' IL_0021: ldarg.0 IL_0022: ldflda valuetype [mscorlib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder Program/'<SimpleBody>d__0'::'<>t__builder' IL_0027: ldloc.1 IL_0028: call instance void [mscorlib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder::SetException( class [mscorlib]System.Exception) IL_002d: leave.s IL_0041 } IL_002f: ldarg.0 IL_0030: ldc.i4.m1 IL_0031: stfld int32 Program/'<SimpleBody>d__0'::'<>1__state' IL_0036: ldarg.0 IL_0037: ldflda valuetype [mscorlib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder Program/'<SimpleBody>d__0'::'<>t__builder' IL_003c: call instance void [mscorlib]System.Runtime.CompilerServices.AsyncTaskMethodBuilder::SetResult() IL_0041: ret }

Garbage Collection

Managed Profile Guided Optimization (for .net Applications)

More Information patbosc@microsoft.com

Twitter: @patricsmsdn

Blog: http://blogs.msdn.com/patricb

Search for:

ASYNC -> Stephen Toub

.GC -> Pracheeti Nagarkar

MPGO -> Mark Miller

.net -> BCL Team Blog & Krzysztof Cwalina, Immo Landwerth and Joshua Goodman

top related