SlideShare a Scribd company logo
1 of 13
Download to read offline
1 
New C#4 Features – Part VI 
Nico Ludwig (@ersatzteilchen)
2 
TOC 
● New C#4 Features – Part VI 
– Beyond Expando Objects: Home-brew Dynamic Dispatch 
– Hosting Scripting Languages 
● Sources: 
– Jon Skeet, CSharp in Depth
3 
If ExpandoObject is not enough 
● We can get very far with ExpandoObject, but data driven APIs require more. 
– The possibility to encapsulate an existing API is often the core paradigm. 
– Adding user defined behavior to get more control (e.g. enforce readonly objects). 
– Sometimes you need or want to deal with operators. 
– The class ExpandoObject is sealed, and can not act as base class. 
● Up to now we've exploited the DLR from the caller side, now we'll implement own dynamic behavior in C#. 
– We can create types, whose interfaces change during run time; and these interface changes are based on how we use the type. 
– That means we'll implement our own dynamic dispatch. 
● ExpandoObject instances behave differently in 
VB and in C#, because in VB symbols are case 
insensitive, but in C# they are case sensitive!
4 
DynamicObject in Action 
<DynamicObjects> 
This example shows home brew dynamic dispatch with the 
CLR type DynamicObject. 
Key aspects of dynamic programming: 
(1. Dynamic Typing) 
(2. Late Binding) 
(3. Duck Typing) 
4. Dynamic Objects
5 
Home-brew Dynamic Dispatch with DynamicObject 
● If you need more than dynamic property bags you can handle the dynamic dispatching yourself in a type derived from 
DynamicObject. 
– E.g. to create methods and properties at run time to create explorative interfaces. 
● E.g. to wrap another API. 
– DynamicObject allows you to dispatch all (twelve) dynamic operations yourself. 
– We have the same abilities to handle unknown methods as in Smalltalk and Obj-C! 
● Instead of overriding "doesNotUnderstand:" (Smalltalk) we have to override "TryInvokeMember()". 
● How to make use of DynamicObject: 
– Import the System.Dynamic namespace. 
– Inherit from DynamicObject. 
– Then you can override the behavior of dynamic operations or define new methods. 
● If you can not derive from DynamicObject, implement IDynamicMetaObjectProvider. 
– But meta programming with Expression trees is not that simple... 
● In the accompanying source code, you can find richly 
commented Smalltalk, Obj-C, Python and Ruby 
implementations of a property bag. (Please have a 
look, it is really not difficult to understand in that 
languages!) 
● It is planned to have a similar home-brew dynamic 
dispatch support in Scala. 
● If you implement IDynamicMetaObjectProvider you 
have to program a DynamicMetaObject, in which you 
code how dynamic operations translate into Expression 
trees. This flexibility comes with a cost: complexity, as 
coding of Expression trees, can be very complicated. 
● In .Net 4.5 there'll be a new type, JsonValue, which 
allows simple serialization of Json-formatted data. In 
order to be programmable in a simple way, it 
implements IDynamicMetaObjectProvide and can be 
used like ExpandoObject to add new Json elements. 
● So if the out-of-the-box abilities of the DLR are not 
suf cient for your scenario, you can write own binders 
and meta-objects.
6 
Mechanics of Dynamic Dispatch 
● The compiler lets us pass any kind of operation using the dynamic keyword. 
– And the real type has to handle the passed operation. 
– But the operation needs not to be a part of the real type's static interface. 
– The idea is to mix dynamic dispatch with static invocations in a single type. 
– This can be done with dynamic objects. 
● Restrictions: 
– To inspect the run time type of DynamicObject you can't use reflection. 
● (Also you can not check, whether a variable was declared as dynamic reference at run time.) 
● As its type (e.g. the properties and methods) is part of its value. 
● You absolutely have to document how your DynamicObject works! 
– No excuses! The compiler can't help here! 
– Best practice: write unit tests documenting your code!
7 
Dynamic Languages based on .Net 
● Meanwhile there exist a lot of dynamic .Net languages based on the DLR. 
– Iron*, like IronRuby, IronPython, IronScheme etc.... 
– Inspired by the success of Jython on the JVM, IronPython was developed. 
– During run time, scripts get translated into IL code and then executed by the CLR. 
● The new features of Expression trees support enabling scripting with the DLR. 
– The compiler/interpreter pipeline of a DLR-based scripting engine: 
● 1. Lex and parse the script => Expression trees, or abstract syntax trees (AST), 
● 2. pass the AST to the DLR, 
● 3. the DLR then compiles the AST to IL and executes it on the CLR, 
● 4. finally the CLR could JIT IL code to native machine code. 
– Expression trees are to the DLR what IL is to the CLR. 
● Some scripting engines can run in compiler mode. 
– Then methods get compiled into IL code (w/o the Expression tree step). 
● IronPython 1.0 was even faster than C-based 
Python, because .Net Delegate-invocations are 
very fast since .Net 2 (CPython, Jython do only 
have an own runtime (different from .Net)). So, 
yes, there exist also variants of the DLR (and 
IronRuby and IronPython) that run on .Net 2 (then 
you’ll need to deploy the DLR 
(Microsoft.Scripting.dll, Microsoft.Dynamic.dll 
etc.) yourself). 
● E.g. IronRuby has a compiler mode (default). In 
opposite to the compiler mode the interpreter 
mode has a better startup time, but the run time is 
worse.
8 
The Iron Languages 
● The DLR and IronPython have been developed by the same team until 2006. 
– The DLR, IronPython and IronRuby (2008) are all open source (Apache lic. v2). 
● Iron* languages offer great integration with .Net: 
– Easy to use .Net libraries and other .Net languages. 
– Easy to use on .Net hosts and with .Net tools. 
● Iron* languages compile to IL code during run time. 
– During run time: Created Expression trees will finally be compiled to IL code. 
● And the JIT compiler will compile the IL code to machine code. 
● Or you can compile your script code into an assembly! 
● But we also have two type systems to deal with: .Net or the Iron* language's one. 
– Ruby has mutable strings, .Net don't (there is a to_clr_string() conversion method). 
● The open source variant of the DLR (hosted on 
CodePlex) develops faster currently. 
● The DLR in .Net resides in System.Core.dll. 
● IRON: (kidding) Implementation Running on .Net. 
● For this presentation: 
● IronRuby 1.1.3 was installed as NuGet package for 
the project. 
● The conversion between Iron* and CLR types does 
often happen automatically, when it should. E.g. 
IronRuby types are no CLR types, but they can act as 
CLR types, when required. Typically CLR types differ 
from Iron* types as they can not be modified at run 
time. 
● E.g. creating assemblies from Python scripts: Use 
ipy.exe with the option "-X:SafeAssemblies". (In such 
an assembly you can inspect the DLR CallSite-code 
created for IronPython (e.g. in Reflector)!) At the time of 
this writing (September 2011) the Iron* compilers don't 
create CLS-compliant assemblies, this means they 
probably can't be used in other .Net languages.
9 
Hosting Scripting Languages 
● Scripting languages have been used as glue languages in past. 
– Hosting scenarios allow scripts to be a substantial part of an application. 
– The DLR enables seamless or restricted usage of scripting language as you configure! 
– Hosting can be controlled via App.config (requires no recompilation of your C# code). 
● Exploit collaboration where required: 
– The communication between .Net and DLR-based languages requires no wrappers. 
● Dynamic dispatch enables this. 
– Different dynamic languages use one GC on the DLR! 
● Establish isolation where required: 
– E.g. Iron* can be hosted in a dedicated AppDomain or process, with a different trust. 
● Also on a dedicated physical machine. 
● The intend is to run user scripts with a different security access level (SAL). 
● The DLR is not able to host a static language within 
another static language or a static language within 
another dynamic language. But hosting of static (.Net) 
languages is in sight with MS's research on "compilers 
as a service" (CAAS) with the project "Roslyn". 
● The DLR hosting API consists of two sets of APIs, one 
for language consumers and the other for language 
providers. 
● The file App.config can e.g. control the discovering of 
script engines). 
● Isolation: the relevant DLR scripting types inherit 
MarshalByRefObject, thus enabling .Net Remoting.
10 
The "Big Iron* Hosting Example" 
<IronRubyHosting> 
This set of examples shows hosting of the scripting language 
IronRuby. 
● IronRuby is to be understood as placeholder for 
any other DLR/.Net based scripting language, like 
IronPython or IronScheme.
11 
DLR in the Web 
● I.e. running the DLR within the browser. 
● ASP-based DLR solutions exist as Codeplex projects, but w/o clear roadmap. 
● Silverlight can script JavaScript via DLR. 
– Other languages (Iron* languages) can be scripted, e.g. via Chiron. 
● Chiron is a packager, packaging Iron* languages' code into Silverlight applications (like class libraries). 
– A modern approach is "Gestalt", which allows "just text" hosting of Iron* languages. 
● "Just text" allows scripting of HTML and XAML with Iron* languages much like JavaScript. 
● Gestalt exploits the Silverlight runtime to host scripting languages used in a site via the DLR. 
● ASP.Net MVC allows using dynamic coding to wire Controllers and Views.
12 
Other new Features in .Net 4 
● Side-by-side (SxS) hosting of different CLRs within one process. 
● New and updated .Net frameworks: 
– New: Parallel Extensions Framework (PXF) 
● Task Parallel Library (TPL), Parallel LINQ (PLINQ) and the types Task and Parallel. 
– New: Managed Extensibility Framework (MEF) and Code Contracts 
– Updates: the new Workflow Foundation (WF) 
● New types: 
– Tuple<T, ...>, IStructuralEquatable and IStructuralComparable, 
– ISet<T>, SortedSet<T>, new types in namespace System.Collections.Concurrent, 
– the namespace System.IO.MemoryMappedFiles, 
– Lazy<T>, IObserver<T> and IObservable<T> and more ... 
– Action<T> and Func<T> are now taking up to 16 arguments. 
– New namespace System.Numeric containing the types BigInteger and Complex. 
● SxS works for CLRs version > 2.0, the hosting 
works with different AppDomains. 
● Memory mapped files is a powerful means to 
operate on large data in memory (it is much faster 
than MemoryStreams) and to establish 
communication between processes. 
● New overloads of Action<T> and Func<T>: They 
are present primarily to help support the DLR.
13 
Thank you!

