Using C#’s new dynamic type

When developing an application for Leankit’s Kanban cards to sync with Microsoft Team Foundation Server work items, I found a use for the dynamic keyword in a practical situation.  The object that is returned from the API’s GetCard call is different from the Card object that exists when you make a call to GetBoardAttributes.  The fields that I require in my case, however exist on both objects, so I was able to create a function that can accept either as an input and work without skipping a beat.  Observe:

This function can be called using either CardReplyData or Card as the dynamic value and it will work.  Just be careful that both objects have the fields you think they do, as these can’t be checked until runtime because of the nature of dynamic.

Advertisements