C# Dynamics in the Wild

56 %
44 %
Information about C# Dynamics in the Wild
Technology

Published on January 17, 2014

Author: TimBourguignon

Source: slideshare.net

Description

C# developers are torn between finding dynamics cool & useful and terrible & dangerous. In this talk I describe the basic use of dynamics and how it works before trying to show some usages of dynamics that can be both useful and even mighty.

This Slide deck (in English) used at the HerbstCampus Conference in Nürnberg in September 2013 and provides you with an overview of the basic elements of the dynamics as well as examples of their usages. The presentation material as well as the demo code can be found on github at https://github.com/Timothep/Talk.Dynamics

Dynamisch und Gefährlich? C# Dynamics in freier Wildbahn Tim Bourguignon Mathema Software GmbH v1.1

Dynamics? Noooooooo.... Herbstcampus 2013 – Dynamisch und Gefährlich? 3

Dynamics? Noooooooo.... Herbstcampus 2013 – Dynamisch und Gefährlich? 4

Dynamics? Noooooooo.... Herbstcampus 2013 – Dynamisch und Gefährlich? 5

Dynamics? Noooooooo.... Herbstcampus 2013 – Dynamisch und Gefährlich? 6

Compiler says *meep* string lang = "C#"; lang++; int theAnswer = 42; theAnswer.ToUpper(); Herbstcampus 2013 – Dynamisch und Gefährlich? 8

Compiler says *meep* string lang = "C#"; lang++; int theAnswer = 42; theAnswer.ToUpper(); Herbstcampus 2013 – Dynamisch und Gefährlich? 9

Dynamics to the rescue dynamic lang = "C#"; lang++; dynamic theAnswer = 42; theAnswer.ToUpper(); Herbstcampus 2013 – Dynamisch und Gefährlich? 10

Dynamics to the rescue dynamic lang = "C#"; lang++; dynamic theAnswer = 42; theAnswer.ToUpper(); Relax Man, he knows what he's doing Herbstcampus 2013 – Dynamisch und Gefährlich? 11

Dynamics to the rescue dynamic lang = "C#"; lang++; dynamic theAnswer = 42; theAnswer.ToUpper(); Relax Man, he knows what he's doing Herbstcampus 2013 – Dynamisch und Gefährlich? … or not! 12

What about 'object' or reflection? Calculator calc = new Calculator(); int sum = calc.Add(10, 20); Herbstcampus 2013 – Dynamisch und Gefährlich? 13

What about 'object' or reflection? Calculator calc = new Calculator(); int sum = calc.Add(10, 20); object calc = new Calculator(); int sum = calc.Add(10, 20); Herbstcampus 2013 – Dynamisch und Gefährlich? 14

What about 'object' or reflection? Calculator calc = new Calculator(); int sum = calc.Add(10, 20); object calc = new Calculator(); int sum = calc.Add(10, 20); Herbstcampus 2013 – Dynamisch und Gefährlich? 15

What about 'object' or reflection? Calculator calc = new Calculator(); int sum = calc.Add(10, 20); object calc = new Calculator(); int sum = calc.Add(10, 20); object reflectionCalc = new Calculator(); Type calcType = reflectionCalc.GetType(); object result = calcType.InvokeMember("Add", BindingFlags.InvokeMethod, null, Activator.CreateInstance(calcType), new object[] { 10, 20 }); int sum2 = Convert.ToInt32(result); Herbstcampus 2013 – Dynamisch und Gefährlich? 16

What about 'object' or reflection? Calculator calc = new Calculator(); int sum = calc.Add(10, 20); object calc = new Calculator(); int sum = calc.Add(10, 20); object reflectionCalc = new Calculator(); Type calcType = reflectionCalc.GetType(); object result = calcType.InvokeMember("Add", BindingFlags.InvokeMethod, null, Activator.CreateInstance(calcType), new object[] { 10, 20 }); int sum2 = Convert.ToInt32(result); Herbstcampus 2013 – Dynamisch und Gefährlich? 17

What about 'object' or reflection? Calculator calc = new Calculator(); int sum = calc.Add(10, 20); object calc = new Calculator(); int sum = calc.Add(10, 20); object reflectionCalc = new Calculator(); Type calcType = reflectionCalc.GetType(); object result = calcType.InvokeMember("Add", BindingFlags.InvokeMethod, null, Activator.CreateInstance(calcType), new object[] { 10, 20 }); int sum2 = Convert.ToInt32(result); Herbstcampus 2013 dynamic calc = new Calculator(); – Dynamisch und Gefährlich? int sum = calc.Add(10, 20); 18

Duck-Typing Herbstcampus 2013 – Dynamisch und Gefährlich? 19

Duck-Typing Herbstcampus 2013 – Dynamisch und Gefährlich? 20

Duck-Typing • When I see a bird that walks like a duck swims like a duck and quacks like a duck, I call that bird a duck James Whitcomb Riley Herbstcampus 2013 – Dynamisch und Gefährlich? 21

Duck-Typing • When I see a bird that walks like a duck swims like a duck and quacks like a duck, I call that bird a duck James Whitcomb Riley vs Be • Look like • Methods & Attributes vs Class Herbstcampus 2013 – Dynamisch und Gefährlich? 22

Duck-Typing • When I see a bird that walks like a duck swims like a duck and quacks like a duck, I call that bird a duck James Whitcomb Riley vs Be • Look like • Methods & Attributes vs Class Herbstcampus 2013 – Dynamisch und Gefährlich? 23