More Related Content

What's hot

Introduction to llvm
Introduction to llvmIntroduction to llvm
Introduction to llvmTao He
 
.Net framework components by naveen kumar veligeti
.Net framework components by naveen kumar veligeti.Net framework components by naveen kumar veligeti
.Net framework components by naveen kumar veligetiNaveen Kumar Veligeti
 
ASP.NET Session 1
ASP.NET Session 1ASP.NET Session 1
ASP.NET Session 1Sisir Ghosh
 
Nakov dot net-framework-overview-english
Nakov dot net-framework-overview-englishNakov dot net-framework-overview-english
Nakov dot net-framework-overview-englishsrivathsan.10
 
Jfokus 2016 - A JVMs Journey into Polyglot Runtimes
Jfokus 2016 - A JVMs Journey into Polyglot RuntimesJfokus 2016 - A JVMs Journey into Polyglot Runtimes
Jfokus 2016 - A JVMs Journey into Polyglot RuntimesCharlie Gracie
 
LinkedIn - Disassembling Dalvik Bytecode
LinkedIn - Disassembling Dalvik BytecodeLinkedIn - Disassembling Dalvik Bytecode
LinkedIn - Disassembling Dalvik BytecodeAlain Leon
 
Java vs .net
Java vs .netJava vs .net
Java vs .netTech_MX
 
