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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s