calculator calc = getcalculator(); int sum = calc.add(10, 20); calculator calc = getcalculator();...

43
Dynamic Binding in C# 4.0 Mads Torgersen C# Language PM Microsoft Corporation FT31

Upload: maude-daniel

Post on 31-Dec-2015

323 views

Category:

Documents


4 download

TRANSCRIPT

Dynamic Binding in C# 4.0

Mads TorgersenC# Language PMMicrosoft Corporation

FT31

Dynamic in C#

> The Dynamic Language Runtime (DLR)

> dynamic in C#

> Designing dynamic

The DLR on the CLR

> Common Language Runtime – CLR:> Common platform for static languages> Facilitates great interop

The DLR on the CLR

> Common Language Runtime – CLR:> Common platform for static languages> Facilitates great interop

> Dynamic Language Runtime – DLR:> Common platform for dynamic languages> Facilitates great interop

The DLR on the CLR

> Common Language Runtime – CLR:> Common platform for static languages> Facilitates great interop

> Dynamic Language Runtime – DLR:> Common platform for dynamic languages> Facilitates great interop

Dynamic Objects

> Implement their own binding

> The DLR caches and optimizes

> Built by dynamic languages – or you!

Why Dynamic in C#?

> Build on DLR opportunity

> Use code from dynamic languages

> Use other dynamic object models

> Better COM interop

The Dynamic Type in C#

Calculator calc = GetCalculator();int sum = calc.Add(10, 20);

object calc = GetCalculator();Type calcType = calc.GetType();object res = calcType.InvokeMember("Add", BindingFlags.InvokeMethod, null, new object[] { 10, 20 });int sum = Convert.ToInt32(res);

ScriptObject calc = GetCalculator();object res = calc.Invoke("Add", 10, 20);int sum = Convert.ToInt32(res);

dynamic calc = GetCalculator();int sum = calc.Add(10, 20);

Statically typed to be dynamic

Dynamic method

invocation

Dynamic conversion

C# Dynamic

demo

Designing Dynamic

> C# is not a dynamic language!

> Dynamic is a dangerous foreign element

> Should be syntactically explicit

Static Example

string[] strings = GetStrings();

string last = strings[strings.Length – 1];

Explicit Dynamic Operations

object strings = GetDynamicObject();

string last = strings[strings.Length – 1];

Explicit Dynamic Operations

object strings = GetDynamicObject();

string last = strings[strings~.Length – 1];

Explicit Dynamic Operations

object strings = GetDynamicObject();

string last = strings~[strings~.Length – 1];

Explicit Dynamic Operations

object strings = GetDynamicObject();

string last = strings~[strings~.Length ~– 1];

Explicit Dynamic Operations

object strings = GetDynamicObject();

string last = (string)strings~[strings~.Length ~– 1];

Explicit Dynamic Operations

object strings = GetDynamicObject();

string last = ~(string)strings~[strings~.Length ~– 1];

Explicit Dynamic Operations

> Reads horribly!

> Dynamic binding travels in packs!

object strings = GetDynamicObject();

string last = ~(string)strings~[strings~.Length ~– 1];

Explicit Dynamic Operations

> Reads horribly!

> Dynamic binding travels in packs!

object strings = GetDynamicObject();

string last = ~(string)strings~[strings~.Length ~– 1];

Dynamic Contexts

object strings = GetDynamicObject();string last; dynamic { last = strings[strings.Length – 1]; }

Dynamic Contexts

object strings = GetDynamicObject();string last; dynamic { last = strings[strings.Length – 1]; }

> Different dialect of C# inside> Opt out with static contexts?> Lose sight of big contexts

Dynamic Contexts

> Different dialect of C# inside> Opt out with static contexts?> Lose sight of big contexts

object strings = GetDynamicObject();string last; dynamic { last = strings[strings.Length – 1]; }

Contagious Dynamic Expressions

object strings = GetDynamicObject(); string last = strings[dynamic(strings).Length – 1];

Contagious Dynamic Expressions

> Rules of propagation – what is dynamic?

> Factoring out subexpressions is hard

