The same type is defined in two assemblies.

I have a VSTO-addin that uses CustomTaskPanes. My code compiles and works fine, but the problem arises from code analyzers such as Resharper and Code contract for .net.

This code causes error messages from both analyzers:

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] '

This is strange because the open type Microsoft.Office.Core.MsoCTPDockPosition exists only in office.dll. Anyway, I tried to resolve this 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 that didn't help at all. What is the reason for the message? How can I solve it (especially for code contracts)?

In addition, I found another strange artifact - Resharper autocomplete shows MsoCTPDockPosition as it exists in Microsoft.Office.Tools.Common.dll, but then I try to autocomplete, it uses the office.dll version: enter image description here

+4
source share
2 answers

, . , Office.dll

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

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

Resharper . Code Contracts - ​​ . , dynamic

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

dynamic. , .

- , Resharper , , - .

Edit:

((dynamic) taskPane).DockPosition TargetExceptions , . ,

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

Microsoft Office 15.0 COM VS office.dll

+2

Source: https://habr.com/ru/post/1626617/


All Articles