1 / 18

C# 4.0

Dynamic internals. C# 4.0. Introductions. Alexandru Ghiondea C# Compiler QA ghiondea.alexandru@microsoft.com http://blogs.msdn.com/alexghi What is your programming language of choice? Do your apps interop with COM? Do you use dynamic languages? Have you seen C# 4.0 before?.

mali
Download Presentation

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. Dynamic internals C# 4.0

  2. Introductions • Alexandru Ghiondea • C# Compiler QA • ghiondea.alexandru@microsoft.com • http://blogs.msdn.com/alexghi • What is your programming language of choice? • Do your apps interop with COM? Do you use dynamic languages? • Have you seen C# 4.0 before?

  3. C# 4.0 language features • Named and Optional parameters • Omit “ref” for COM calls • noPIA • Co&Contra Variance • Dynamic

  4. Why dynamic? • There is a lot of interest in accessing the highly dynamic object model of HTML DOM • COM is heavily used and the interop code could be easier to read and write • Dynamic languages are becoming increasingly popular • We need a unified way to work with all of the above

  5. Architecture IronPython IronRuby C# VB.NET Others… Dynamic Language Runtime Expression Trees Dynamic Dispatch Call Site Caching ObjectBinder JavaScriptBinder PythonBinder RubyBinder COMBinder

  6. How dynamic works • We have a new type called dynamic • Calls on variables of type dynamic are treated differently by the compiler • We package extra information about the call • Calls can be: Method calls, property access, indexer call, operator call • There is a C# runtime binder to interpret the information (at runtime) • It uses the runtime type for all dynamic arguments • It uses the compile time type for all other arguments

  7. How dynamic works (2) CLR Expression Tree COM Binder Exe IronPython Binder Bind call Compile Run Dynamic Call Delegate DLR C# Runtime Binder Cache …

  8. IronPython, PowerPoint and C# Demo

  9. Under the cover • What is the Dynamic type? • There is no dynamic type . There is only object! • What operations are supported? • A variable of type dynamic is assumed to support any kind of operation (method call, property access, operator call and indexer call) • How is the information about the call stored? • Using CallSites objects the compiler packages data about the call

  10. What does a CallSite contain? • Information about the call being made • What type of call (method call, property, operator, etc) • The name of the member • The context of the call • The type arguments for the call • Information about the parameters and return types • Is it a constant, is it passed by-ref, etc

  11. Dynamic and CallSites Demo

  12. Dynamic FAQ • When do you go dynamic? • When the target of the call or any of it’s parameters are dynamic • What is the return type of a dynamic call? • It is dynamic in most cases* • *What about conversions and constructors? • They are dispatched at runtime, but their return type is known at compile time

  13. Dynamic FAQ (2) • What about private methods? • Information about the context of call is embedded in the CallSite allowing the Binder to “do the right thing” • Calling a method off a non-dynamic target with dynamic arguments. (c.M(d)); • It works as expected! • Can dynamic “cross” assembly boundaries? • Yes, we decorate any dynamic parameter or return type with a DynamicAttribute • It works for generic types constructed with dynamic as well 

  14. Dynamic FAQ (3) • Can I use Named and Optional with dynamic? • Yes! • And also Co & Contra variance • Where can’t I use dynamic? • Lambda expressions • LINQ • Can’t use dynamic and method groups • throw or catch statements

  15. Return type, private methods and generics Demo

  16. Build you own dynamic object • It’s simple! • Implement IDynamicObject • Derive from DynamicObject • Implements IDynamicObject

  17. I want to be dynamic!!! Demo

  18. Additional Resources • C# 4.0 Samples and Whitepaper • http://code.msdn.microsoft.com/csharpfuture • Visual C# Developer Center • http://csharp.net • C# team member’s blogs on dynamic • http://blogs.msdn.com/cburrows/ • http://blogs.msdn.com/samng/ • http://blogs.msdn.com/sreekarc/ • http://blogs.msdn.com/alexghi/

More Related