CA1017 VS2010 StyleCop的ComVisible相关错误

时间:2011-05-24 15:18:07

标签: c# .net visual-studio-2010 stylecop comvisible

我在StyleCop上有CA1017错误消息,说我需要将其设为ComVisible false。

Error   18  CA1017 : Microsoft.Design : 
Because 'NationalInstruments.Labview.FPGA.ModelsimCommunicator.dll' exposes externally 
visible types, mark it with ComVisible(false) at the assembly level and then mark all 
types within the assembly that should be exposed to COM clients with ComVisible(true).

然后,我将代码[assembly: ComVisible(false)]放在最顶层的命名空间之前。但是,我仍然得到与其他错误消息相同的错误。

Error   19  The type or namespace name 'ComVisible' could not be found (are you 
missing a using directive or an assembly reference?)    


Error   20  The type or namespace name 'ComVisibleAttribute' could not be found (are
you missing a using directive or an assembly reference?)    

似乎VS2010也无法识别此名称。

enter image description here

这有什么问题?

1 个答案:

答案 0 :(得分:3)

ComVisibleAttribute中定义了System.Runtime.InteropServices namespace

所以你需要:

  1. 使用其命名空间对属性的名称进行全面限定:

    [assembly: System.Runtime.InteropServices.ComVisible(false)]
    
  2. 在源文件的顶部添加using指令以导入该文件的命名空间:

    using System.Runtime.InteropServices;
    
  3. 将来,您应该能够让Visual Studio警告您这些事情。当您看到表示编译器错误的波浪线时,请查找附近的下拉按钮或按 Ctrl + 应显示一个菜单,指示问题的可能解决方案。在这种情况下,建议您选择上面列出的选项1或2,只需单击一下,就可以为您执行所有必要的操作。

    (以上惊人的动画图像是从here中删除的。)