P4 P Update January 2009
P4 P Update January 2009P4 P Update January 2009
P4 P Update January 2009vsainteluce
 
Net serialization
Net serializationNet serialization
Net serializationGreg Sohl
 
A Plan towards Ruby 3 Types
A Plan towards Ruby 3 TypesA Plan towards Ruby 3 Types
A Plan towards Ruby 3 Typesmametter
 
ARB_gl_spirv implementation in Mesa: status update (XDC 2018)
ARB_gl_spirv implementation in Mesa: status update (XDC 2018)ARB_gl_spirv implementation in Mesa: status update (XDC 2018)
ARB_gl_spirv implementation in Mesa: status update (XDC 2018)Igalia
 
Introduction to Kotlin coroutines
Introduction to Kotlin coroutinesIntroduction to Kotlin coroutines
Introduction to Kotlin coroutinesRoman Elizarov
 
Agile DSL Development in Ruby
Agile DSL Development in RubyAgile DSL Development in Ruby
Agile DSL Development in Rubyelliando dias
 

What's hot (20)

Introduction to llvm
Introduction to llvmIntroduction to llvm
Introduction to llvm
 
.Net framework components by naveen kumar veligeti
.Net framework components by naveen kumar veligeti.Net framework components by naveen kumar veligeti
.Net framework components by naveen kumar veligeti
 
ASP.NET Session 1
ASP.NET Session 1ASP.NET Session 1
ASP.NET Session 1
 
Nakov dot net-framework-overview-english
Nakov dot net-framework-overview-englishNakov dot net-framework-overview-english
Nakov dot net-framework-overview-english
 
Jfokus 2016 - A JVMs Journey into Polyglot Runtimes
Jfokus 2016 - A JVMs Journey into Polyglot RuntimesJfokus 2016 - A JVMs Journey into Polyglot Runtimes
Jfokus 2016 - A JVMs Journey into Polyglot Runtimes
 
LinkedIn - Disassembling Dalvik Bytecode
LinkedIn - Disassembling Dalvik BytecodeLinkedIn - Disassembling Dalvik Bytecode
LinkedIn - Disassembling Dalvik Bytecode
 
C Course Material0209
C Course Material0209C Course Material0209
C Course Material0209
 
Java vs .net
Java vs .netJava vs .net
Java vs .net
 
