You are on page 1of 43

FT31

Dynamic Binding in C# 4.0

Mads Torgersen
C# Language PM
Microsoft Corporation
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 });
ScriptObject calc = GetCalculator();
int sum = Convert.ToInt32(res);
object res = calc.Invoke("Add", 10, 20);
int sum = Convert.ToInt32(res);

Statically typed dynamic calc = GetCalculator();


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

Dynamic Dynamic method


conversion invocation
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

object strings = GetDynamicObject();

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

> Reads horribly!

> Dynamic binding travels in packs!


Explicit Dynamic Operations

object strings = GetDynamicObject();

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

> Reads horribly!

> Dynamic binding travels in packs!


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

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


Contagious Dynamic Expressions

object strings = GetDynamicObject();

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


Contagious Dynamic Expressions

object strings = GetDynamicObject();

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

> Rules of propagation – what is dynamic?

> Factoring out subexpressions is hard


Contagious Dynamic Expressions

object strings = GetDynamicObject();

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

> Rules of propagation – what is dynamic?

> Factoring out subexpressions is hard


Dynamic Type

dynamic strings = GetDynamicObject();

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


Dynamic Type

dynamic strings = GetDynamicObject();

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

> “Dynamicness” follows the object

> There is no syntactic difference!


Dynamic Type

dynamic strings = GetDynamicObject();

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

> “Dynamicness” follows the object

> There is no syntactic difference!


Why is this OK?

> Embrace dynamic!

> Still explicit – just not in syntax!

> Replaces lengthy error-prone code


Type or Type Modifier?

> Generality:
dynamic Foo foo = GetDynamicFoo();
> Static binding of Foo’s members
> Dynamic binding of the rest

> Simplicity:
dynamic foo = GetDynamicFoo();

> Dynamic binding of all members


> Even those on Object
Type or Type Modifier?

> Generality:
dynamic Foo foo = GetDynamicFoo();
> Static binding of Foo’s members
> Dynamic binding of the rest 
> Simplicity:
dynamic foo = GetDynamicFoo();

> Dynamic binding of all members


> Even those on Object

Dynamic Binding When?

> When the receiver is dynamic:


dynamic result = Math.Abs((double)d);
> Forces you to choose a type

> When any subexpression is dynamic:


dynamic result = Math.Abs(d);
> Softer landing
Dynamic Binding When?

> When the receiver is dynamic:


dynamic result = Math.Abs((double)d);
> Forces you to choose a type 
> When any subexpression is dynamic:
dynamic result = Math.Abs(d);
> Softer landing

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)
How Dynamic?

M(GetFoo(), d);

Bind with Bind with


compile-time type runtime type
How Dynamic?

M(GetFoo(), d);

Bind with Bind with


compile-time type runtime type

> “Just enough” dynamicness

> Principle of least surprise:


> Argument’s contribution to binding is invariant
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);

or
int sum = calc.Add(10, 20);
Related Sessions
Future Directions for C# and Visual Basic
November 17, 11:00 - 12:00 – Hall F Luca Bolognese
Dynamic Binding in C# 4
November 17, 12:30 - 13:15 – Hall F Mads Torgersen
Using Dynamic Languages to Build Scriptable Applications
November 17, 12:30 - 13:15 – 403AB Dino Viehland
Code Like the Wind with Microsoft Visual Basic 2010
November 18, 13:00 - 13:45 – Petree Hall D Lucian Wischik
F# for Parallel and Asynchronous Programming
November 19, 11:30 - 12:30 – 515A Luke Hoban
Microsoft Visual C# IDE Tips and Tricks
November 19, 12:45 - 13:30 – Petree Hall D DJ Park
Microsoft Visual Basic IDE Tips and Tricks
November 19, 12:45 - 13:30 – Petree Hall C Dustin Campbell
YOUR
FEEDBACK IS
IMPORTANT TO
US! Please fill out session
evaluation forms online
at
MicrosoftPDC.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/learn
Built 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.

You might also like