SlideShare a Scribd company logo
1 of 9
Download to read offline
1 
New C#4 Features – Part V 
Nico Ludwig (@ersatzteilchen)
2 
TOC 
● New C#4 Features – Part V 
– Dynamic COM Automation with C# 
– Dynamic Objects in JavaScript 
– Expando Objects in C# 
● Sources: 
– Jon Skeet, CSharp in Depth
3 
Dynamic Coding – A better Experience with COM 
● You no longer need interop types: 
– Just don't use them: 
● if you want lazy dispatching and you don't bother finding method signatures yourself... 
● or if you simply don't want to or can't use type infos (w/o type library)! - You just use dynamic. 
– (Use them if you want to exploit early dispatching, e.g. if you want to use IntelliSense.) 
– To have no need to create code as an extra step is a key benefit of dynamic coding. 
● But if you use interop types w/o PIAs, the DLR still makes your life easier: 
– No-PIA deployed types provide the type dynamic instead of object in their APIs. 
– E.g. IDispatch and VARIANTs are exposed as dynamics, reducing cast operations. 
● With dynamic typing and COM you now have a "natural" coding experience: 
– "Plain old CLR objects" (POCOs) and COM objects can be programmed like peers. 
– During debugging the new "Dynamic View" assists you watching COM objects. 
● COM as "peer objects" is one advantage of VB and 
PowerShell programming.
4 
Example with COM Automation 
<DynamicComInteropCSharp> 
aThis example shows the application of C# dynamic coding to allow 
simple COM automation.
5 
Example: Transportation of Anonymous Types' Instances 
● Passing or returning anonymous types' instances from or to methods is limited. 
– Feasible solutions like generic methods or using the type Object are quite limiting... 
– You could only call Object's methods within such a method, not the anonymous type's ones (well, you might use reflection...). 
● With dynamic you can overcome these limits. 
● You'll get following features: 
– Easy to use adapters. 
– But data-binding, e.g. from IList<dynamic> to your controls is still fully functional (Windows Forms and WPF). 
● !!You can't access properties of anonymous types in non-friend assemblies!! 
– Anonymous types are internal and accessing them publicly may fail. 
– => Keep using them in the same assembly! 
● This is just one selected example of how dynamic typing 
simplifies coding problems. Passing anonymous types' 
instances to methods accepting dynamic arguments is used 
with view models in ASP.Net MVC. 
● Basically dynamic makes it possible to use otherwise only 
locally known anonymous types in other methods. 
● Databinding in WPF is less "sensitive", because it also 
accepts different types having equally named properties. The 
default behavior in Windows Forms requires identical types. 
● Situation in non-friend assemblies: On accessing the 
anonymous type's instance's properties (via a dynamically 
typed symbol) you'll get a RuntimeBinderException on the 
caller side, because the properties couldn't be bound. 
● More: 
● The type dynamic enables us to solve a problem on 
programming of generics: we can call arbitrary methods on 
unconstrained types' instances! Esp. we can call operators 
in generics with dynamic dispatching! 
● .Net remoting: Also can we use dynamic dispatch to access 
the interface of proxy objects on marshaled instances w/o 
having the object's type in avail.
6 
Leering at JavaScript: Dynamic Objects 
<Safari Web Inspector as REPL> 
Shows how we can exploit dynamic objects with dynamic 
properties in JavaScript. 
Key aspects of dynamic programming: 
(1. Dynamic Typing) 
(2. Late Binding) 
(3. Duck Typing) 
4. Dynamic Objects 
● The ability to modify objects during run time is 
another aspect of dynamic programming that is 
shown here. -> Dynamic objects.
7 
Dynamic Objects with .Net ExpandoObjects 
● Dynamic programming allows creating types, whose interfaces change at run time. 
– These interface changes are based on how you use instances of such dynamic types. 
● Dynamic objects are objects that have their own late binding logic! 
– (In contrast to static objects that get bound by the compiler (early binding).) 
– We can create own dynamic APIs to put mighty data driven APIs into effect! 
– The easiest way to get this is a dynamic property bag, via the type ExpandoObject. 
– ExpandoObjects are similar to JavaScript's dynamic objects that work like dictionaries. 
– In many dynamic languages (e.g. JavaScript, Python) all objects are property bags. 
● How to make use of .Net's ExpandoObject: 
– Import the namespace System.Dynamic. 
– Create a new ExpandoObject and assign it to a dynamic to enable dynamic dispatch. 
– Add/modify members during run time and exploit ExpandoObjects as dictionaries. 
● In static typing types are immutable, if you don’t have 
the source code. - In dynamic typing this is no longer 
the case, because the dynamic type information of an 
object is part of the object's value! 
● You should also check, whether a member of a 
dynamic object is really present, before accessing it 
(RuntimeBinderException). - This is a duck-typing 
aspect (Does "it" lay eggs?)! 
● As a matter of fact in .Net there also exists the type 
IExpando, which is implemented by JScript .Net objects 
(and by dynamic objects in some situations). 
● ExpandoObjects can also have functions as properties 
of a Delegate type! 
● Interoperability when hosting a DLR scripting language: 
Expose your C# objects to IronRuby as 
ExpandoObjects! 
● The class ExpandoObject is sealed. The discussion of 
more flexible dynamic objects is discussed in the next 
lecture.
8 
ExpandoObjects in Action 
<DynamicObjects> 
Shows the basic usage of ExpandoObjects. 
Key aspects of dynamic programming: 
(1. Dynamic Typing) 
(2. Late Binding) 
(3. Duck Typing) 
4. Dynamic Objects
9 
Thank you!