Ebay News 2001 4 19 Earnings
Ebay News 2001 4 19 EarningsEbay News 2001 4 19 Earnings
Ebay News 2001 4 19 Earnings
 
Ebay News 2000 10 19 Earnings
Ebay News 2000 10 19 EarningsEbay News 2000 10 19 Earnings
Ebay News 2000 10 19 Earnings
 
P4 P Update January 2009
P4 P Update January 2009P4 P Update January 2009
P4 P Update January 2009
 
Net serialization
Net serializationNet serialization
Net serialization
 
A Plan towards Ruby 3 Types
A Plan towards Ruby 3 TypesA Plan towards Ruby 3 Types
A Plan towards Ruby 3 Types
 
ARB_gl_spirv implementation in Mesa: status update (XDC 2018)
ARB_gl_spirv implementation in Mesa: status update (XDC 2018)ARB_gl_spirv implementation in Mesa: status update (XDC 2018)
ARB_gl_spirv implementation in Mesa: status update (XDC 2018)
 
Introduction to Kotlin coroutines
Introduction to Kotlin coroutinesIntroduction to Kotlin coroutines
Introduction to Kotlin coroutines
 
Memory models in c#
Memory models in c#Memory models in c#
Memory models in c#
 
C#
C#C#
C#
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
 
Agile DSL Development in Ruby
Agile DSL Development in RubyAgile DSL Development in Ruby
Agile DSL Development in Ruby
 
2CPP02 - C++ Primer
2CPP02 - C++ Primer2CPP02 - C++ Primer
2CPP02 - C++ Primer
 

Similar to New c sharp4_features_part_vi

New c sharp4_features_part_iv
New c sharp4_features_part_ivNew c sharp4_features_part_iv
New c sharp4_features_part_ivNico Ludwig
 
A Comparison of .NET Framework vs. Java Virtual Machine
A Comparison of .NET Framework vs. Java Virtual MachineA Comparison of .NET Framework vs. Java Virtual Machine
A Comparison of .NET Framework vs. Java Virtual MachineAbdelrahman Hosny
 
Building a high-performance, scalable ML & NLP platform with Python, Sheer El...
Building a high-performance, scalable ML & NLP platform with Python, Sheer El...Building a high-performance, scalable ML & NLP platform with Python, Sheer El...
Building a high-performance, scalable ML & NLP platform with Python, Sheer El...Pôle Systematic Paris-Region
 
ConFoo Montreal - Microservices for building an IDE - The innards of JetBrain...
ConFoo Montreal - Microservices for building an IDE - The innards of JetBrain...ConFoo Montreal - Microservices for building an IDE - The innards of JetBrain...
ConFoo Montreal - Microservices for building an IDE - The innards of JetBrain...Maarten Balliauw
 
.NET Core, ASP.NET Core Course, Session 3
.NET Core, ASP.NET Core Course, Session 3.NET Core, ASP.NET Core Course, Session 3
.NET Core, ASP.NET Core Course, Session 3aminmesbahi
 
Ruby on Rails (RoR) as a back-end processor for Apex
Ruby on Rails (RoR) as a back-end processor for Apex Ruby on Rails (RoR) as a back-end processor for Apex
Ruby on Rails (RoR) as a back-end processor for Apex Espen Brækken
 
.Net overview|Introduction Of .net
.Net overview|Introduction Of .net.Net overview|Introduction Of .net
.Net overview|Introduction Of .netpinky singh
 
Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....
Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....
Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....AboutYouGmbH
 
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...AboutYouGmbH
 
New c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_ivNew c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_ivNico Ludwig
 
Copmuter Languages
Copmuter LanguagesCopmuter Languages
Copmuter Languagesactanimation
 
Introduction of A Lightweight Stage-Programming Framework
Introduction of A Lightweight Stage-Programming FrameworkIntroduction of A Lightweight Stage-Programming Framework
Introduction of A Lightweight Stage-Programming FrameworkYu Liu
 
New c sharp4_features_part_iii
New c sharp4_features_part_iiiNew c sharp4_features_part_iii
New c sharp4_features_part_iiiNico Ludwig
 
Object Oriented Programming with COBOL
Object Oriented Programming with COBOLObject Oriented Programming with COBOL
Object Oriented Programming with COBOLMicro Focus
 
.Net introduction by Quontra Solutions
.Net introduction by Quontra Solutions.Net introduction by Quontra Solutions
.Net introduction by Quontra SolutionsQUONTRASOLUTIONS
 
New c sharp3_features_(linq)_part_ii
New c sharp3_features_(linq)_part_iiNew c sharp3_features_(linq)_part_ii
New c sharp3_features_(linq)_part_iiNico Ludwig
 

Similar to New c sharp4_features_part_vi (20)

