Tip #832: Dynamic does not work in sandbox

Dynamic not DynamicS. The reference is to dynamic keyword making C# developer’s life easier since 2010. As Rickard “Ikea Frysta Köttbullar” Norström has discovered, the following code works in a sandbox on-premises but spits the dummy in Dynamics 365 Online environment. Not all sandboxes were created equal, it seems.

01
02
03
04
05
06
07
08
09
10
11
string outstring = "get it from an external service";
if (!string.IsNullOrEmpty(outstring))
{
   dynamic outp = JArray.Parse(outstring);
   if (outp.Count == 1)
   {
      var objekt = outp[0];
      if (objekt["ProjectTypeID"] != "ART")
      ...
   }
}

Avoiding dynamic and using explicit cast (instead of relying on var seems to be the key:

01
02
03
04
05
06
07
08
09
10
11
12
JArray outp = JArray.Parse(outstring);
string outstring = "get it from an external service";
if (!string.IsNullOrEmpty(outstring))
{
   JArray outp = JArray.Parse(outstring);
   if (outp.Count == 1)
   {
      JToken objekt = outp[0];
      if (objekt["ProjectTypeID"] != "ART")
      ...
   }
}

Tipp Jarr’s $0.02 + GST

If object coming down the wire is well known in advance, entire Newtonsoft assembly can be avoided by using DataContractJsonSerializer instead.

One thought on “Tip #832: Dynamic does not work in sandbox

  1. […] got a bit of help by George Doubinski  and the issue was that the code I was using implicit typed variables. The reason for this is […]

Leave a Reply

Your email address will not be published. Required fields are marked *