Richard Howells' Blog

Start here with lots of good stuff from Tomas

As mentioned, the JSON serializer returns Dictionary<string, object>.  This is inconvenient in a strongly typed environment like C#.   The entries in the dictionary vary through being both ArrayLists and further Dictionaries.  In C# we’d normally need to keep casting them to the correct type before we access properties and methods.

Enter dynamic!  In C#, dynamic allows you to simply assume that methods and properties will be there at run time.  That truth is checked at run time so you may see exceptions if you are wrong.  It did allow me to retrieve a JSON result from Google's Geocoding service and extract data from it like so…

                var geoData = jsSerializer.Deserialize<Dictionary<string, dynamic>>(rv);

                if (geoData["status"] == "OK")

                    latitude = Convert.ToSingle(geoData["results"][0]["geometry"]["location"]["lat"]);

Not the prettiest code in the world, but short.

As Tomas points out in his article, you can get to the same answer, more strongly typed, by creating a series of classes to map the JSON.  But when you want a quick and short answer, this will do.


Read More »

I was trying to upgrade a VS2008 project to VS2012 – After upgrade I had the message ‘Application’ is ambiguous in the namespace ‘Microsoft.Office.Interop.Excel’

Back in the original VS2008 project there was a Warning message about unable to find an assembly.  It seemed to be related to Excel, but had not stopped the app working in years and had been ignored.  Deciding that the original complaint in VS2008 was probably the root cause of the ambiguity I set out to try and fix it.

Eventually I tracked it down.  The project had a COM reference to Excel and a .Net reference to the Excel Primary Interop Assembly.  I only discovered this by poking about in the vbproj file with a text editor.  I didn’t find a way of seeing, or removing, the COM reference through the VS UI.  Eventually I excised the entire COMReference node starting COMReference Include="Microsoft.Office.Interop.Excel" using the text editor.  Saved the project and all was well.  Back in VS2008 it built without the warning.  Converted to VS2012 it built without the ambiguity message.

Read More »

Copyright 2002-15 by Dynamisys Ltd