New c sharp4_features_part_iv
New c sharp4_features_part_ivNew c sharp4_features_part_iv
New c sharp4_features_part_iv
 
A Comparison of .NET Framework vs. Java Virtual Machine
A Comparison of .NET Framework vs. Java Virtual MachineA Comparison of .NET Framework vs. Java Virtual Machine
A Comparison of .NET Framework vs. Java Virtual Machine
 
Introduction to .net
Introduction to .netIntroduction to .net
Introduction to .net
 
Building a high-performance, scalable ML & NLP platform with Python, Sheer El...
Building a high-performance, scalable ML & NLP platform with Python, Sheer El...Building a high-performance, scalable ML & NLP platform with Python, Sheer El...
Building a high-performance, scalable ML & NLP platform with Python, Sheer El...
 
ConFoo Montreal - Microservices for building an IDE - The innards of JetBrain...
ConFoo Montreal - Microservices for building an IDE - The innards of JetBrain...ConFoo Montreal - Microservices for building an IDE - The innards of JetBrain...
ConFoo Montreal - Microservices for building an IDE - The innards of JetBrain...
 
.NET Core, ASP.NET Core Course, Session 3
.NET Core, ASP.NET Core Course, Session 3.NET Core, ASP.NET Core Course, Session 3
.NET Core, ASP.NET Core Course, Session 3
 
Ruby on Rails (RoR) as a back-end processor for Apex
Ruby on Rails (RoR) as a back-end processor for Apex Ruby on Rails (RoR) as a back-end processor for Apex
Ruby on Rails (RoR) as a back-end processor for Apex
 
.Net overview|Introduction Of .net
.Net overview|Introduction Of .net.Net overview|Introduction Of .net
.Net overview|Introduction Of .net
 
Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....
Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....
Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....
 
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
 
sl slides-unit-1.pptx
sl slides-unit-1.pptxsl slides-unit-1.pptx
sl slides-unit-1.pptx
 
New c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_ivNew c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_iv
 
Copmuter Languages
Copmuter LanguagesCopmuter Languages
Copmuter Languages
 
Apache Drill (ver. 0.2)
Apache Drill (ver. 0.2)Apache Drill (ver. 0.2)
Apache Drill (ver. 0.2)
 
Introduction of A Lightweight Stage-Programming Framework
Introduction of A Lightweight Stage-Programming FrameworkIntroduction of A Lightweight Stage-Programming Framework
Introduction of A Lightweight Stage-Programming Framework
 
New c sharp4_features_part_iii
New c sharp4_features_part_iiiNew c sharp4_features_part_iii
New c sharp4_features_part_iii
 
Object Oriented Programming with COBOL
Object Oriented Programming with COBOLObject Oriented Programming with COBOL
Object Oriented Programming with COBOL
 
.Net introduction by Quontra Solutions
.Net introduction by Quontra Solutions.Net introduction by Quontra Solutions
.Net introduction by Quontra Solutions
 
Inside.Net
Inside.NetInside.Net
Inside.Net
 
New c sharp3_features_(linq)_part_ii
New c sharp3_features_(linq)_part_iiNew c sharp3_features_(linq)_part_ii
New c sharp3_features_(linq)_part_ii
 

More from Nico Ludwig

Grundkurs fuer excel_part_v
Grundkurs fuer excel_part_vGrundkurs fuer excel_part_v
Grundkurs fuer excel_part_vNico Ludwig
 
Grundkurs fuer excel_part_iv
Grundkurs fuer excel_part_ivGrundkurs fuer excel_part_iv
Grundkurs fuer excel_part_ivNico Ludwig
 
Grundkurs fuer excel_part_iii
Grundkurs fuer excel_part_iiiGrundkurs fuer excel_part_iii
Grundkurs fuer excel_part_iiiNico Ludwig
 
Grundkurs fuer excel_part_ii
Grundkurs fuer excel_part_iiGrundkurs fuer excel_part_ii
Grundkurs fuer excel_part_iiNico Ludwig
 
Grundkurs fuer excel_part_i
Grundkurs fuer excel_part_iGrundkurs fuer excel_part_i
Grundkurs fuer excel_part_iNico Ludwig
 
(1) gui history_of_interactivity
(1) gui history_of_interactivity(1) gui history_of_interactivity
(1) gui history_of_interactivityNico Ludwig
 
(1) gui history_of_interactivity
(1) gui history_of_interactivity(1) gui history_of_interactivity
(1) gui history_of_interactivityNico Ludwig
 
New c sharp4_features_part_ii
New c sharp4_features_part_iiNew c sharp4_features_part_ii
New c sharp4_features_part_iiNico Ludwig
 
New c sharp4_features_part_i
New c sharp4_features_part_iNew c sharp4_features_part_i
New c sharp4_features_part_iNico Ludwig
 
