SlideShare a Scribd company logo
1 of 44
Download to read offline
Buu Nguyen

DYNAMIC BINDING IN C#
Buu Nguyen

 Vice President of Technology, KMS Technology
 Lecturer, RMIT University Vietnam
 Microsoft MVP
Agenda

 What is dynamic binding
 How is it implemented
 When do we need it
 Restrictions of dynamic binding
What is Dynamic Binding
C# Language Evolution


                                                           C# 4.0
                                  C# 3.0                   Dynamic binding (*)
                                                           Named arguments
                                  LINQ (*)                 Optional parameters
                                  Auto- properties         Generic variance
              C# 2.0              Collection initializer   Field-like events
                                  Object initializer       Robust locking
              Generics (*)        Anonymous types          Better COM interop
              Nullable types      Extension methods
              Anonymous methods   Partial methods
     C# 1.0   Yield return
              Partial type
                                  Lambda expressions
                                  Expression trees
              Static class
              Namespace alias
Static vs. Dynamic Binding

Static Binding                  Dynamic Biding
 Compiler figures out which     All bindings happen during
  members to call (binding        run time
  process)
   Defer subtype polymorphic
     resolution till run time
Static Binding
Benefits of Static Binding

 Type and name errors are detected at
  compile time, e.g.
   Invoke non-existent members
   Pass in arguments with wrong type
   Perform illegal cast
Dynamic Binding
How Is It Implemented?
Run Time Binding

 Instead of attempting binding and generating
  CIL, the compiler packages the call and sends
  it to the Dynamic Language Runtime
 At run time, the DLR performs binding and
  execution
Under the Hood


becomes
The Dynamic Language Runtime
Process in a nutshell

   C#
                      Dynamic               builds
 dynamic
                      Objects
                                  User-defined or from other languages
      compiled


               uses   IDynamicMetaObjectProvider
Call Sites

      cached
                                                                                 Delegate
                                                       Expression        emits
                                                          Tree

DLR
                      C# Binder            builds
Dynamic Type in CIL
When Do We Need It?
Key Scenarios

1. Access a member with only knowledge of its
   name, arguments, and target object

2. Interop with dynamic languages, e.g.
   IronRuby, IronPython

3. Have the target object decide how to
   respond to a call at run time
Key Scenarios

1. Access a member with only knowledge of its
   name, arguments, and target object

2. Interop with dynamic languages, e.g.
   IronRuby, IronPython

3. Have the target object decide how to
   respond to a call at run time
Access Members
Reflection
Dynamic Type
Single vs. Multiple Dispatch

Single Dispatch                Multiple Dispatch
 Method is selected based      Method is selected based
  on the runtime type of the     on both the runtime type
  target object                  of the target object and
                                 those of the method’s
                                 arguments
Dispatch Example
Key Scenarios

1. Access a member with only knowledge of its
   name, arguments, and target object

2. Interop with dynamic languages, e.g.
   IronRuby, IronPython

3. Have the target object decide how to
   respond to a call at run time
Invoke Ruby Code
Work with Ruby Class
Work with method_missing
Key Scenarios

1. Access a member with only knowledge of its
   name, arguments, and target object

2. Interop with dynamic languages, e.g.
   IronRuby, IronPython

3. Have the target object decide how to
   respond to a call at run time
The Magic Interface


    IDynamicMetaObjectProvider


                            ExpandoObject

                            DynamicObject
