Avoiding Reflection: Adding the InteropServices library to the WP7 SDK

It’s a bit of a hassle to have to reflect the code as and when you want to use it in your code, so adding it to the files with the Windows Phone 7 SDK allows you to reference it just like any other included library.

You must have admin rights (as it requires changing files in your Program Files folder) and a copy of the Microsoft.Phone.InteropServices.dll, easily obtained by extracting them from an Emulator BIN. It will originally be named something like GAC_Microsoft.Phone.InteropServices_v7_0_0_0_cneutral_1.dll. You should rename it as I described above, before copying it to “C:\Program Files\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0\Profile\WindowsPhone”, adjusting C: to the drive Program Files is located on, and adjusting “Program Files” to “Program Files (x86)” if you are using 64-bit Windows.

Then, navigate to the RedistList, where there is a “FrameworkList.xml”. This lists the files in that particular framework, and it is how Visual Studio populates the list of DLLs available to be referenced. You’ll need to open it up in a text editor, running as administrator or you won’t be able to save the modifications. Find the line:
<File AssemblyName="Microsoft.Phone.Interop" Version="" PublicKeyToken="24eec0d8c86cda1e" Culture="neutral" ProcessorArchitecture="MSIL" InGac="false" />

Duplicate that line underneath itself, and change “Microsoft.Phone.Interop” to “Microsoft.Phone.InteropServices” and remove the “PublicKeyToken” attribute. For some reason, the GAC DLL fails Strong Name verification if you leave the PublicKeyToken there, but it doesn’t matter for our purposes anyway. You should then end up with a line like:
<File AssemblyName="Microsoft.Phone.InteropServices" Version="" Culture="neutral" ProcessorArchitecture="MSIL" InGac="false" />

Now, save the file, open Visual Studio, and open your project. As you go to add a reference, you’ll now notice “Microsoft.Phone.InteropServices” is one of the standard assemblies available for you to add, and adding it allows you to use the functionality it provides without having to reflect the library.