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

C# Dynamics in the Wild

Embed Size (px)

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

Citation preview

Page 1: C# Dynamics in the Wild

Dynamisch und Gefährlich?C# Dynamics in freier Wildbahn

Tim BourguignonMathema Software GmbH

v1.1

Page 2: C# Dynamics in the Wild

Herbstcampus 2013 – Dynamisch und Gefährlich? 3

Dynamics? Noooooooo....

Page 3: C# Dynamics in the Wild

Herbstcampus 2013 – Dynamisch und Gefährlich? 4

Dynamics? Noooooooo....

Page 4: C# Dynamics in the Wild

Herbstcampus 2013 – Dynamisch und Gefährlich? 5

Dynamics? Noooooooo....

Page 5: C# Dynamics in the Wild

Herbstcampus 2013 – Dynamisch und Gefährlich? 6

Dynamics? Noooooooo....

Page 6: C# Dynamics in the Wild

Herbstcampus 2013 – Dynamisch und Gefährlich? 8

Compiler says *meep*

string lang = "C#";lang++;

int theAnswer = 42;theAnswer.ToUpper();

Page 7: C# Dynamics in the Wild

Herbstcampus 2013 – Dynamisch und Gefährlich? 9

Compiler says *meep*

string lang = "C#";lang++;

int theAnswer = 42;theAnswer.ToUpper();

Page 8: C# Dynamics in the Wild

Herbstcampus 2013 – Dynamisch und Gefährlich? 10

Dynamics to the rescue

dynamic lang = "C#";lang++;

dynamic theAnswer = 42;theAnswer.ToUpper();

Page 9: C# Dynamics in the Wild

Herbstcampus 2013 – Dynamisch und Gefährlich? 11

Dynamics to the rescue

dynamic lang = "C#";lang++;

dynamic theAnswer = 42;theAnswer.ToUpper();

Relax Man,he knowswhat he's doing

Page 10: C# Dynamics in the Wild

Herbstcampus 2013 – Dynamisch und Gefährlich? 12

Dynamics to the rescue

dynamic lang = "C#";lang++;

dynamic theAnswer = 42;theAnswer.ToUpper();

Relax Man,he knowswhat he's doing … or not!

Page 11: C# Dynamics in the Wild

Herbstcampus 2013 – Dynamisch und Gefährlich? 13

What about 'object' or reflection?

Calculator calc = new Calculator();int sum = calc.Add(10, 20);

Page 12: C# Dynamics in the Wild

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);

Page 13: C# Dynamics in the Wild

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);

Page 14: C# Dynamics in the Wild

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);

Page 15: C# Dynamics in the Wild

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);

Page 16: C# Dynamics in the Wild

Herbstcampus 2013 – Dynamisch und Gefährlich? 18

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);

dynamic calc = new Calculator();int sum = calc.Add(10, 20);

Page 17: C# Dynamics in the Wild

Herbstcampus 2013 – Dynamisch und Gefährlich? 19

Duck-Typing

Page 18: C# Dynamics in the Wild

Herbstcampus 2013 – Dynamisch und Gefährlich? 20

Duck-Typing

Page 19: C# Dynamics in the Wild

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

Page 20: C# Dynamics in the Wild

Herbstcampus 2013 – Dynamisch und Gefährlich? 22

Duck-Typing

• Look like vs Be• Methods & Attributes vs Class

• 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

Page 21: C# Dynamics in the Wild

Herbstcampus 2013 – Dynamisch und Gefährlich? 23

Duck-Typing

• Look like vs Be• Methods & Attributes vs Class

• 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

Page 22: C# Dynamics in the Wild

Herbstcampus 2013 – Dynamisch und Gefährlich? 24

Dynamic languages: IronPython - IronRuby

Page 23: C# Dynamics in the Wild

Herbstcampus 2013 – Dynamisch und Gefährlich? 25

var pythonRuntime = Python.CreateRuntime();dynamic pythonScript =

pythonRuntime.UseFile("script.py");var result = pythonScript.add(100, 200)));

Dynamic languages: IronPython - IronRuby

#Python script.pydef add(a, b): return a + b

Page 24: C# Dynamics in the Wild

Herbstcampus 2013 – Dynamisch und Gefährlich? 26

Base objects & Tools

Page 25: C# Dynamics in the Wild

Herbstcampus 2013 – Dynamisch und Gefährlich? 27

Base objects & Tools

DynamicObject

Page 26: C# Dynamics in the Wild

Herbstcampus 2013 – Dynamisch und Gefährlich? 28

Base objects & Tools

DynamicObject ExpandoObject

Page 27: C# Dynamics in the Wild

Herbstcampus 2013 – Dynamisch und Gefährlich? 29

Base objects & Tools

DynamicObject

ElasticObject

ExpandoObject

Page 28: C# Dynamics in the Wild

Herbstcampus 2013 – Dynamisch und Gefährlich? 30

Base objects & Tools

DynamicObject

ElasticObject

Gemini

ExpandoObject

Page 29: C# Dynamics in the Wild

Herbstcampus 2013 – Dynamisch und Gefährlich? 31

Frameworks

Page 30: C# Dynamics in the Wild

Herbstcampus 2013 – Dynamisch und Gefährlich? 32

Frameworks

Massive

Page 31: C# Dynamics in the Wild

Herbstcampus 2013 – Dynamisch und Gefährlich? 33

Frameworks

Massive

Nancy

Page 32: C# Dynamics in the Wild

Herbstcampus 2013 – Dynamisch und Gefährlich? 34

Frameworks

Massive

Simple.DataNancy

Page 33: C# Dynamics in the Wild

Herbstcampus 2013 – Dynamisch und Gefährlich? 35

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

Page 34: C# Dynamics in the Wild

Herbstcampus 2013 – Dynamisch und Gefährlich? 36

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

Page 35: C# Dynamics in the Wild

Herbstcampus 2013 – Dynamisch und Gefährlich? 37

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

Page 36: C# Dynamics in the Wild

Herbstcampus 2013 – Dynamisch und Gefährlich? 38

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

Page 37: C# Dynamics in the Wild

Herbstcampus 2013 – Dynamisch und Gefährlich? 39

NancyFx

• Lightweight WebFramework• Demo

• Parameters

• Return object

Page 38: C# Dynamics in the Wild

Herbstcampus 2013 – Dynamisch und Gefährlich? 40

Massive

• Wrapper for DB tables that uses dynamics• Create a class that wraps a table• Query away• Demo

• Usage

• Definition of TryGetMember

Page 39: C# Dynamics in the Wild

Herbstcampus 2013 – Dynamisch und Gefährlich? 41

Conclusion

• Objects• Core Objects: DynamicObject, ExpandoObject

• Variations: ElasticObject, Gemini

• Usages: NancyFx, Massive, Simple.Data• DTOs• Architectural Flexibility• API Design

• Think about using it!

Page 40: C# Dynamics in the Wild

Ich freue mich auf Eure Fragen!

[email protected]/timbourguignon@timothep