I had the cunning idea of using a dynamic variable to test the results of a method that returns an anonymous type - more specifically it returns a JsonResult, which as json looks like this
{ "newData" : [ 1120741.2697475906,
826527.64681837813
],
"oldData" : [ 1849870.2326665826,
1763440.5884212805
],
"timeSteps" : [ 0,
4.8828124999999998e-10
],
"total" : 2
}
I can read the JSonResult which will give me the anonymous type. Here's my code:
var jsonResult = controller.GetChangeData(1) as JsonResult;
dynamic data = jsonResult.Data;
Assert.AreEqual(2, data.total); // This works fine :)
But how do I get at "newData" for example? This code....
var newData = data.newData;
Gives me a System.Linq.Enumerable.WhereSelectArrayIterator, but I don't know what to do with it to be able to just use it as an arry of doubles.
I tried casting it as a double[], but it doesn't work either.
As an aside, can I easily check if a property is defined on the dynamic?
data.newData.ToArray()?