visual-studio-2013 - VS 2013 Ultimate 中的 codelens 引用不正确

标签 visual-studio-2013 reference-counting codelens

我不确定这是否是设计使然,还是需要启用/禁用 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 及其对应的行号:

enter image description here

它应该在弹出窗口中仅显示一个引用(1 个引用)和类 5。另外我不确定为什么 codelens 还包含实际实现 count 中的方法的类。我现在看不到谁在调用谁。当您有很多类需要处理时,引用计数会很有帮助。

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

最佳答案

过去,当此功能正在开发时,我已经看到了很多关于此主题的讨论。我不得不在我的邮件文件中挖掘一下,看看原因是什么。官方的解释是这样的:

What’s described below is definitely working as designed, though it might seem a bug from a different perspective. In essence, Find All References cascades through related symbols (such as virtual methods, overrides, interface implementations, etc.) for a couple of reasons:

  1. Find All References is tied intrinsically to Rename Symbol. So, this is the set of symbols that you would need to be included in a rename symbol operation if you wished to not break your program.
  2. Without performing whole program analysis, visual studio won’t know exactly what methods truly are real references when the program executes. (And that’s impossible in reality anyway...) In addition, it doesn’t know exactly what you’re looking for (maybe you were looking for a derived method after all) and so it presents the whole set.

随着越来越多的人使用 IoC/DI 框架,运行时替换的可能性越来越大,我个人发现寻找可以在运行时替换其位置的其他方法非常有值(value),但是当它们将被单独分组,以清楚地显示哪些方法是直接引用的,哪些是可替代引用的。

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

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

关于visual-studio-2013 - VS 2013 Ultimate 中的 codelens 引用不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32169280/

相关文章:

c++ - 构造函数定义可以以 "class"关键字为前缀吗?

c++ - C++ 中有一些更智能的智能指针吗?

Swift Struct 的引用计数

visual-studio-2013 - CodeLens 只找到用 MSTests 编写的测试

c# - 数据库实体的 MVC 错误

c++ - Visual Studio 错误的自动完成功能

git - 使用 git 子模块时的 NuGet 自动包还原

c++ - 引用计数类和多线程

visual-studio-2017 - 如何为 Visual Studio 2017 社区启用 CodeLens

c# - 在 Visual Studio 中获取零引用代码列表