dynamics and object runtime composition with c# 4.0

14
Jon Limjap Microsoft Most Valuable Professional for Visual C# Senior Application Developer, FBM e-Services Dynamics & Object Runtime Composition with C# 4.0

Upload: lattex

Post on 10-Apr-2015

816 views

Category:

Documents


1 download

DESCRIPTION

A presentation about the dynamic features of .NET Framework 4.0, C# 4.0, and the Dynamic Language Runtime

TRANSCRIPT

Page 1: Dynamics and Object Runtime Composition With C# 4.0

Jon LimjapMicrosoft Most Valuable Professional for Visual C#

Senior Application Developer, FBM e-Services

Dynamics & Object Runtime Composition with C# 4.0

Page 2: Dynamics and Object Runtime Composition With C# 4.0

?use C#Do you

Page 3: Dynamics and Object Runtime Composition With C# 4.0

?dynamicstatic

Is C#...

Page 4: Dynamics and Object Runtime Composition With C# 4.0

static dynamicC# 4.0

Page 5: Dynamics and Object Runtime Composition With C# 4.0

Dynamic Language RuntimeMicrosoft

.NET

Page 6: Dynamics and Object Runtime Composition With C# 4.0

Why dynamic?

Page 7: Dynamics and Object Runtime Composition With C# 4.0

Tell me what to do……not how to do it

Expressiveness

Page 8: Dynamics and Object Runtime Composition With C# 4.0

If it quacks, it’s a duck!

Duck Typing

Page 9: Dynamics and Object Runtime Composition With C# 4.0

Object types can be modified on

runtime

Object Runtime Modification

Page 10: Dynamics and Object Runtime Composition With C# 4.0

dynamic myObj = "blah";

Dynamic at compile time

System.String at run time

Page 11: Dynamics and Object Runtime Composition With C# 4.0

dynamic myObj = "blah";Console.WriteLine(myObj.Contains("lah"));Console.WriteLine(myObj.IndexOf("x"));Console.WriteLine(myObj.GetType());

myObj = new List<int>();Console.WriteLine(myObj.Count);Console.WriteLine(myObj.GetType());

But no compiler errors!

Change of type here

Method signature doesn’t match initial type

Page 12: Dynamics and Object Runtime Composition With C# 4.0

dynamic mrFantastic = new ExpandoObject();mrFantastic.Name = "Reed Richards";mrFantastic.BirthDate =  new DateTime(year: 1961, month: 12, day: 2);

Console.WriteLine(mrFantastic.Name);Console.WriteLine(mrFantastic.BirthDate);

Requires System.Dynamic

Properties can be added during runtime

Page 13: Dynamics and Object Runtime Composition With C# 4.0

mrFantastic.SayMyName = new Action( () => Console.WriteLine(mrFantastic.Name));

mrFantastic.GetAge = new Func<int>(() => Convert.ToInt32( (DateTime.Now - mrFantastic.BirthDate).Days  / 365.25));

mrFantastic.SayMyName();Console.WriteLine(mrFantastic.GetAge());

Methods could be added using Action<T> & Func<T>

Page 14: Dynamics and Object Runtime Composition With C# 4.0

[email protected]://twitter.com/LaTtEX

http://dotnet.kapenilattex.com