More Related Content

What's hot

A Static Type Analyzer of Untyped Ruby Code for Ruby 3
A Static Type Analyzer of Untyped Ruby Code for Ruby 3A Static Type Analyzer of Untyped Ruby Code for Ruby 3
A Static Type Analyzer of Untyped Ruby Code for Ruby 3
mametter
 

What's hot (19)

TypeScript: Angular's Secret Weapon
TypeScript: Angular's Secret WeaponTypeScript: Angular's Secret Weapon
TypeScript: Angular's Secret Weapon
 
Typescript for the programmers who like javascript
Typescript for the programmers who like javascriptTypescript for the programmers who like javascript
Typescript for the programmers who like javascript
 
Metaprogramming with javascript
Metaprogramming with javascriptMetaprogramming with javascript
Metaprogramming with javascript
 
FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...
FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...
FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...
 
Comparing Golang and understanding Java Value Types
Comparing Golang and understanding Java Value TypesComparing Golang and understanding Java Value Types
Comparing Golang and understanding Java Value Types
 
Swift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming languageSwift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming language
 
TypeScript 101
TypeScript 101TypeScript 101
TypeScript 101
 
Kotlin & arrow: the functional way
Kotlin & arrow:  the functional wayKotlin & arrow:  the functional way
Kotlin & arrow: the functional way
 
Introduction to f#
Introduction to f#Introduction to f#
Introduction to f#
 
TypeScript Modules
TypeScript ModulesTypeScript Modules
TypeScript Modules
 
Groovy / comparison with java
Groovy / comparison with javaGroovy / comparison with java
Groovy / comparison with java
 
A Static Type Analyzer of Untyped Ruby Code for Ruby 3
A Static Type Analyzer of Untyped Ruby Code for Ruby 3A Static Type Analyzer of Untyped Ruby Code for Ruby 3
A Static Type Analyzer of Untyped Ruby Code for Ruby 3
 
Javascript omg!
Javascript omg!Javascript omg!
Javascript omg!
 
Kotlin & Arrow the functional way
Kotlin & Arrow the functional wayKotlin & Arrow the functional way
Kotlin & Arrow the functional way
 
Akka actorstotherescue nirmalya sengupta
Akka actorstotherescue nirmalya senguptaAkka actorstotherescue nirmalya sengupta
Akka actorstotherescue nirmalya sengupta
 
C#
C#C#
C#
 
C#
C#C#
C#
 
C#
C#C#
C#
 
Understanding Typing. Understanding Ruby.
Understanding Typing. Understanding Ruby.Understanding Typing. Understanding Ruby.
Understanding Typing. Understanding Ruby.
 

Viewers also liked