ExpandoObject
DynamicObject’s Operations
Name                 Description
TryGetMember         Member getter, e.g. obj.Name
TrySetMember         Member setter, e.g. obj.age = 10
TryDeleteMember      Member removal (no equivalent in C#)
TryInvokeMember      Method invocation, e.g. obj.Invoke()
TryConvert           Casting, e.g. (int)obj
TryCreateInstance    Object creation (no equivalent in C#)
TryInvoke            Self invocation, e.g. obj(10)
TryBinaryOperation   Binary operation, e.g. obj + 10
TryUnaryOperation    Unary operation, e.g. !obj
TryGetIndex          Indexer getter, e.g. obj[“key”]
TrySetIndex          Indexer setter, e.g. obj[“key”] = value
TryDeleteIndex       Indexer removal (no equivalent in C#)
Log Setters & Invocations
StaticInvoker
StaticInvoker
Close to the metal
…Close to the metal
…Close to the metal
Restrictions
Restriction #1

 Doesn’t work with extension methods
Restriction #2

 Can’t resolve static members or constructors
  on a dynamic type
Restriction #3

 Method groups, anonymous methods and
  lambda expressions to be casted to exact type
Restriction #4
Thank You!
buunguyen@kms-technology.com
http://vn.linkedin.com/in/buunguyen
http://www.twitter.com/buunguyen
http://www.facebook.com/buunguyen
References

 DLR specs from
  http://dlr.codeplex.com/documentation
 C# in Depth, 2nd, Jon Skeet, Manning, 2010
 Pro DLR in .NET 4.0, Chaur Wu, Apress, 2010

More Related Content

What's hot (20)

Java constructors
Java constructorsJava constructors
Java constructors
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Data Types - Premetive and Non Premetive
Data Types - Premetive and Non Premetive Data Types - Premetive and Non Premetive
Data Types - Premetive and Non Premetive
 
C# Framework class library
C# Framework class libraryC# Framework class library
C# Framework class library
 
Net framework
Net frameworkNet framework
Net framework
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
 
Finalize() method
Finalize() methodFinalize() method
Finalize() method
 
Oop concepts in python
Oop concepts in pythonOop concepts in python
Oop concepts in python
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Array of objects.pptx
Array of objects.pptxArray of objects.pptx
Array of objects.pptx
 
Multiple inheritance in c++
Multiple inheritance in c++Multiple inheritance in c++
Multiple inheritance in c++
 
Object oriented approach in python programming
Object oriented approach in python programmingObject oriented approach in python programming
Object oriented approach in python programming
 
Object Oriented Programming with C#
Object Oriented Programming with C#Object Oriented Programming with C#
Object Oriented Programming with C#
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
C# Types of classes
C# Types of classesC# Types of classes
C# Types of classes
 

Similar to Dynamic Binding in C# 4.0

Introduction to c sharp 4.0 and dynamic
Introduction to c sharp 4.0 and dynamicIntroduction to c sharp 4.0 and dynamic
Introduction to c sharp 4.0 and dynamicGieno Miao
 
PDC Video on C# 4.0 Futures
PDC Video on C# 4.0 FuturesPDC Video on C# 4.0 Futures
PDC Video on C# 4.0 Futuresnithinmohantk
 
New c sharp4_features_part_iv
New c sharp4_features_part_ivNew c sharp4_features_part_iv
New c sharp4_features_part_ivNico Ludwig
 
Dynamic languages for .NET CLR
Dynamic languages for .NET CLRDynamic languages for .NET CLR
Dynamic languages for .NET CLRpy_sunil
 
C# 3.0 and 4.0
C# 3.0 and 4.0C# 3.0 and 4.0
C# 3.0 and 4.0Buu Nguyen
 
Basics of c# by sabir
Basics of c# by sabirBasics of c# by sabir
Basics of c# by sabirSabir Ali
 
Dynamic Language Performance
Dynamic Language PerformanceDynamic Language Performance
Dynamic Language PerformanceKevin Hazzard
 
NDK Primer (AnDevCon Boston 2014)
NDK Primer (AnDevCon Boston 2014)NDK Primer (AnDevCon Boston 2014)
NDK Primer (AnDevCon Boston 2014)Ron Munitz
 
Component object model and
Component object model andComponent object model and
Component object model andSaransh Garg
 
C# advanced topics and future - C#5
C# advanced topics and future - C#5C# advanced topics and future - C#5
C# advanced topics and future - C#5Peter Gfader
 
Lecture4 corba
Lecture4   corbaLecture4   corba
Lecture4 corbapoovi117
 
NDK Primer (Wearable DevCon 2014)
NDK Primer (Wearable DevCon 2014)NDK Primer (Wearable DevCon 2014)
NDK Primer (Wearable DevCon 2014)Ron Munitz
 

Similar to Dynamic Binding in C# 4.0 (20)

Introduction to c sharp 4.0 and dynamic
Introduction to c sharp 4.0 and dynamicIntroduction to c sharp 4.0 and dynamic
Introduction to c sharp 4.0 and dynamic
 
C sharp
C sharpC sharp
C sharp
 
PDC Video on C# 4.0 Futures
PDC Video on C# 4.0 FuturesPDC Video on C# 4.0 Futures
PDC Video on C# 4.0 Futures
 
New c sharp4_features_part_iv
New c sharp4_features_part_ivNew c sharp4_features_part_iv
New c sharp4_features_part_iv
 
C#4.0 features
C#4.0 featuresC#4.0 features
C#4.0 features
 
Dynamic languages for .NET CLR
Dynamic languages for .NET CLRDynamic languages for .NET CLR
Dynamic languages for .NET CLR
 
Presentation On Com Dcom
Presentation On Com DcomPresentation On Com Dcom
Presentation On Com Dcom
 
C Course Material0209
C Course Material0209C Course Material0209
C Course Material0209
 
201005 accelerometer and core Location
201005 accelerometer and core Location201005 accelerometer and core Location
201005 accelerometer and core Location
 
How To Code in C#
How To Code in C#How To Code in C#
How To Code in C#
 
C# 3.0 and 4.0
C# 3.0 and 4.0C# 3.0 and 4.0
C# 3.0 and 4.0
 
Basics of c# by sabir
Basics of c# by sabirBasics of c# by sabir
Basics of c# by sabir
 
internet
internetinternet
internet
 
Dynamic Language Performance
Dynamic Language PerformanceDynamic Language Performance
Dynamic Language Performance
 
NDK Primer (AnDevCon Boston 2014)
NDK Primer (AnDevCon Boston 2014)NDK Primer (AnDevCon Boston 2014)
NDK Primer (AnDevCon Boston 2014)
 
Component object model and
Component object model andComponent object model and
Component object model and
 
DLR MCQs
DLR MCQsDLR MCQs
DLR MCQs
 
C# advanced topics and future - C#5
C# advanced topics and future - C#5C# advanced topics and future - C#5
C# advanced topics and future - C#5
 
Lecture4 corba
Lecture4   corbaLecture4   corba
Lecture4 corba
 
NDK Primer (Wearable DevCon 2014)
NDK Primer (Wearable DevCon 2014)NDK Primer (Wearable DevCon 2014)
NDK Primer (Wearable DevCon 2014)
 

More from Buu Nguyen

On Becoming a Technical Lead
On Becoming a Technical LeadOn Becoming a Technical Lead
On Becoming a Technical LeadBuu Nguyen
 
Stories about KMS Technology
Stories about KMS TechnologyStories about KMS Technology
Stories about KMS TechnologyBuu Nguyen
 
ASP.NET MVC 2.0
ASP.NET MVC 2.0ASP.NET MVC 2.0
ASP.NET MVC 2.0Buu Nguyen
 
Building Scalable .NET Web Applications
Building Scalable .NET Web ApplicationsBuilding Scalable .NET Web Applications
Building Scalable .NET Web ApplicationsBuu Nguyen
 
New Features of ASP.NET 4.0
New Features of ASP.NET 4.0New Features of ASP.NET 4.0
New Features of ASP.NET 4.0Buu Nguyen
 
C# 4.0 and .NET 4.0
C# 4.0 and .NET 4.0C# 4.0 and .NET 4.0
C# 4.0 and .NET 4.0Buu Nguyen
 

More from Buu Nguyen (10)

On Becoming a Technical Lead
On Becoming a Technical LeadOn Becoming a Technical Lead
On Becoming a Technical Lead
 
Stories about KMS Technology
Stories about KMS TechnologyStories about KMS Technology
Stories about KMS Technology
 
ASP.NET MVC 3
ASP.NET MVC 3ASP.NET MVC 3
ASP.NET MVC 3
 
HTML5 in IE9
HTML5 in IE9HTML5 in IE9
HTML5 in IE9
 
ASP.NET MVC 2.0
ASP.NET MVC 2.0ASP.NET MVC 2.0
ASP.NET MVC 2.0
 
Building Scalable .NET Web Applications
Building Scalable .NET Web ApplicationsBuilding Scalable .NET Web Applications
Building Scalable .NET Web Applications
 
New Features of ASP.NET 4.0
New Features of ASP.NET 4.0New Features of ASP.NET 4.0
New Features of ASP.NET 4.0
 
C# 4.0 and .NET 4.0
C# 4.0 and .NET 4.0C# 4.0 and .NET 4.0
C# 4.0 and .NET 4.0
 
Combres
CombresCombres
Combres
 
Fasterflect
FasterflectFasterflect
Fasterflect
 

Recently uploaded

(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?Paolo Missier
 
Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024Patrick Viafore
 
WebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceWebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceSamy Fodil
 
Intro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptxIntro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptxFIDO Alliance
 
The Metaverse: Are We There Yet?
The  Metaverse:    Are   We  There  Yet?The  Metaverse:    Are   We  There  Yet?
The Metaverse: Are We There Yet?Mark Billinghurst
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxFIDO Alliance
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc
 
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...ScyllaDB
 
How to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfHow to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfdanishmna97
 
ChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityVictorSzoltysek
 
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...ScyllaDB
 
Top 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTop 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTopCSSGallery
 
Vector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptxVector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptxjbellis
 
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...panagenda
 
AI mind or machine power point presentation
AI mind or machine power point presentationAI mind or machine power point presentation
AI mind or machine power point presentationyogeshlabana357357
 
Oauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoftOauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoftshyamraj55
 
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...marcuskenyatta275
 
Event-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingEvent-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingScyllaDB
 
How we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfHow we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfSrushith Repakula
 
Google I/O Extended 2024 Warsaw
Google I/O Extended 2024 WarsawGoogle I/O Extended 2024 Warsaw
Google I/O Extended 2024 WarsawGDSC PJATK
 

Recently uploaded (20)

(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
 
Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024
 
WebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceWebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM Performance
 
Intro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptxIntro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptx
 
The Metaverse: Are We There Yet?
The  Metaverse:    Are   We  There  Yet?The  Metaverse:    Are   We  There  Yet?
The Metaverse: Are We There Yet?
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
 
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
 
How to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfHow to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cf
 
ChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps Productivity
 
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
 
Top 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTop 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development Companies
 
Vector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptxVector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptx
 
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
 
AI mind or machine power point presentation
AI mind or machine power point presentationAI mind or machine power point presentation
AI mind or machine power point presentation
 
Oauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoftOauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoft
 
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
 
Event-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingEvent-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream Processing
 
How we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfHow we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdf
 
Google I/O Extended 2024 Warsaw
Google I/O Extended 2024 WarsawGoogle I/O Extended 2024 Warsaw
Google I/O Extended 2024 Warsaw
 

Dynamic Binding in C# 4.0