The strange case of “Set property ‘System.Windows.ResourceDictionary.DeferrableContent’ threw an exception.”

In the recent days, each time that I clicked the New Project button of the Visual Studio 2012 IDE, I got this exception:

“Set property ‘System.Windows.ResourceDictionary.DeferrableContent’ threw an exception.”

I have been clueless about this problem until today. When a problem happens in Visual Studio, the recommended approach is to launch it in “Safe mode”, because maybe an extension (add-in, package, etc.) is causing it. As a previous step, what I did today is to take a look at the Add-In Manager and I noticed that I had an add-in (a test runner that I created to perform integration tests of my MZ-Tools add-in) marked to load on startup. I unmarked it and then the problem disappeared. Why was this add-in causing this problem?

After some isolation, it happened that this add-in was setting an event handler for the AppDomain.AssemblyResolve event (to locate required assemblies) and a silenced NullReferenceException was happening in the event handler. The following minimal add-in reproduces the issue:

public class Connect : IDTExtensibility2
{
   private DTE2 _applicationObject;
   private AddIn _addInInstance;
   private AppDomain _appDomain;

   public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
   {
      _applicationObject = (DTE2)application;
      _addInInstance = (AddIn)addInInst;

      switch (connectMode)
      {
         case ext_ConnectMode.ext_cm_Startup:
            // OnStartupComplete will be called
            break;

         case ext_ConnectMode.ext_cm_AfterStartup:
            InitializeAddIn();
            break;
       }
   }

   public void OnStartupComplete(ref Array custom)
   {
      InitializeAddIn();
   }

   private void InitializeAddIn()
   {
      _appDomain = AppDomain.CurrentDomain;
      _appDomain.AssemblyResolve += AppDomain_AssemblyResolve;
   }

   public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
   {
      _appDomain.AssemblyResolve -= AppDomain_AssemblyResolve;
   }

   public void OnAddInsUpdate(ref Array custom)
   {
   }

   public void OnBeginShutdown(ref Array custom)
   {
   }

   private Assembly AppDomain_AssemblyResolve(object sender, ResolveEventArgs args)
   {
      Assembly objAssembly = null;
      AssemblyName objAssemblyName = null;

      // Force a NullReferenceException
      if (objAssemblyName.Name == "")
      {
      }

      return objAssembly;
   }
}