dynamic binding in c# 4.0
DESCRIPTION
TRANSCRIPT
DYNAMIC BINDING IN C# Buu Nguyen
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# 1.0
C# 2.0
C# 3.0
C# 4.0
Generics (*) Nullable types Anonymous methods Yield return Partial type Static class Namespace alias
LINQ (*) Auto- properties Collection initializer Object initializer Anonymous types Extension methods Partial methods Lambda expressions Expression trees
Dynamic binding (*) Named arguments Optional parameters Generic variance Field-like events Robust locking Better COM interop
Static vs. Dynamic Binding
Static Binding Dynamic Biding
Compiler figures out which members to call (binding process) Defer subtype polymorphic
resolution till run time
All bindings happen during 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
Call Sites
C# Binder
Expression Tree
Delegate
Dynamic Objects
compiled
builds
emits
DLR
uses
builds
cached
User-defined or from other languages
IDynamicMetaObjectProvider
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 on the runtime type of the target object
Method is selected based on both the runtime type 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
DynamicObject
ExpandoObject
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!
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