SVR17: Data-Intensive Computing on Windows HPC Server with the ...
SVR17: Data-Intensive Computing on Windows HPC Server with the ...SVR17: Data-Intensive Computing on Windows HPC Server with the ...
SVR17: Data-Intensive Computing on Windows HPC Server with the ...
butest
 
Introduction to Reactive Extensions
Introduction to Reactive ExtensionsIntroduction to Reactive Extensions
Introduction to Reactive Extensions
Peter Goodman
 
Reactive Extensions
Reactive ExtensionsReactive Extensions
Reactive Extensions
Kiev ALT.NET
 
Pertemuan 1. introduction to image processing
Pertemuan 1. introduction to image processingPertemuan 1. introduction to image processing
Pertemuan 1. introduction to image processing
Aditya Kurniawan
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharp
sarfarazali
 
Getting the MVVM Kicked Out of Your F#'n Monads
Getting the MVVM Kicked Out of Your F#'n MonadsGetting the MVVM Kicked Out of Your F#'n Monads
Getting the MVVM Kicked Out of Your F#'n Monads
Richard Minerich
 
Texas STaR
Texas STaRTexas STaR
Texas STaR
llyarbro
 
F# with Excel at F# in Finance, London Nov 2013
F# with Excel at F# in Finance, London Nov 2013F# with Excel at F# in Finance, London Nov 2013
F# with Excel at F# in Finance, London Nov 2013
Adam Mlocek
 

Viewers also liked (20)

New c sharp4_features_part_iii
New c sharp4_features_part_iiiNew c sharp4_features_part_iii
New c sharp4_features_part_iii
 
SVR17: Data-Intensive Computing on Windows HPC Server with the ...
SVR17: Data-Intensive Computing on Windows HPC Server with the ...SVR17: Data-Intensive Computing on Windows HPC Server with the ...
SVR17: Data-Intensive Computing on Windows HPC Server with the ...
 
New c sharp4_features_part_ii
New c sharp4_features_part_iiNew c sharp4_features_part_ii
New c sharp4_features_part_ii
 
Test first
Test firstTest first
Test first
 
New c sharp4_features_part_vi
New c sharp4_features_part_viNew c sharp4_features_part_vi
New c sharp4_features_part_vi
 
Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05
Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05
Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05
 
New c sharp4_features_part_iv
New c sharp4_features_part_ivNew c sharp4_features_part_iv
New c sharp4_features_part_iv
 
Introduction to F#x
Introduction to F#xIntroduction to F#x
Introduction to F#x
 
Introduction to Reactive Extensions
Introduction to Reactive ExtensionsIntroduction to Reactive Extensions
Introduction to Reactive Extensions
 
Introduction to C# 3.0
Introduction to C# 3.0Introduction to C# 3.0
Introduction to C# 3.0
 
Reactive Extensions
Reactive ExtensionsReactive Extensions
Reactive Extensions
 
Pertemuan 1. introduction to image processing
Pertemuan 1. introduction to image processingPertemuan 1. introduction to image processing
Pertemuan 1. introduction to image processing
 
F:\Universidad Tecnologica Israel
F:\Universidad Tecnologica IsraelF:\Universidad Tecnologica Israel
F:\Universidad Tecnologica Israel
 
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
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharp
 
Getting the MVVM Kicked Out of Your F#'n Monads
Getting the MVVM Kicked Out of Your F#'n MonadsGetting the MVVM Kicked Out of Your F#'n Monads
Getting the MVVM Kicked Out of Your F#'n Monads
 
Texas
TexasTexas
Texas
 
The Power of F#
The Power of F#The Power of F#
The Power of F#
 
Texas STaR
Texas STaRTexas STaR
Texas STaR
 
F# with Excel at F# in Finance, London Nov 2013
F# with Excel at F# in Finance, London Nov 2013F# with Excel at F# in Finance, London Nov 2013
F# with Excel at F# in Finance, London Nov 2013
 

Similar to New c sharp4_features_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
Nico Ludwig
 
ActionScript 3.0 Fundamentals
ActionScript 3.0 FundamentalsActionScript 3.0 Fundamentals
ActionScript 3.0 Fundamentals
Saurabh Narula
 