New c sharp3_features_(linq)_part_v
New c sharp3_features_(linq)_part_vNew c sharp3_features_(linq)_part_v
New c sharp3_features_(linq)_part_vNico Ludwig
 
New c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_ivNew c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_ivNico Ludwig
 
New c sharp3_features_(linq)_part_iii
New c sharp3_features_(linq)_part_iiiNew c sharp3_features_(linq)_part_iii
New c sharp3_features_(linq)_part_iiiNico Ludwig
 
New c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_iNew c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_iNico Ludwig
 
New c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_iNew c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_iNico Ludwig
 
Review of c_sharp2_features_part_iii
Review of c_sharp2_features_part_iiiReview of c_sharp2_features_part_iii
Review of c_sharp2_features_part_iiiNico Ludwig
 
Review of c_sharp2_features_part_ii
Review of c_sharp2_features_part_iiReview of c_sharp2_features_part_ii
Review of c_sharp2_features_part_iiNico Ludwig
 
Review of c_sharp2_features_part_i
Review of c_sharp2_features_part_iReview of c_sharp2_features_part_i
Review of c_sharp2_features_part_iNico Ludwig
 
(7) c sharp introduction_advanvced_features_part_ii
(7) c sharp introduction_advanvced_features_part_ii(7) c sharp introduction_advanvced_features_part_ii
(7) c sharp introduction_advanvced_features_part_iiNico Ludwig
 

More from Nico Ludwig (20)

Grundkurs fuer excel_part_v
Grundkurs fuer excel_part_vGrundkurs fuer excel_part_v
Grundkurs fuer excel_part_v
 
Grundkurs fuer excel_part_iv
Grundkurs fuer excel_part_ivGrundkurs fuer excel_part_iv
Grundkurs fuer excel_part_iv
 
Grundkurs fuer excel_part_iii
Grundkurs fuer excel_part_iiiGrundkurs fuer excel_part_iii
Grundkurs fuer excel_part_iii
 
Grundkurs fuer excel_part_ii
Grundkurs fuer excel_part_iiGrundkurs fuer excel_part_ii
Grundkurs fuer excel_part_ii
 
Grundkurs fuer excel_part_i
Grundkurs fuer excel_part_iGrundkurs fuer excel_part_i
Grundkurs fuer excel_part_i
 
(2) gui drawing
(2) gui drawing(2) gui drawing
(2) gui drawing
 
(2) gui drawing
(2) gui drawing(2) gui drawing
(2) gui drawing
 
(1) gui history_of_interactivity
(1) gui history_of_interactivity(1) gui history_of_interactivity
(1) gui history_of_interactivity
 
(1) gui history_of_interactivity
(1) gui history_of_interactivity(1) gui history_of_interactivity
(1) gui history_of_interactivity
 
New c sharp4_features_part_ii
New c sharp4_features_part_iiNew c sharp4_features_part_ii
New c sharp4_features_part_ii
 
New c sharp4_features_part_i
New c sharp4_features_part_iNew c sharp4_features_part_i
New c sharp4_features_part_i
 
New c sharp3_features_(linq)_part_v
New c sharp3_features_(linq)_part_vNew c sharp3_features_(linq)_part_v
New c sharp3_features_(linq)_part_v
 
New c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_ivNew c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_iv
 
New c sharp3_features_(linq)_part_iii
New c sharp3_features_(linq)_part_iiiNew c sharp3_features_(linq)_part_iii
New c sharp3_features_(linq)_part_iii
 
New c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_iNew c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_i
 
New c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_iNew c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_i
 
Review of c_sharp2_features_part_iii
Review of c_sharp2_features_part_iiiReview of c_sharp2_features_part_iii
Review of c_sharp2_features_part_iii
 
Review of c_sharp2_features_part_ii
Review of c_sharp2_features_part_iiReview of c_sharp2_features_part_ii
Review of c_sharp2_features_part_ii
 
Review of c_sharp2_features_part_i
Review of c_sharp2_features_part_iReview of c_sharp2_features_part_i
Review of c_sharp2_features_part_i
 
(7) c sharp introduction_advanvced_features_part_ii
(7) c sharp introduction_advanvced_features_part_ii(7) c sharp introduction_advanvced_features_part_ii
(7) c sharp introduction_advanvced_features_part_ii
 

