Top Banner
DYNAMIC BINDING IN C# Buu Nguyen
44

Dynamic Binding in C# 4.0

Nov 01, 2014

Download

Technology

Buu Nguyen

 
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Dynamic Binding in C# 4.0

DYNAMIC BINDING IN C# Buu Nguyen

Page 2: Dynamic Binding in C# 4.0

Buu Nguyen

Vice President of Technology, KMS Technology

Lecturer, RMIT University Vietnam

Microsoft MVP

Page 3: Dynamic Binding in C# 4.0

Agenda

What is dynamic binding

How is it implemented

When do we need it

Restrictions of dynamic binding

Page 4: Dynamic Binding in C# 4.0

What is Dynamic Binding

Page 5: Dynamic Binding in C# 4.0

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

Page 6: Dynamic Binding in C# 4.0

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

Page 7: Dynamic Binding in C# 4.0

Static Binding

Page 8: Dynamic Binding in C# 4.0

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

Page 9: Dynamic Binding in C# 4.0

Dynamic Binding

Page 10: Dynamic Binding in C# 4.0

How Is It Implemented?

Page 11: Dynamic Binding in C# 4.0

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

Page 12: Dynamic Binding in C# 4.0

Under the Hood

becomes

Page 13: Dynamic Binding in C# 4.0

The Dynamic Language Runtime

Page 14: Dynamic Binding in C# 4.0

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

Page 15: Dynamic Binding in C# 4.0

Dynamic Type in CIL

Page 16: Dynamic Binding in C# 4.0

When Do We Need It?

Page 17: Dynamic Binding in C# 4.0

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

Page 18: Dynamic Binding in C# 4.0

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

Page 19: Dynamic Binding in C# 4.0

Access Members

Page 20: Dynamic Binding in C# 4.0

Reflection

Page 21: Dynamic Binding in C# 4.0

Dynamic Type

Page 22: Dynamic Binding in C# 4.0

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

Page 23: Dynamic Binding in C# 4.0

Dispatch Example

Page 24: Dynamic Binding in C# 4.0

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

Page 25: Dynamic Binding in C# 4.0

Invoke Ruby Code

Page 26: Dynamic Binding in C# 4.0

Work with Ruby Class

Page 27: Dynamic Binding in C# 4.0

Work with method_missing

Page 28: Dynamic Binding in C# 4.0

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

Page 29: Dynamic Binding in C# 4.0

The Magic Interface

IDynamicMetaObjectProvider

DynamicObject

ExpandoObject

Page 30: Dynamic Binding in C# 4.0

ExpandoObject

Page 31: Dynamic Binding in C# 4.0

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#)

Page 32: Dynamic Binding in C# 4.0

Log Setters & Invocations

Page 33: Dynamic Binding in C# 4.0

StaticInvoker

Page 34: Dynamic Binding in C# 4.0

StaticInvoker

Page 35: Dynamic Binding in C# 4.0

Close to the metal

Page 36: Dynamic Binding in C# 4.0

…Close to the metal

Page 37: Dynamic Binding in C# 4.0

…Close to the metal

Page 38: Dynamic Binding in C# 4.0

Restrictions

Page 39: Dynamic Binding in C# 4.0

Restriction #1

Doesn’t work with extension methods

Page 40: Dynamic Binding in C# 4.0

Restriction #2

Can’t resolve static members or constructors on a dynamic type

Page 41: Dynamic Binding in C# 4.0

Restriction #3

Method groups, anonymous methods and lambda expressions to be casted to exact type

Page 42: Dynamic Binding in C# 4.0

Restriction #4

Page 44: Dynamic Binding in C# 4.0

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