dynamic binding in c 4 0 l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Dynamic Binding in C# 4.0 PowerPoint Presentation
Download Presentation
Dynamic Binding in C# 4.0

Loading in 2 Seconds...

play fullscreen
1 / 43

Dynamic Binding in C# 4.0 - PowerPoint PPT Presentation


  • 208 Views
  • Uploaded on

FT31. Dynamic Binding in C# 4.0. Mads Torgersen C# Language PM Microsoft Corporation. Dynamic in C#. The Dynamic Language Runtime (DLR) d ynamic in C# Designing dynamic. The DLR on the CLR. Common Language Runtime – CLR: Common platform for static languages Facilitates great interop.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Dynamic Binding in C# 4.0' - albert


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
dynamic binding in c 4 0
FT31

Dynamic Binding in C# 4.0

Mads Torgersen

C# Language PM

Microsoft Corporation

dynamic in c
Dynamic in C#
  • The Dynamic Language Runtime (DLR)
  • dynamic in C#
  • Designing dynamic
the dlr on the clr
The DLR on the CLR
  • Common Language Runtime – CLR:
    • Common platform for static languages
    • Facilitates great interop
the dlr on the clr4
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 clr5
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
Dynamic Objects
  • Implement their own binding
  • The DLR caches and optimizes
  • Built by dynamic languages – or you!
why dynamic in c
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
The Dynamic Type in C#

Calculator calc = GetCalculator();

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

object calc = GetCalculator();

TypecalcType = calc.GetType();

object res = calcType.InvokeMember("Add",

BindingFlags.InvokeMethod, null,

newobject[] { 10, 20 });

int sum = Convert.ToInt32(res);

ScriptObject calc = GetCalculator();

object res = calc.Invoke("Add", 10, 20);

int sum = Convert.ToInt32(res);

Statically typed to be dynamic

dynamic calc = GetCalculator();

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

Dynamic conversion

Dynamic method invocation

designing dynamic
Designing Dynamic
  • C# is not a dynamic language!
  • Dynamic is a dangerous foreign element
  • Should be syntactically explicit
static example
Static Example

string[]strings = GetStrings();

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

explicit dynamic operations
Explicit Dynamic Operations

object strings = GetDynamicObject();

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

explicit dynamic operations13
Explicit Dynamic Operations

object strings = GetDynamicObject();

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

explicit dynamic operations14
Explicit Dynamic Operations

object strings = GetDynamicObject();

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

explicit dynamic operations15
Explicit Dynamic Operations

object strings = GetDynamicObject();

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

explicit dynamic operations16
Explicit Dynamic Operations

object strings = GetDynamicObject();

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

explicit dynamic operations17
Explicit Dynamic Operations

object strings = GetDynamicObject();

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

explicit dynamic operations18
Explicit Dynamic Operations
  • Reads horribly!
  • Dynamic binding travels in packs!

object strings = GetDynamicObject();

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

explicit dynamic operations19
Explicit Dynamic Operations
  • Reads horribly!
  • Dynamic binding travels in packs!

object strings = GetDynamicObject();

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

dynamic contexts
Dynamic Contexts

object strings = GetDynamicObject();string last;

dynamic {last = strings[strings.Length– 1]; }

dynamic contexts21
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 contexts22
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
Contagious Dynamic Expressions

object strings = GetDynamicObject();

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

contagious dynamic expressions24
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 expressions25
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 Type

dynamic strings = GetDynamicObject();

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

dynamic type27
Dynamic Type
  • “Dynamicness” follows the object
  • There is no syntactic difference!

dynamic strings = GetDynamicObject();

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

dynamic type28
Dynamic Type
  • “Dynamicness” follows the object
  • There is no syntactic difference!

dynamic strings = GetDynamicObject();

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

why is this ok
Why is this OK?
  • Embrace dynamic!
  • Still explicit – just not in syntax!
  • Replaces lengthy error-prone code
type or type modifier
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 modifier31
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
Dynamic Binding When?
  • When the receiver is dynamic:
    • Forces you to choose a type
  • When anysubexpression is dynamic:
    • Softer landing

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

dynamic result = Math.Abs(d);

dynamic binding when33
Dynamic Binding When?
  • When the receiver is dynamic:
    • Forces you to choose a type
  • When anysubexpression is dynamic:
    • Softer landing

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

dynamic result = Math.Abs(d);

dynamic operations
Dynamic Operations
  • Dynamic result:
    • Method call Math.Abs(d)
    • Invocationd(“Hello”)
    • Member access d.Length
    • Operator application 4+ d
    • Indexing d[“Hello”]
  • Static result:
    • Conversions (double)d
    • Object creation newFoo(d)
how dynamic
How Dynamic?

M(GetFoo(), d);

Bind with

runtime type

Bind with

compile-time type

how dynamic36
How Dynamic?
  • “Just enough” dynamicness
  • Principle of least surprise:
    • Argument’s contribution to binding is invariant

M(GetFoo(), d);

Bind with

runtime type

Bind with

compile-time type

the meaning of dynamic
The Meaning of dynamic

dynamicmeans“use my runtime type for binding”

twice daily against skepticism
Twice Daily Against Skepticism
  • Which would you rather see – or write?

TypecalcType = calc.GetType();

object res = calcType.InvokeMember("Add",

BindingFlags.InvokeMethod, null,

newobject[] { 10, 20 });

int sum = Convert.ToInt32(res);

or

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

related sessions
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

slide40

YOUR FEEDBACK IS IMPORTANT TO US!

Please fill out session evaluation forms online at

MicrosoftPDC.com

learn more on channel 9
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….