Dynamic languages: IronPython - IronRuby Herbstcampus 2013 – Dynamisch und Gefährlich? 24

Dynamic languages: IronPython - IronRuby #Python script.py def add(a, b): return a + b var pythonRuntime = Python.CreateRuntime(); dynamic pythonScript = pythonRuntime.UseFile("script.py"); var result = pythonScript.add(100, 200))); Herbstcampus 2013 – Dynamisch und Gefährlich? 25

Base objects & Tools Herbstcampus 2013 – Dynamisch und Gefährlich? 26

Base objects & Tools DynamicObject Herbstcampus 2013 – Dynamisch und Gefährlich? 27

Base objects & Tools DynamicObject Herbstcampus 2013 – Dynamisch und Gefährlich? ExpandoObject 28

Base objects & Tools DynamicObject ExpandoObject ElasticObject Herbstcampus 2013 – Dynamisch und Gefährlich? 29

Base objects & Tools DynamicObject ExpandoObject ElasticObject Gemini Herbstcampus 2013 – Dynamisch und Gefährlich? 30

Frameworks Herbstcampus 2013 – Dynamisch und Gefährlich? 31

Frameworks Massive Herbstcampus 2013 – Dynamisch und Gefährlich? 32

Frameworks Massive Nancy Herbstcampus 2013 – Dynamisch und Gefährlich? 33

Frameworks Massive Nancy Herbstcampus 2013 – Dynamisch und Gefährlich? Simple.Data 34

System.Dynamic.DynamicObject • Exposes members at run time instead of at compile time • Important methods • TrySetMember • TryGetMember • Is called when a member of a dynamic class is requested and no arguments are specified • TryInvokeMember • Is called when a member of a dynamic class is requested with arguments • Combining those functions in a smart way is the key Herbstcampus 2013 – Dynamisch und Gefährlich? 35

System.Dynamic.ExpandoObject • Represents an object whose members can be dynamically added and removed at run time • Demo • • • • Simple ExpandoObject Expando structure vs Xml structure ExpandoToXml Linq-to-Object http://blogs.msdn.com/b/csharpfaq/archive/2009/10/01/dynamic-in-c-4-0-introducing-the-expandoobject.aspx Herbstcampus 2013 – Dynamisch und Gefährlich? 36

ElasticObject • Multi level dynamic object implementation using .NET 4.0 dynamic features, for fluent access of data types like XML • Demo • Expando vs Elastic • Elastic-to-Xml https://github.com/amazedsaint/ElasticObject Herbstcampus 2013 – Dynamisch und Gefährlich? 37

Gemini • „Brings the capabilities of a dynamic type system to C#“ • Demo • • • • • Members on the fly Methods on the fly Object graph Responds to Introspection Herbstcampus 2013 – Dynamisch und Gefährlich? 38

NancyFx • Lightweight WebFramework • Demo • Parameters • Return object Herbstcampus 2013 – Dynamisch und Gefährlich? 39

Massive • Wrapper for DB tables that uses dynamics • Create a class that wraps a table • Query away • Demo • Usage • Definition of TryGetMember Herbstcampus 2013 – Dynamisch und Gefährlich? 40

Conclusion • Objects • Core Objects: DynamicObject, ExpandoObject • Variations: ElasticObject, Gemini • Usages: NancyFx, Massive, Simple.Data • DTOs • Architectural Flexibility • API Design • Think about using it! Herbstcampus 2013 – Dynamisch und Gefährlich? 41

Ich freue mich auf Eure Fragen! tim.bourguignon@mathema.de about.me/timbourguignon @timothep

#python presentations

Add a comment

Related presentations

Related pages

Rash thoughts about .NET, C#, F# and Dynamics NAV.

Rash thoughts about .NET, C#, F# and Dynamics NAV. Search: Main: Home; About me ... The sample was even smaller than the one that was reported from the wild.
Read more

Using wild character in Name Search in C#.net for CRM 4.0 ...

Using wild character in Name Search in C#.net for CRM 4.0 Windows Application. Dynamics > ... to use wild Character for Name Search in C#.net ...
Read more

X++ Keywords [AX 2012] - msdn.microsoft.com

X++ Keywords [AX 2012] ... Applies To: Microsoft Dynamics AX 2012 R3, Microsoft Dynamics AX 2012 R2, ... (such as with the C# keywords out or ...
Read more

Hottest 'dynamics-nav' Answers - Stack Overflow

c# sql-server excel dynamics-nav ... it is always better to make use of STRSUBSTNO function for wild cards ... How to manage MS Dynamics NAV C/AL Codes in ...
Read more

Dynamic query with Linq - CodeProject

Dynamic query with Linq. Pranay Rana, ... Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the ...
Read more

Efficient String Matching Algorithm with Use of Wildcard ...

C# Corner. Contribute. An Article; ... Big Data Dynamics CRM Microsoft Phone SharePoint ... Efficient String Matching Algorithm with Use of Wildcard ...
Read more

Example of using end-to-end dynamic in a C# Web API ...

Example of using end-to-end dynamic in a C# Web API project using Dapper for data access ... end dynamic in a C# Web API project using Dapper for data ...
Read more

c# - Dynamic WHERE clause in LINQ - Stack Overflow

Dynamic where clause in Linq to Entities. 4. LINQ is it possible to add where clauses ... C# Using Linq Query with where clause as variable on dataTable.
Read more

Enter Criteria in Filters - MSDN - Microsoft

Working with Microsoft Dynamics NAV Enter Criteria in Filters. Enter Criteria ... The SQL Server Option allows you to combine this symbol with a wild card ...
Read more