C# 4.0 - Dynamic internalsx
Download
Report
Transcript C# 4.0 - Dynamic internalsx
Dynamic internals
C# 4.0
Introductions
Alexandru Ghiondea
C# Compiler QA
[email protected]
http://blogs.msdn.com/alexghi
What is your programming language of choice?
Do your apps interop with COM? Do you use
dynamic languages?
Have you seen C# 4.0 before?
C# 4.0 language features
Named and Optional parameters
Omit “ref” for COM calls
noPIA
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 interop 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
IronPython
IronRuby
C#
VB.NET
Others…
Dynamic Language Runtime
Expression Trees
Object
Binder
JavaScript
Binder
Dynamic Dispatch
Python
Binder
Call Site Caching
Ruby
Binder
COM
Binder
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)
COM Binder
CLR
Compile
Run
DLR
Cache
Bind call
Expression
Tree
IronPython
Binder
C# Runtime
Binder
…
IronPython, PowerPoint and C#
DEMO
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
contain?
Information about the call being made
What type of call (method call, property, 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 and CallSites
DEMO
Dynamic FAQ
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
well
Dynamic FAQ (3)
Can I use Named and Optional with dynamic?
Yes!
And also Co & Contra variance
Where can’t I use dynamic?
Lambda expressions
LINQ
Can’t use dynamic and method groups
throw or catch statements
Return type, private methods and generics
DEMO
Build you own dynamic object
It’s simple!
Implement IDynamicObject
Derive from DynamicObject
Implements IDynamicObject
I want to be dynamic!!!
DEMO
Additional Resources
C# 4.0 Samples and Whitepaper
http://code.msdn.microsoft.com/csharpfuture
Visual C# Developer Center
http://csharp.net
C# team member’s blogs on dynamic
http://blogs.msdn.com/cburrows/
http://blogs.msdn.com/samng/
http://blogs.msdn.com/sreekarc/
http://blogs.msdn.com/alexghi/