Dynamic binding in C# allows binding to occur at runtime rather than compile time. It is implemented using the Dynamic Language Runtime (DLR) which handles binding and execution. Dynamic binding is useful for accessing members with only a name at runtime, for interoperating with dynamic languages, and for letting an object decide how to handle calls. It has some restrictions like not supporting extension methods or resolving static members on dynamic types.
5. 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
6. 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
8. 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
11. 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
14. 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
17. 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
18. 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
22. 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
24. 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
28. 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
31. 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#)