Wednesday 1 October 2014

Nicely Dynamic

The dynamic keyword scares me. I understand why it exists (interaction with COM and scripting languages like IronPython) but I know what developers are like. Dynamic used everywhere for no apparent reason other than to try it, resulting in code that is hard to read and maintain. OK, probably not that bad, but I think it's fair to say that under normal circumstances a developer should not use dynamic. Unless...

I think I found a justifiable use of dynamic. Consider a Dictionary<Type, IConverter<T>> where Type and T should be the same. But how would you manage the dictionary. Dynamic to the rescue.


I really like this approach. The use of dynamic is nicely abstracted within your class so nobody ever needs to know. And because you're managing the getting and setting you can be very confident that the dynamic value extracted from the Dictionary is always what you expect.