dynamics and object runtime composition with c# 4.0

Post on 10-Apr-2015

816 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

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>

jonlimjap@gmail.comhttp://twitter.com/LaTtEX

http://dotnet.kapenilattex.com

top related