Jon Limjap Microsoft Most Valuable Professional for Visual C# Senior Application Developer, FBM e-Services Dynamics & Object Runtime Composition with C# 4.0
May 25, 2015
Jon LimjapMicrosoft Most Valuable Professional for Visual C#
Senior Application Developer, FBM e-Services
Dynamics & Object Runtime Composition with C# 4.0
?use C#Do you
?dynamicstatic
Is C#...
static dynamicC# 4.0
Dynamic Language RuntimeMicrosoft
.NET
Why dynamic?
Tell me what to do……not how to do it
Expressiveness
If it quacks, it’s a duck!
Duck Typing
Object types can be modified on
runtime
Object Runtime Modification
dynamic myObj = "blah";
Dynamic at compile time
System.String at run time
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
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
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>
[email protected]://twitter.com/LaTtEX
http://dotnet.kapenilattex.com