object strings = GetDynamicObject(); string last = strings[dynamic(strings).Length – 1];

Contagious Dynamic Expressions

> Rules of propagation – what is dynamic?

> Factoring out subexpressions is hard

object strings = GetDynamicObject(); string last = strings[dynamic(strings).Length – 1];

Dynamic Type

dynamic strings = GetDynamicObject(); string last = strings[strings.Length – 1];

Dynamic Type

> “Dynamicness” follows the object

> There is no syntactic difference!

dynamic strings = GetDynamicObject(); string last = strings[strings.Length – 1];

Dynamic Type

> “Dynamicness” follows the object

> There is no syntactic difference!

dynamic strings = GetDynamicObject(); string last = strings[strings.Length – 1];

Why is this OK?

> Embrace dynamic!

> Still explicit – just not in syntax!

> Replaces lengthy error-prone code

Type or Type Modifier?

> Generality:

> Static binding of Foo’s members> Dynamic binding of the rest

> Simplicity:

> Dynamic binding of all members> Even those on Object

dynamic Foo foo = GetDynamicFoo();

dynamic foo = GetDynamicFoo();

Type or Type Modifier?

> Generality:

> Static binding of Foo’s members> Dynamic binding of the rest

> Simplicity:

> Dynamic binding of all members> Even those on Object

dynamic Foo foo = GetDynamicFoo();

dynamic foo = GetDynamicFoo();

Dynamic Binding When?

> When the receiver is dynamic:

> Forces you to choose a type

> When any subexpression is dynamic:

> Softer landing

dynamic result = Math.Abs((double)d);

dynamic result = Math.Abs(d);

Dynamic Binding When?

> When the receiver is dynamic:

> Forces you to choose a type

> When any subexpression is dynamic:

> Softer landing

dynamic result = Math.Abs((double)d);

dynamic result = Math.Abs(d);

Dynamic Operations

> Dynamic result:> Method call Math.Abs(d)> Invocation d(“Hello”)> Member access d.Length> Operator application 4 + d> Indexing d[“Hello”]

> Static result:> Conversions (double)d> Object creation new Foo(d)

M(GetFoo(), d);

How Dynamic?

Bind with compile-time

type

Bind with runtime type

M(GetFoo(), d);

How Dynamic?

> “Just enough” dynamicness

> Principle of least surprise:> Argument’s contribution to binding is

invariant

Bind with compile-time

type

Bind with runtime type

The Meaning of dynamic

dynamic means“use my runtime type for binding”

Twice Daily Against Skepticism

> Which would you rather see – or write?

Type calcType = calc.GetType();object res = calcType.InvokeMember("Add", BindingFlags.InvokeMethod, null, new object[] { 10, 20 });int sum = Convert.ToInt32(res);

int sum = calc.Add(10, 20);

or

Related Sessions

Code Like the Wind with Microsoft Visual Basic 2010November 18, 13:00 - 13:45 – Petree Hall D Lucian Wischik

Microsoft Visual C# IDE Tips and TricksNovember 19, 12:45 - 13:30 – Petree Hall D DJ Park

F# for Parallel and Asynchronous ProgrammingNovember 19, 11:30 - 12:30 – 515A Luke Hoban

Microsoft Visual Basic IDE Tips and TricksNovember 19, 12:45 - 13:30 – Petree Hall C Dustin Campbell

Future Directions for C# and Visual Basic November 17, 11:00 - 12:00 – Hall F Luca Bolognese

Using Dynamic Languages to Build Scriptable ApplicationsNovember 17, 12:30 - 13:15 – 403AB Dino Viehland

Dynamic Binding in C# 4 November 17, 12:30 - 13:15 – Hall F Mads Torgersen

YOUR FEEDBACK IS IMPORTANT TO US!

Please fill out session evaluation

forms online atMicrosoftPDC.com

Learn More On Channel 9

> Expand your PDC experience through Channel 9

> Explore videos, hands-on labs, sample code and demos through the new Channel 9 training courses

channel9.msdn.com/learnBuilt by Developers for Developers….

© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.