Write Better JavaScript
Write Better JavaScriptWrite Better JavaScript
Write Better JavaScript
Kevin Whinnery
 
Write Better JavaScript
Write Better JavaScriptWrite Better JavaScript
Write Better JavaScript
Kevin Whinnery
 

Similar to New c sharp4_features_part_v (20)

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
 
TypeScript Overview
TypeScript OverviewTypeScript Overview
TypeScript Overview
 
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
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
ActionScript 3.0 Fundamentals
ActionScript 3.0 FundamentalsActionScript 3.0 Fundamentals
ActionScript 3.0 Fundamentals
 
Kevin Whinnery: Write Better JavaScript
Kevin Whinnery: Write Better JavaScriptKevin Whinnery: Write Better JavaScript
Kevin Whinnery: Write Better JavaScript
 
Bootstrapping iPhone Development
Bootstrapping iPhone DevelopmentBootstrapping iPhone Development
Bootstrapping iPhone Development
 
The advantage of developing with TypeScript
The advantage of developing with TypeScript The advantage of developing with TypeScript
The advantage of developing with TypeScript
 
Memory models in c#
Memory models in c#Memory models in c#
Memory models in c#
 
Libreplan architecture
Libreplan architectureLibreplan architecture
Libreplan architecture
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascript
 
Introducing object oriented programming (oop)
Introducing object oriented programming (oop)Introducing object oriented programming (oop)
Introducing object oriented programming (oop)
 
Write Better JavaScript
Write Better JavaScriptWrite Better JavaScript
Write Better JavaScript
 
Write Better JavaScript
Write Better JavaScriptWrite Better JavaScript
Write Better JavaScript
 
Half-automatic Compilable Source Code Recovery
Half-automatic Compilable Source Code RecoveryHalf-automatic Compilable Source Code Recovery
Half-automatic Compilable Source Code Recovery
 
Functional Programming In Jdk8
Functional Programming In Jdk8 Functional Programming In Jdk8
Functional Programming In Jdk8
 
object oriented programming examples
object oriented programming examplesobject oriented programming examples
object oriented programming examples
 
DDD with Behat
DDD with BehatDDD with Behat
DDD with Behat
 
The Good, the Bad and the Ugly things to do with android
The Good, the Bad and the Ugly things to do with androidThe Good, the Bad and the Ugly things to do with android
The Good, the Bad and the Ugly things to do with android
 
Hsc IT Chap 3. Advanced javascript-1.pdf
Hsc IT Chap 3. Advanced javascript-1.pdfHsc IT Chap 3. Advanced javascript-1.pdf
Hsc IT Chap 3. Advanced javascript-1.pdf
 

More from Nico 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_i
New c sharp4_features_part_iNew c sharp4_features_part_i
New c sharp4_features_part_i
 
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
 
