【问题标题】:Incorrect codelens references in VS 2013 ultimateVS 2013 Ultimate 中的 codelens 引用不正确
【发布时间】:2020-04-18 00:49:04
【问题描述】:

我不确定这是设计使然还是需要在 VS 2013 Ultimate 中启用/禁用功能,但 codelens 生成的引用计数完全不正常。它不是显示直接引用特定类/方法的类/方法的计数,而是显示整个解决方案中与该类/方法同名的所有内容的计数。

例如,假设我的解决方案中有四个类(四个项目各有一个类并不重要)。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary20
{

    public interface IWillPrint
    {
        void PrintThis();
    }


    public class Class1 : IWillPrint
    {
        public void PrintThis() { }
    }

    public class Class2 : IWillPrint
    {
        public void PrintThis() { }
    }

    public class Class3 : IWillPrint
    {
        public void PrintThis() { }
    }

    public class Class4 : IWillPrint
    {
        public void PrintThis() { }
    }

    public class Class5
    {

        public void SomeMethod()
        {
            var j = new Class1();
            j.PrintThis();
        }
    }
}

class1 中方法 PrintThis() 的引用计数显示为 5。引用弹出窗口显示 class1、class2、class3、class4 和 class5 及其对应的行号:

它应该在弹出窗口中只显示一个引用(1 个引用)和第 5 类。另外我不确定为什么codelens还包括实际实现计数中的方法的类。我现在看不到谁在呼唤谁。当你有很多类要处理时,引用计数是一个很大的帮助。

如果有更简单的解决方案,我不想重新安装 resharper 和/或 VS。

【问题讨论】:

  • 我有同样的“问题”:我也想只看到“真正的”引用,这意味着在派生类中我只想看到基类的方法,而不是所有方法派生自同一个基类的所有类。不幸的是,这不可用。

标签: visual-studio-2013 reference-counting codelens


【解决方案1】:

过去我看到过很多关于这个话题的讨论,而这个功能正在开发中。我不得不在我的邮件档案中挖掘一下,看看是什么原因。官方解释是这样的:

下面描述的内容肯定按设计工作,尽管从不同的角度来看它可能看起来是一个错误。本质上,Find All References 通过相关符号(如虚方法、覆盖、接口实现等)级联,原因如下:

  1. 查找所有引用本质上与重命名符号相关联。因此,如果您不希望破坏您的程序,则需要在重命名符号操作中包含这组符号。
  2. 如果不执行整个程序分析,Visual Studio 将无法确切知道程序执行时哪些方法真正是真正的参考。 (无论如何这在现实中是不可能的......)此外,它并不确切知道您在寻找什么(也许您毕竟在寻找派生方法),因此它会呈现整个集合。

随着越来越多的人使用 IoC/DI 框架,运行时替换的可能性越来越大,我个人认为找到其他可能在运行时替换的方法很有价值,但如果它们将被单独分组,以清楚地显示哪些方法是直接引用的,哪些是可替换的引用。

也许“引用”是错误的名称,但我想“相关符号”也会导致很多问题;)。

我知道没有办法在 CodeLens 中更改此行为,因此您可能仍需要安装 Resharper,Visual Studio User Voice 上可能已经有此项目,但我找不到。

【讨论】:

  • 种类。当我在 codelens 上工作时,有一段时间引用被分成 2 个数字,“直接”引用和“间接”引用。直接引用是您可以确定的引用,间接引用是接口实现方法和可能作为引用的虚拟方法。我们尝试让引用指示器显示 2 个数字,或者只是直接引用,并让弹出窗口显示 2 个不同的类别,这只会让在用户研究和测试期间尝试过它的每个人感到困惑。 (就个人而言,我喜欢它!)
  • @JohnGardner:我也希望看到这一点,因为拥有广泛使用的基类,在许多函数中读取“99 多个引用”是非常无用的。
【解决方案2】:

您发布的代码有缺陷.. 类声明无效。

当我采用相同的方法并将 class 关键字放在声明中时,我确实在 Class1 中显示了 1 个引用,在 Classes 2、3、4 中显示了 0 个引用

我使用 VS-2014 [Update 4] 和 VS-2015 [RTM] 来完成此操作.....代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
public class class1
{
    public void PrintThis()
    {
    }
}

public class class2
{
    public void PrintThis()
    {
    }
}

public class class3
{
    public void PrintThis()
    {
    }
}

public class class4
{
    public void PrintThis()
    {
    }
}

public class class5
{

    public void SomeMethod()
    {
        var j = new class1();
        j.PrintThis();
    }
}

internal class Program
{
    private static void Main(string[] args)
    {
    }
}
}

【讨论】:

  • 用@Terje 更新的样本更新了样本。问题显然存在,但需要稍微不同的样本来重现。
  • @david - 类声明中有什么无效的?谢谢
  • 呃!!不知道我怎么错过了“课”。
猜你喜欢
  • 1970-01-01
  • 2014-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多