Search code examples
c#.netvstocode-analysiscustomtaskpane

The same type is defined in two assemblies


I have a VSTO-addin, which uses CustomTaskPanes. My code compiles and works fine, but problem comes from code analizers, like Resharper and Code contracts for .net.

This code provokes error messages from both analizers:

CustomTaskPane taskPane = CustomTaskPanes.Add(new UserControl(), "Title");
taskPane.DockPosition = MsoCTPDockPosition.msoCTPDockPositionFloating;

Cannot convert source type 'Microsoft.Office.Core.MsoCTPDockPosition [office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c]' to target type 'Microsoft.Office.Core.MsoCTPDockPosition [Microsoft.Office.Tools.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]'

That is weird, because public type Microsoft.Office.Core.MsoCTPDockPosition exists only in office.dll. Anyway, I tried to resolve it using aliases and named Microsoft.Office.Tools.Common as Tools_Common:

extern alias Tools_Common;
using System;
using System.Windows.Forms;
using Microsoft.Office.Core;
using Tools_Common::Microsoft.Office.Tools;
using CustomTaskPane = Tools_Common::Microsoft.Office.Tools.CustomTaskPane;

But it didn't help at all. What is the cause of the message? How can I solve it (especially for code contracts)?

Also, I found another strange artifact - Resharper's autocomplete shows MsoCTPDockPosition like it exists in Microsoft.Office.Tools.Common.dll, but then I try to do autocomplete, it uses office.dll version: enter image description here


Solution

  • So, I tried a few different ways and found solutions. I found that if I change the Office.dll assembly to another version from

    C:\Program Files (x86)\Microsoft Visual Studio 14.0\Visual Studio Tools for Office\PIA\Office15\Office.dll, 15.0.4420.1017

    to

    C:\Windows\assembly\GAC_MSIL\office, 15.0.4787.1001

    then Resharper works fine. But Code Contracts still give me the same error. So I fooled it using the dynamic word

    ((dynamic) taskPane).DockPosition = MsoCTPDockPosition.msoCTPDockPositionFloating;
    

    I'm not really happy about using dynamic. But Code Contracts are pretty important to me, so I can tolerate a little dirtiness for that.

    If someone can explain, why Resharper started to work well after I changed version or find better, cleaner solution for Code Contracts - I will reaccept the answer.

    Edit:

    ((dynamic) taskPane).DockPosition shows TargetExceptions and says that the property doesn't exist in the object. So I changed it to use reflection

    typeof(CustomTaskPane)
        .InvokeMember("DockPosition", BindingFlags.SetProperty, null, taskPane, new object[] { MsoCTPDockPosition.msoCTPDockPositionFloating }, null);