C# Dynamics in the Wild

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
1 of 40

Recommended

Angular JS deep dive by
Angular JS deep diveAngular JS deep dive
Angular JS deep diveAxilis
1.5K views15 slides
Micro ORM vs Entity Framework by
Micro ORM vs Entity FrameworkMicro ORM vs Entity Framework
Micro ORM vs Entity FrameworkAxilis
7.2K views13 slides
Nancy & Simple.Data from ProgNet 11 by
Nancy & Simple.Data from ProgNet 11Nancy & Simple.Data from ProgNet 11
Nancy & Simple.Data from ProgNet 11Mark Rendle
1.3K views14 slides
Should you react? by
Should you react?Should you react?
Should you react?Axilis
273 views19 slides
NuGet Must Haves for LINQ by
NuGet Must Haves for LINQNuGet Must Haves for LINQ
NuGet Must Haves for LINQAxilis
609 views5 slides
Introduction to Simple.Data by
Introduction to Simple.DataIntroduction to Simple.Data
Introduction to Simple.DataTim Bourguignon
3.6K views48 slides

More Related Content

Featured

ChatGPT and the Future of Work - Clark Boyd by
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
26.2K views69 slides
Getting into the tech field. what next by
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
6.3K views22 slides
Google's Just Not That Into You: Understanding Core Updates & Search Intent by
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
6.7K views99 slides
How to have difficult conversations by
How to have difficult conversations How to have difficult conversations
How to have difficult conversations Rajiv Jayarajah, MAppComm, ACC
5.4K views19 slides
Introduction to Data Science by
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data ScienceChristy Abraham Joy
82.5K views51 slides
Time Management & Productivity - Best Practices by
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
169.8K views42 slides

Featured(20)

ChatGPT and the Future of Work - Clark Boyd by Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd26.2K views
Getting into the tech field. what next by Tessa Mero
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero6.3K views
Google's Just Not That Into You: Understanding Core Updates & Search Intent by Lily Ray
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray6.7K views
Time Management & Productivity - Best Practices by Vit Horky
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky169.8K views
The six step guide to practical project management by MindGenius
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
MindGenius36.7K views
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright... by RachelPearson36
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson3612.7K views
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present... by Applitools
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Applitools55.5K views
12 Ways to Increase Your Influence at Work by GetSmarter
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
GetSmarter401.7K views
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G... by DevGAMM Conference
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
DevGAMM Conference3.6K views
Barbie - Brand Strategy Presentation by Erica Santiago
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
Erica Santiago25.1K views
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well by Saba Software
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Saba Software25.3K views
Introduction to C Programming Language by Simplilearn
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
Simplilearn8.4K views
The Pixar Way: 37 Quotes on Developing and Maintaining a Creative Company (fr... by Palo Alto Software
The Pixar Way: 37 Quotes on Developing and Maintaining a Creative Company (fr...The Pixar Way: 37 Quotes on Developing and Maintaining a Creative Company (fr...
The Pixar Way: 37 Quotes on Developing and Maintaining a Creative Company (fr...
Palo Alto Software88.4K views
9 Tips for a Work-free Vacation by Weekdone.com
9 Tips for a Work-free Vacation9 Tips for a Work-free Vacation
9 Tips for a Work-free Vacation
Weekdone.com7.2K views
How to Map Your Future by SlideShop.com
How to Map Your FutureHow to Map Your Future
How to Map Your Future
SlideShop.com275.1K views

C# Dynamics in the Wild

  • 1. Dynamisch und Gefährlich? C# Dynamics in freier Wildbahn Tim Bourguignon Mathema Software GmbH v1.1
  • 2. Dynamics? Noooooooo.... Herbstcampus 2013 – Dynamisch und Gefährlich? 3
  • 3. Dynamics? Noooooooo.... Herbstcampus 2013 – Dynamisch und Gefährlich? 4
  • 4. Dynamics? Noooooooo.... Herbstcampus 2013 – Dynamisch und Gefährlich? 5
  • 5. Dynamics? Noooooooo.... Herbstcampus 2013 – Dynamisch und Gefährlich? 6
  • 6. Compiler says *meep* string lang = "C#"; lang++; int theAnswer = 42; theAnswer.ToUpper(); Herbstcampus 2013 – Dynamisch und Gefährlich? 8
  • 7. Compiler says *meep* string lang = "C#"; lang++; int theAnswer = 42; theAnswer.ToUpper(); Herbstcampus 2013 – Dynamisch und Gefährlich? 9
  • 8. Dynamics to the rescue dynamic lang = "C#"; lang++; dynamic theAnswer = 42; theAnswer.ToUpper(); Herbstcampus 2013 – Dynamisch und Gefährlich? 10
  • 9. 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
  • 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? … or not! 12
  • 11. What about 'object' or reflection? Calculator calc = new Calculator(); int sum = calc.Add(10, 20); Herbstcampus 2013 – Dynamisch und Gefährlich? 13
  • 12. 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
  • 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? 15
  • 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); 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
  • 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? 17
  • 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 dynamic calc = new Calculator(); – Dynamisch und Gefährlich? int sum = calc.Add(10, 20); 18
  • 17. Duck-Typing Herbstcampus 2013 – Dynamisch und Gefährlich? 19
  • 18. Duck-Typing Herbstcampus 2013 – Dynamisch und Gefährlich? 20
  • 19. 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
  • 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 vs Be • Look like • Methods & Attributes vs Class Herbstcampus 2013 – Dynamisch und Gefährlich? 22
  • 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? 23
  • 22. Dynamic languages: IronPython - IronRuby Herbstcampus 2013 – Dynamisch und Gefährlich? 24
  • 23. 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
  • 24. Base objects & Tools Herbstcampus 2013 – Dynamisch und Gefährlich? 26
  • 25. Base objects & Tools DynamicObject Herbstcampus 2013 – Dynamisch und Gefährlich? 27
  • 26. Base objects & Tools DynamicObject Herbstcampus 2013 – Dynamisch und Gefährlich? ExpandoObject 28
  • 27. Base objects & Tools DynamicObject ExpandoObject ElasticObject Herbstcampus 2013 – Dynamisch und Gefährlich? 29
  • 28. Base objects & Tools DynamicObject ExpandoObject ElasticObject Gemini Herbstcampus 2013 – Dynamisch und Gefährlich? 30
  • 29. Frameworks Herbstcampus 2013 – Dynamisch und Gefährlich? 31
  • 30. Frameworks Massive Herbstcampus 2013 – Dynamisch und Gefährlich? 32
  • 31. Frameworks Massive Nancy Herbstcampus 2013 – Dynamisch und Gefährlich? 33
  • 32. Frameworks Massive Nancy Herbstcampus 2013 – Dynamisch und Gefährlich? Simple.Data 34
  • 33. 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
  • 34. 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
  • 35. 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
  • 36. 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
  • 37. NancyFx • Lightweight WebFramework • Demo • Parameters • Return object Herbstcampus 2013 – Dynamisch und Gefährlich? 39
  • 38. 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
  • 39. 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
  • 40. Ich freue mich auf Eure Fragen! tim.bourguignon@mathema.de about.me/timbourguignon @timothep