Dynamics and Object Runtime Composition With C# 4.0
description
Transcript of Dynamics and Object Runtime Composition With C# 4.0
![Page 1: Dynamics and Object Runtime Composition With C# 4.0](https://reader034.fdocuments.net/reader034/viewer/2022051820/5527abb3550346cd358b489c/html5/thumbnails/1.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051820/5527abb3550346cd358b489c/html5/thumbnails/2.jpg)
?use C#Do you
![Page 3: Dynamics and Object Runtime Composition With C# 4.0](https://reader034.fdocuments.net/reader034/viewer/2022051820/5527abb3550346cd358b489c/html5/thumbnails/3.jpg)
?dynamicstatic
Is C#...
![Page 4: Dynamics and Object Runtime Composition With C# 4.0](https://reader034.fdocuments.net/reader034/viewer/2022051820/5527abb3550346cd358b489c/html5/thumbnails/4.jpg)
static dynamicC# 4.0
![Page 5: Dynamics and Object Runtime Composition With C# 4.0](https://reader034.fdocuments.net/reader034/viewer/2022051820/5527abb3550346cd358b489c/html5/thumbnails/5.jpg)
Dynamic Language RuntimeMicrosoft
.NET
![Page 6: Dynamics and Object Runtime Composition With C# 4.0](https://reader034.fdocuments.net/reader034/viewer/2022051820/5527abb3550346cd358b489c/html5/thumbnails/6.jpg)
Why dynamic?
![Page 7: Dynamics and Object Runtime Composition With C# 4.0](https://reader034.fdocuments.net/reader034/viewer/2022051820/5527abb3550346cd358b489c/html5/thumbnails/7.jpg)
Tell me what to do……not how to do it
Expressiveness
![Page 8: Dynamics and Object Runtime Composition With C# 4.0](https://reader034.fdocuments.net/reader034/viewer/2022051820/5527abb3550346cd358b489c/html5/thumbnails/8.jpg)
If it quacks, it’s a duck!
Duck Typing
![Page 9: Dynamics and Object Runtime Composition With C# 4.0](https://reader034.fdocuments.net/reader034/viewer/2022051820/5527abb3550346cd358b489c/html5/thumbnails/9.jpg)
Object types can be modified on
runtime
Object Runtime Modification
![Page 10: Dynamics and Object Runtime Composition With C# 4.0](https://reader034.fdocuments.net/reader034/viewer/2022051820/5527abb3550346cd358b489c/html5/thumbnails/10.jpg)
dynamic myObj = "blah";
Dynamic at compile time
System.String at run time
![Page 11: Dynamics and Object Runtime Composition With C# 4.0](https://reader034.fdocuments.net/reader034/viewer/2022051820/5527abb3550346cd358b489c/html5/thumbnails/11.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051820/5527abb3550346cd358b489c/html5/thumbnails/12.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051820/5527abb3550346cd358b489c/html5/thumbnails/13.jpg)
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>