The short story is that I don’t have enough scientific knowledge to know how to calculate magnetic declination, but enough to know that it can be done, using just a position on the surface of the earth and some information about the earth’s magnetic field. This is such an important topic that I was pretty sure there was open source code out there to do it, and I was right.
I found a pretty excellent implementation in C by Edward A Williams (right here) that is being maintained with the most recent magnetic field data, and ported it to C#. I present the port in the spirit of the GPL License under which the code was originally written. The nicest thing of all is that this code doesn’t depend on anything except system libraries, so you should be able to integrate it into almost anything.