Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

March 31st 2010

Dynamic Deep Dive

Alexandru Ghiondea
Software Developer Engineer in Test
C# Compiler
Ghiondea.Alexandru@microsoft.com

March 31st 2010

Dynamic Deep Dive

Alexandru Ghiondea
Software Developer Engineer in Test
C# Compiler
Ghiondea.Alexandru@microsoft.com

C# 4.0 language features


Named and Optional parameters
Omit ref for COM calls
noPIA
Indexed properties
Co&Contra Variance
Dynamic

Why dynamic?
There is a lot of interest in accessing the
highly dynamic object model of HTML
DOM
COM is heavily used and the inter-op
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

Architecture
IronPytho
n

IronRuby

C#

VB.NET

Others

Dynamic Language Runtime


Expression Trees
Object
Binder

JavaScrip
t
Binder

Dynamic
Dispatch
Python
Binder

Call Site Caching


Ruby
Binder

COM
Binder

How dynamic works


We have a new type called dynamic
Calls that involve 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

How dynamic works (2)


COM
Binder
CLR
Compil
e

e
Ex

Run

D
e e le
ga
t

yn
am

ic

Ca
ll

DL
R Cach
e

Bind call
Expressio
n Tree

IronPytho
n Binder
C#
Runtime
Binder

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

What does a CallSite


contain?

Information about the call being made


What type of call (method call, property
access, 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

Dynamic FAQ
When do you go dynamic?
When the target of the call OR
Any of the call arguments 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

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!
We embed the type of the receiver in the CallSite

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

Dynamic FAQ (3)


Can I use Named and Optional with
dynamic?
Yes!
And also Co & Contra variance

Where cant I use dynamic?


Lambda expressions
LINQ

Cant use dynamic with method groups


throw or catch statements

Build you own dynamic


object
Its simple!

Implement IDynamicObject OR
Derive from DynamicObject
Implements IDynamicObject

Demo

I want to be dynamic!!!

Additional Resources
C# 4.0 Samples and Whitepaper
http://code.msdn.microsoft.com/csharpfuture

Visual C# Developer Center


http://csharp.net

C# team members blogs

http://blogs.msdn.com/ericlippert/
http://blogs.msdn.com/cburrows/
http://blogs.msdn.com/samng/
http://blogs.msdn.com/sreekarc/
http://blogs.msdn.com/mattwar/
http://blogs.msdn.com/ed_maurer/
http://blogs.msdn.com/davsterl/
http://blogs.msdn.com/alexghi

You might also like