Wednesday, July 5, 2017

C# 4.0 - dynamic ... my new best friend

Okay, I can see the opportunity for all sorts of abuse if used incorrectly.

However, I found the perfect use for C# dynamic keyword ... dealing with JSON.

Often times, I need to send back a JSON Result with a subset of my object graph, or a flattened object graph.  I only want to send back the fields that the front end needs (trying to be judicious with bandwidth).


  dynamic result = new Object();
  result.AgendaOrigFileName = agenda.AgendaOrigFileName;
  result.AgendaId = agenda.AgendaId;
  return Json(result);

No comments:

Post a Comment