C# 4.0 - Dynamic internalsx
Transcript C# 4.0 - Dynamic internalsx
C# Compiler QA
What is your programming language of choice?
Do your apps interop with COM? Do you use
Have you seen C# 4.0 before?
C# 4.0 language features
Named and Optional parameters
Omit “ref” for COM calls
There is a lot of interest in accessing the
highly dynamic object model of HTML DOM
COM is heavily used and the interop code
could be easier to read and write
Dynamic languages are becoming
We need a unified way to work with all of the
Dynamic Language Runtime
Call Site Caching
How dynamic works
We have a new type called dynamic
Calls on 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)
IronPython, PowerPoint and C#
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 packages
data about the call
What does a CallSite
Information about the call being made
What type of call (method call, property, operator,
The name of the member
The context of the call
The type arguments for the call
Information about the parameters and return
Is it a constant, is it passed by-ref, etc
Dynamic and CallSites
When do you go dynamic?
When the target of the call or any of it’s
parameters 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!
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
Dynamic FAQ (3)
Can I use Named and Optional with dynamic?
And also Co & Contra variance
Where can’t I use dynamic?
Can’t use dynamic and method groups
throw or catch statements
Return type, private methods and generics
Build you own dynamic object
Derive from DynamicObject
I want to be dynamic!!!
C# 4.0 Samples and Whitepaper
Visual C# Developer Center
C# team member’s blogs on dynamic