What’s new in Visual C# 4.0 ? – Part 3 - Dynamic ExpendoObject

This is the third post of what’s new in Visual Studio C# 4.0.

At the former posts we covered optional parameters, Named Parameters at this post we will cover C# Dynamics and ExpandoObject

dynamic & ExpendoObject

C# 1.0 introduced us to the managed world (based on Microsoft perception)

C# 2.0 brought us Genetic types.

C# 3.0 introduced us to new concept – LINQ

C# 4.0 highlight is all about Dynamic Types

Say for example that you have the need to create an object on the spot and use it in a local scope, without dynamic type you had to define a class and then create the object. you could do it at runtime using reflection emit, code dom etc…now its much simpler !

All you have to do it to create an object of the type dynamic and using a special builder called ExpandoObject you can now define your object on the fly and use it.

  1. class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             dynamic person = new ExpandoObject();
  6.             person.Firstname="ohad";
  7.             person.Lastname = "israeli";
  8.             Console.WriteLine(person.Firstname);
  9.             Console.WriteLine(person.Lastname);
  10.             Console.ReadLine();
  11.         }
  12.     }

You may say… wow this is cool.. well wait and see some more cool stuff..

Now lets add to this example and say that you would like to add a functionality to this object, what about adding person.Fullname in order to join the first and last name of the person and return it back.

  1. class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             dynamic person = new ExpandoObject();
  6.             person.Firstname="ohad";
  7.             person.Lastname = "israeli";
  8.             person.Fullname = new Func<string>(delegate() { return person.Firstname + " " + person.Lastname; });
  9.             Console.WriteLine(person.Firstname);
  10.             Console.WriteLine(person.Lastname);
  11.             Console.WriteLine(person.Fullname());
  12.             Console.ReadLine();
  13.         }
  14.     }

As you can see we can point the new object properties to lambda expressions such as Func<string> and define on the spot a function that will return the full name of the person.

Note that on line 12 you need to call the function and not use it as property – person.Fullname()

What will happen if you forget the () by the end of the function name ? (note the change on line 12)

  1. class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             dynamic person = new ExpandoObject();
  6.             person.Firstname="ohad";
  7.             person.Lastname = "israeli";
  8.             person.Fullname = new Func<string>(delegate() { return person.Firstname + " " + person.Lastname; });
  9.             Console.WriteLine(person.Firstname);
  10.             Console.WriteLine(person.Lastname);
  11.             Console.WriteLine(person.Fullname);
  12.             Console.ReadLine();
  13.         }
  14.     }

 

 

The result will be the lambda expression itself instead of the result of the lamba expression:

ohad
israeli
System.Func`1[System.String]

You can also add methods and not just functions using the Action expression: (note line 9)

  1. class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             dynamic person = new ExpandoObject();
  6.             person.Firstname="ohad";
  7.             person.Lastname = "israeli";
  8.             person.Fullname = new Func<string>(delegate() { return person.Firstname + " " + person.Lastname; });
  9.             person.CallOhad = new Action(() => { Console.WriteLine("Hi Ohad are you there ?"); });
  10.             Console.WriteLine(person.Firstname);
  11.             Console.WriteLine(person.Lastname);
  12.             Console.WriteLine(person.Fullname());
  13.             
  14.             person.CallOhad();
  15.             Console.ReadLine();
  16.         }
  17.     }

In conclusion dynamic types are cool but and there is a big but !

They are hard to debug and some of their functionality is only being tested at runtime this is why it is very important that whenever you use dynamic types test, test, and do some more testing using unit test of your code.

If you follow the following code you will notice that each of the calls to the properties in lines 11,12,13 begins with a small letter instead of uppercase.  This code will compile without any errors but of course the code will fail at runtime as the properties / function names are all uppercase.

  1. class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             dynamic person = new ExpandoObject();
  6.             person.Firstname="ohad";
  7.             person.Lastname = "israeli";
  8.             person.Fullname = new Func<string>(delegate() { return person.Firstname + " " + person.Lastname; });
  9.             person.CallOhad = new Action(() => { Console.WriteLine("Hi Ohad are you there ?"); });
  10.             Console.WriteLine(person.firstname);
  11.             Console.WriteLine(person.lastname);
  12.             Console.WriteLine(person.fullname());
  13.             
  14.             person.CallOhad();
  15.             Console.ReadLine();
  16.         }
  17.     }

2 Comments

Comments have been disabled for this content.