430 likes | 614 Views
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.
E N D
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(); 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
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 anysubexpression 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 anysubexpression is dynamic: • Softer landing dynamic result = Math.Abs((double)d); dynamic result = Math.Abs(d);
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? M(GetFoo(), d); Bind with runtime type Bind with compile-time type
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 dynamicmeans“use my runtime type for binding”
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 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….