(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
 
(6) c sharp introduction_advanced_features_part_i
(6) c sharp introduction_advanced_features_part_i(6) c sharp introduction_advanced_features_part_i
(6) c sharp introduction_advanced_features_part_i
 

Recently uploaded

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Recently uploaded (20)

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 

New c sharp4_features_part_v

  • 1. 1 New C#4 Features – Part V Nico Ludwig (@ersatzteilchen)
  • 2. 2 TOC ● New C#4 Features – Part V – Dynamic COM Automation with C# – Dynamic Objects in JavaScript – Expando Objects in C# ● Sources: – Jon Skeet, CSharp in Depth
  • 3. 3 Dynamic Coding – A better Experience with COM ● You no longer need interop types: – Just don't use them: ● if you want lazy dispatching and you don't bother finding method signatures yourself... ● or if you simply don't want to or can't use type infos (w/o type library)! - You just use dynamic. – (Use them if you want to exploit early dispatching, e.g. if you want to use IntelliSense.) – To have no need to create code as an extra step is a key benefit of dynamic coding. ● But if you use interop types w/o PIAs, the DLR still makes your life easier: – No-PIA deployed types provide the type dynamic instead of object in their APIs. – E.g. IDispatch and VARIANTs are exposed as dynamics, reducing cast operations. ● With dynamic typing and COM you now have a "natural" coding experience: – "Plain old CLR objects" (POCOs) and COM objects can be programmed like peers. – During debugging the new "Dynamic View" assists you watching COM objects. ● COM as "peer objects" is one advantage of VB and PowerShell programming.
  • 4. 4 Example with COM Automation <DynamicComInteropCSharp> aThis example shows the application of C# dynamic coding to allow simple COM automation.
  • 5. 5 Example: Transportation of Anonymous Types' Instances ● Passing or returning anonymous types' instances from or to methods is limited. – Feasible solutions like generic methods or using the type Object are quite limiting... – You could only call Object's methods within such a method, not the anonymous type's ones (well, you might use reflection...). ● With dynamic you can overcome these limits. ● You'll get following features: – Easy to use adapters. – But data-binding, e.g. from IList<dynamic> to your controls is still fully functional (Windows Forms and WPF). ● !!You can't access properties of anonymous types in non-friend assemblies!! – Anonymous types are internal and accessing them publicly may fail. – => Keep using them in the same assembly! ● This is just one selected example of how dynamic typing simplifies coding problems. Passing anonymous types' instances to methods accepting dynamic arguments is used with view models in ASP.Net MVC. ● Basically dynamic makes it possible to use otherwise only locally known anonymous types in other methods. ● Databinding in WPF is less "sensitive", because it also accepts different types having equally named properties. The default behavior in Windows Forms requires identical types. ● Situation in non-friend assemblies: On accessing the anonymous type's instance's properties (via a dynamically typed symbol) you'll get a RuntimeBinderException on the caller side, because the properties couldn't be bound. ● More: ● The type dynamic enables us to solve a problem on programming of generics: we can call arbitrary methods on unconstrained types' instances! Esp. we can call operators in generics with dynamic dispatching! ● .Net remoting: Also can we use dynamic dispatch to access the interface of proxy objects on marshaled instances w/o having the object's type in avail.
  • 6. 6 Leering at JavaScript: Dynamic Objects <Safari Web Inspector as REPL> Shows how we can exploit dynamic objects with dynamic properties in JavaScript. Key aspects of dynamic programming: (1. Dynamic Typing) (2. Late Binding) (3. Duck Typing) 4. Dynamic Objects ● The ability to modify objects during run time is another aspect of dynamic programming that is shown here. -> Dynamic objects.
  • 7. 7 Dynamic Objects with .Net ExpandoObjects ● Dynamic programming allows creating types, whose interfaces change at run time. – These interface changes are based on how you use instances of such dynamic types. ● Dynamic objects are objects that have their own late binding logic! – (In contrast to static objects that get bound by the compiler (early binding).) – We can create own dynamic APIs to put mighty data driven APIs into effect! – The easiest way to get this is a dynamic property bag, via the type ExpandoObject. – ExpandoObjects are similar to JavaScript's dynamic objects that work like dictionaries. – In many dynamic languages (e.g. JavaScript, Python) all objects are property bags. ● How to make use of .Net's ExpandoObject: – Import the namespace System.Dynamic. – Create a new ExpandoObject and assign it to a dynamic to enable dynamic dispatch. – Add/modify members during run time and exploit ExpandoObjects as dictionaries. ● In static typing types are immutable, if you don’t have the source code. - In dynamic typing this is no longer the case, because the dynamic type information of an object is part of the object's value! ● You should also check, whether a member of a dynamic object is really present, before accessing it (RuntimeBinderException). - This is a duck-typing aspect (Does "it" lay eggs?)! ● As a matter of fact in .Net there also exists the type IExpando, which is implemented by JScript .Net objects (and by dynamic objects in some situations). ● ExpandoObjects can also have functions as properties of a Delegate type! ● Interoperability when hosting a DLR scripting language: Expose your C# objects to IronRuby as ExpandoObjects! ● The class ExpandoObject is sealed. The discussion of more flexible dynamic objects is discussed in the next lecture.
  • 8. 8 ExpandoObjects in Action <DynamicObjects> Shows the basic usage of ExpandoObjects. Key aspects of dynamic programming: (1. Dynamic Typing) (2. Late Binding) (3. Duck Typing) 4. Dynamic Objects