1 / 43

Dynamic Binding in C# 4.0

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.

lupita
Download Presentation

Dynamic Binding in C# 4.0

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Dynamic in C# • The Dynamic Language Runtime (DLR) • dynamic in C# • Designing dynamic

  3. The DLR on the CLR • Common Language Runtime – CLR: • Common platform for static languages • Facilitates great interop

  4. 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

  5. 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

  6. Dynamic Objects • Implement their own binding • The DLR caches and optimizes • Built by dynamic languages – or you!

  7. Why Dynamic in C#? • Build on DLR opportunity • Use code from dynamic languages • Use other dynamic object models • Better COM interop

  8. 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

  9. C# Dynamic demo

  10. Designing Dynamic • C# is not a dynamic language! • Dynamic is a dangerous foreign element • Should be syntactically explicit

  11. Static Example string[]strings = GetStrings(); string last = strings[strings.Length – 1];

  12. Explicit Dynamic Operations object strings = GetDynamicObject(); string last = strings[strings.Length – 1];

  13. Explicit Dynamic Operations object strings = GetDynamicObject(); string last = strings[strings~.Length – 1];

  14. Explicit Dynamic Operations object strings = GetDynamicObject(); string last = strings~[strings~.Length – 1];

  15. Explicit Dynamic Operations object strings = GetDynamicObject(); string last = strings~[strings~.Length~– 1];

  16. Explicit Dynamic Operations object strings = GetDynamicObject(); string last = (string)strings~[strings~.Length~– 1];

  17. Explicit Dynamic Operations object strings = GetDynamicObject(); string last = ~(string)strings~[strings~.Length~– 1];

  18. Explicit Dynamic Operations • Reads horribly! • Dynamic binding travels in packs! object strings = GetDynamicObject(); string last = ~(string)strings~[strings~.Length~– 1];

  19. Explicit Dynamic Operations • Reads horribly! • Dynamic binding travels in packs! object strings = GetDynamicObject(); string last = ~(string)strings~[strings~.Length~– 1]; 

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

  21. 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

  22. 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]; } 

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

  24. Contagious Dynamic Expressions • Rules of propagation – what is dynamic? • Factoring out subexpressions is hard object strings = GetDynamicObject(); string last = strings[dynamic(strings).Length – 1];

  25. Contagious Dynamic Expressions • Rules of propagation – what is dynamic? • Factoring out subexpressions is hard object strings = GetDynamicObject(); string last = strings[dynamic(strings).Length – 1]; 

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

  27. Dynamic Type • “Dynamicness” follows the object • There is no syntactic difference! dynamic strings = GetDynamicObject(); string last = strings[strings.Length– 1];

  28. Dynamic Type • “Dynamicness” follows the object • There is no syntactic difference! dynamic strings = GetDynamicObject(); string last = strings[strings.Length– 1]; 

  29. Why is this OK? • Embrace dynamic! • Still explicit – just not in syntax! • Replaces lengthy error-prone code

  30. 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();

  31. 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(); 

  32. 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);

  33. 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); 

  34. 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)

  35. How Dynamic? M(GetFoo(), d); Bind with runtime type Bind with compile-time type

  36. 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

  37. The Meaning of dynamic dynamicmeans“use my runtime type for binding”

  38. 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);

  39. 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

  40. YOUR FEEDBACK IS IMPORTANT TO US! Please fill out session evaluation forms online at MicrosoftPDC.com

  41. 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….

More Related