Home
  Latest posts
  My Writings
  My Code
  My Gallery
  About me
 
  rssfeed Syndication
 
Bloggtoppen.se
 
 
Links
  Cornerstone
  SweNug
 
Post categories
  misc (48)
  Architecture (21)
  C# (19)
  Asp.Net (2)
  Vb.Net (2)
  Training (7)
  Data (19)
  Events (40)
  Platform (2)
  Orcas (4)
  Updates (3)
  Methods (10)
  Tools (6)
  Announcements (14)
  Languages (1)
  Patterns (6)
  Opinions (11)
  Fun (3)
  Ineta (1)
  Opinion (0)
  Practices (2)
  WCF (5)
 
 
 

Having Fun with Anonymous types and Extension Methods in C# 3.0

Sunday, November 11, 2007

For version 3.0 of C# Microsoft have added features that makes the language just a little bit dynamic but still strongly typed. One of this feature is collection initializers for lists and dictionaries like we initialized arrays on earlier versions:

List<int> list = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; Dictionary<string, string> dictionary = new Dictionary<string, string> { { "key1", "value1"}, { "key2", "value2"}, { "key3", "value3"}, };

I like this feature. But for Dictionaries where the key will be a string, wouldn't it be cool to do this instead:

Dictionary<string, string> dictionary = new Dictionary<string, string> { key1 = "value1", key2 = "value2", key3 = "value3" };

Fredrik (http://fredrik.nsquared2.com/) popped up on MSN with this idea earlier and I really liked it. After playing around with some C# 3.0 example code I came up with a almost as neat solution as the one proposed.

By utilizing extension methods, anonymous types and fluent interfaces this was the resulting usage:

Dictionary<string, string> dic = new Dictionary<string, string>() .Init(new { Key1 = "val1", Key2 = "val3" });

Arguable not as cool as the proposed syntax but still.

I've been playing around with implicit cast operator and some generics stuff and tried to make the syntax even more slick but haven't managed to do so just yet. Anyone got propositions for improvements? Here's my code:

public static class DictionaryExtensions { public static Dictionary<string, T> Init<T>(this Dictionary<string, T> dictionaryToInit, object initializationData) { if (dictionaryToInit == null) ThrowDictionaryNullException(); if (initializationData != null) { PropertyDescriptorCollection propertiesFromType = TypeDescriptor.GetProperties(initializationData); foreach (PropertyDescriptor propertyItem in propertiesFromType) { object value = propertyItem.GetValue(initializationData); if (IsOfType<T>(value)) { dictionaryToInit.Add(propertyItem.Name, (T)value); } else { ThrowExceptionForInvalidValueType(propertyItem); } } } return dictionaryToInit; } private static bool IsOfType<T>(object value) { return value is T; } private static void ThrowExceptionForInvalidValueType(PropertyDescriptor propertyItem) { throw new ArgumentException(string.Format("Value for key {0} is of invalid type for this dictionary", propertyItem.Name)); } private static void ThrowDictionaryNullException() { throw new ArgumentException("Can't initialize a null Dictionary"); } }
kick it on DotNetKicks.com
 

Comments


Comment
Title:
Your name:
Your url:
Text:
Please enter the text from the image: