15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

304 ❘ ChaPTer 12 dynAmic lAnGuAGe extensiOns<br />

What about adding a method That can be done easily. You can use the same WroxDynamicObject <strong>and</strong> add<br />

a GetTomorrowDate method on it. It takes a DateTime object <strong>and</strong> returns a date string that is the next day.<br />

Here’s the code:<br />

dynamic wroxDyn = new WroxDynamicObject();<br />

Func GetTomorrow = today => today.AddDays(1).ToShortDateString();<br />

wroxDyn.GetTomorrowDate = GetTomorrow;<br />

Console.WriteLine("Tomorrow is {0}", wroxDyn.GetTomorrowDate(DateTime.Now));<br />

You create the delegate GetTomorrow using Func. The method the delegate represents is the<br />

call to AddDays. One day is added to the Date that is passed in, <strong>and</strong> a string of that date is returned. The<br />

delegate is then set to GetTomorrowDate on the wroxDyn object. The last line calls the new method, passing<br />

in today’s date.<br />

Once again the dynamic magic happens <strong>and</strong> you have an object with a valid method.<br />

exp<strong>and</strong>oobject<br />

Exp<strong>and</strong>oObject works similar to the WroxDynamicObject created in the previous section. The difference is<br />

that you don’t have to override any methods, as shown in the following code example:<br />

static void DoExp<strong>and</strong>o()<br />

{<br />

dynamic expObj = new Exp<strong>and</strong>oObject();<br />

expObj.FirstName = "Daffy";<br />

expObj.LastName = "Duck";<br />

Console.WriteLine(expObj.FirstName + " " + expObj.LastName);<br />

Func GetTomorrow = today => today.AddDays(1).ToShortDateString();<br />

expObj.GetTomorrowDate = GetTomorrow;<br />

Console.WriteLine("Tomorrow is {0}", expObj.GetTomorrowDate(DateTime.Now));<br />

}<br />

expObj.Friends = new List();<br />

expObj.Friends.Add(new Person() { FirstName = "Bob", LastName = "Jones" });<br />

expObj.Friends.Add(new Person() { FirstName = "Robert", LastName = "Jones" });<br />

expObj.Friends.Add(new Person() { FirstName = "Bobby", LastName = "Jones" });<br />

foreach (Person friend in expObj.Friends)<br />

{<br />

Console.WriteLine(friend.FirstName + " " + friend.LastName);<br />

}<br />

Notice that this code is almost identical to what you did earlier. You add a FirstName <strong>and</strong> LastName<br />

property, add a GetTomorrow function, <strong>and</strong> do one additional thing—add a collection of Person objects as<br />

a property of the object.<br />

At first glance, it may seem that this is no different than using the dynamic type. Well, there are a couple<br />

of subtle differences that are important. First, you can’t just create an empty dynamic typed object. The<br />

dynamic type has to have something assigned to it. For example, the following code won’t work:<br />

dynamic dynObj;<br />

dynObj.FirstName = "Joe";<br />

As seen in the previous example, this is possible with Exp<strong>and</strong>oObject.<br />

Second, because the dynamic type has to be assigned to, it will report back the type of what was assigned<br />

to it if you do a GetType call. So if you say assign an int, it will report back that it is an int. This won’t<br />

happen with Exp<strong>and</strong>oObject or an object derived from DynamicObject.<br />

If you have to control the addition <strong>and</strong> access of properties in your dynamic object, then deriving from<br />

DynamicObject is your best choice. With DynamicObject, you can use several methods to override <strong>and</strong><br />

control exactly how the object interacts with the runtime. For other cases, using the dynamic type or the<br />

Exp<strong>and</strong>oObject may be just what you need.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!