New c sharp4_features_part_vi

  • 1. 1 New C#4 Features – Part VI Nico Ludwig (@ersatzteilchen)
  • 2. 2 TOC ● New C#4 Features – Part VI – Beyond Expando Objects: Home-brew Dynamic Dispatch – Hosting Scripting Languages ● Sources: – Jon Skeet, CSharp in Depth
  • 3. 3 If ExpandoObject is not enough ● We can get very far with ExpandoObject, but data driven APIs require more. – The possibility to encapsulate an existing API is often the core paradigm. – Adding user defined behavior to get more control (e.g. enforce readonly objects). – Sometimes you need or want to deal with operators. – The class ExpandoObject is sealed, and can not act as base class. ● Up to now we've exploited the DLR from the caller side, now we'll implement own dynamic behavior in C#. – We can create types, whose interfaces change during run time; and these interface changes are based on how we use the type. – That means we'll implement our own dynamic dispatch. ● ExpandoObject instances behave differently in VB and in C#, because in VB symbols are case insensitive, but in C# they are case sensitive!
  • 4. 4 DynamicObject in Action <DynamicObjects> This example shows home brew dynamic dispatch with the CLR type DynamicObject. Key aspects of dynamic programming: (1. Dynamic Typing) (2. Late Binding) (3. Duck Typing) 4. Dynamic Objects
  • 5. 5 Home-brew Dynamic Dispatch with DynamicObject ● If you need more than dynamic property bags you can handle the dynamic dispatching yourself in a type derived from DynamicObject. – E.g. to create methods and properties at run time to create explorative interfaces. ● E.g. to wrap another API. – DynamicObject allows you to dispatch all (twelve) dynamic operations yourself. – We have the same abilities to handle unknown methods as in Smalltalk and Obj-C! ● Instead of overriding "doesNotUnderstand:" (Smalltalk) we have to override "TryInvokeMember()". ● How to make use of DynamicObject: – Import the System.Dynamic namespace. – Inherit from DynamicObject. – Then you can override the behavior of dynamic operations or define new methods. ● If you can not derive from DynamicObject, implement IDynamicMetaObjectProvider. – But meta programming with Expression trees is not that simple... ● In the accompanying source code, you can find richly commented Smalltalk, Obj-C, Python and Ruby implementations of a property bag. (Please have a look, it is really not difficult to understand in that languages!) ● It is planned to have a similar home-brew dynamic dispatch support in Scala. ● If you implement IDynamicMetaObjectProvider you have to program a DynamicMetaObject, in which you code how dynamic operations translate into Expression trees. This flexibility comes with a cost: complexity, as coding of Expression trees, can be very complicated. ● In .Net 4.5 there'll be a new type, JsonValue, which allows simple serialization of Json-formatted data. In order to be programmable in a simple way, it implements IDynamicMetaObjectProvide and can be used like ExpandoObject to add new Json elements. ● So if the out-of-the-box abilities of the DLR are not suf cient for your scenario, you can write own binders and meta-objects.
  • 6. 6 Mechanics of Dynamic Dispatch ● The compiler lets us pass any kind of operation using the dynamic keyword. – And the real type has to handle the passed operation. – But the operation needs not to be a part of the real type's static interface. – The idea is to mix dynamic dispatch with static invocations in a single type. – This can be done with dynamic objects. ● Restrictions: – To inspect the run time type of DynamicObject you can't use reflection. ● (Also you can not check, whether a variable was declared as dynamic reference at run time.) ● As its type (e.g. the properties and methods) is part of its value. ● You absolutely have to document how your DynamicObject works! – No excuses! The compiler can't help here! – Best practice: write unit tests documenting your code!
  • 7. 7 Dynamic Languages based on .Net ● Meanwhile there exist a lot of dynamic .Net languages based on the DLR. – Iron*, like IronRuby, IronPython, IronScheme etc.... – Inspired by the success of Jython on the JVM, IronPython was developed. – During run time, scripts get translated into IL code and then executed by the CLR. ● The new features of Expression trees support enabling scripting with the DLR. – The compiler/interpreter pipeline of a DLR-based scripting engine: ● 1. Lex and parse the script => Expression trees, or abstract syntax trees (AST), ● 2. pass the AST to the DLR, ● 3. the DLR then compiles the AST to IL and executes it on the CLR, ● 4. finally the CLR could JIT IL code to native machine code. – Expression trees are to the DLR what IL is to the CLR. ● Some scripting engines can run in compiler mode. – Then methods get compiled into IL code (w/o the Expression tree step). ● IronPython 1.0 was even faster than C-based Python, because .Net Delegate-invocations are very fast since .Net 2 (CPython, Jython do only have an own runtime (different from .Net)). So, yes, there exist also variants of the DLR (and IronRuby and IronPython) that run on .Net 2 (then you’ll need to deploy the DLR (Microsoft.Scripting.dll, Microsoft.Dynamic.dll etc.) yourself). ● E.g. IronRuby has a compiler mode (default). In opposite to the compiler mode the interpreter mode has a better startup time, but the run time is worse.
  • 8. 8 The Iron Languages ● The DLR and IronPython have been developed by the same team until 2006. – The DLR, IronPython and IronRuby (2008) are all open source (Apache lic. v2). ● Iron* languages offer great integration with .Net: – Easy to use .Net libraries and other .Net languages. – Easy to use on .Net hosts and with .Net tools. ● Iron* languages compile to IL code during run time. – During run time: Created Expression trees will finally be compiled to IL code. ● And the JIT compiler will compile the IL code to machine code. ● Or you can compile your script code into an assembly! ● But we also have two type systems to deal with: .Net or the Iron* language's one. – Ruby has mutable strings, .Net don't (there is a to_clr_string() conversion method). ● The open source variant of the DLR (hosted on CodePlex) develops faster currently. ● The DLR in .Net resides in System.Core.dll. ● IRON: (kidding) Implementation Running on .Net. ● For this presentation: ● IronRuby 1.1.3 was installed as NuGet package for the project. ● The conversion between Iron* and CLR types does often happen automatically, when it should. E.g. IronRuby types are no CLR types, but they can act as CLR types, when required. Typically CLR types differ from Iron* types as they can not be modified at run time. ● E.g. creating assemblies from Python scripts: Use ipy.exe with the option "-X:SafeAssemblies". (In such an assembly you can inspect the DLR CallSite-code created for IronPython (e.g. in Reflector)!) At the time of this writing (September 2011) the Iron* compilers don't create CLS-compliant assemblies, this means they probably can't be used in other .Net languages.
  • 9. 9 Hosting Scripting Languages ● Scripting languages have been used as glue languages in past. – Hosting scenarios allow scripts to be a substantial part of an application. – The DLR enables seamless or restricted usage of scripting language as you configure! – Hosting can be controlled via App.config (requires no recompilation of your C# code). ● Exploit collaboration where required: – The communication between .Net and DLR-based languages requires no wrappers. ● Dynamic dispatch enables this. – Different dynamic languages use one GC on the DLR! ● Establish isolation where required: – E.g. Iron* can be hosted in a dedicated AppDomain or process, with a different trust. ● Also on a dedicated physical machine. ● The intend is to run user scripts with a different security access level (SAL). ● The DLR is not able to host a static language within another static language or a static language within another dynamic language. But hosting of static (.Net) languages is in sight with MS's research on "compilers as a service" (CAAS) with the project "Roslyn". ● The DLR hosting API consists of two sets of APIs, one for language consumers and the other for language providers. ● The file App.config can e.g. control the discovering of script engines). ● Isolation: the relevant DLR scripting types inherit MarshalByRefObject, thus enabling .Net Remoting.
  • 10. 10 The "Big Iron* Hosting Example" <IronRubyHosting> This set of examples shows hosting of the scripting language IronRuby. ● IronRuby is to be understood as placeholder for any other DLR/.Net based scripting language, like IronPython or IronScheme.
  • 11. 11 DLR in the Web ● I.e. running the DLR within the browser. ● ASP-based DLR solutions exist as Codeplex projects, but w/o clear roadmap. ● Silverlight can script JavaScript via DLR. – Other languages (Iron* languages) can be scripted, e.g. via Chiron. ● Chiron is a packager, packaging Iron* languages' code into Silverlight applications (like class libraries). – A modern approach is "Gestalt", which allows "just text" hosting of Iron* languages. ● "Just text" allows scripting of HTML and XAML with Iron* languages much like JavaScript. ● Gestalt exploits the Silverlight runtime to host scripting languages used in a site via the DLR. ● ASP.Net MVC allows using dynamic coding to wire Controllers and Views.
  • 12. 12 Other new Features in .Net 4 ● Side-by-side (SxS) hosting of different CLRs within one process. ● New and updated .Net frameworks: – New: Parallel Extensions Framework (PXF) ● Task Parallel Library (TPL), Parallel LINQ (PLINQ) and the types Task and Parallel. – New: Managed Extensibility Framework (MEF) and Code Contracts – Updates: the new Workflow Foundation (WF) ● New types: – Tuple<T, ...>, IStructuralEquatable and IStructuralComparable, – ISet<T>, SortedSet<T>, new types in namespace System.Collections.Concurrent, – the namespace System.IO.MemoryMappedFiles, – Lazy<T>, IObserver<T> and IObservable<T> and more ... – Action<T> and Func<T> are now taking up to 16 arguments. – New namespace System.Numeric containing the types BigInteger and Complex. ● SxS works for CLRs version > 2.0, the hosting works with different AppDomains. ● Memory mapped files is a powerful means to operate on large data in memory (it is much faster than MemoryStreams) and to establish communication between processes. ● New overloads of Action<T> and Func<T>: They are present primarily to help support the DLR.