Incorrect codelens references in VS 2013 ultimate

冷暖自知 提交于 2020-01-02 09:21:10

问题


I am not sure if this is by design or need to enable/disable features in VS 2013 ultimate but the reference counts generated by the codelens is completely out of whack. Instead of showing the count of classes/methods directly referencing a particular class/method, it shows the count of everything that has the same name as the class/method in the entire solution.

For example, say I have four classes in my solution (doesn't matter four projects with one class in each).

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();
        }
    }
}

The reference count for the method PrintThis() in class1 shows 5. The reference popup windows shows class1, class2, class3, class4 and class5 and their corresponding line numbers:

It should show only one reference (1 reference) and class 5 in the popup. Also I am not sure why the codelens also includes the class that actually implements the method in the count. I am now unable to see who is calling who. The reference counts are a big help when you have lots of classes to deal with.

I would hate to reinstall the resharper and/or VS if there is a much simpler solution.


回答1:


I've seen quite a bit of discussion on this topic in the past, while this feature was under development. I had to dig a bit in my mail archives to see what the reason was. The official explanation is this:

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.

With more-and-more people using IoC/DI frameworks, substitution at runtime is more and more likely and I personally find great value in finding other methods that might be substituted in its place at runtime, but it would be great when they would be grouped separately to clearly show which method is directly referenced and which ones are substitutable-references.

Maybe "references" is the wrong name, but I suppose that "Related Symbols" would result in a lot of questions as well ;).

I know of no way to change this behavior in CodeLens, so you may need to install Resharper anyway, there may already be an item for this on the Visual Studio User Voice, but I could not find it.




回答2:


Your posted code is flawed.. the class declarations are invalid.

When I took the same and put the class keyword on the declarations, I did get 1 reference shown in Class1, and 0 references shown in Classes 2,3,4

I did this with VS-2014 [Update 4] and VS-2015 [RTM].....code below:

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)
    {
    }
}
}


来源:https://stackoverflow.com/questions/32169280/incorrect-codelens-references-in-vs-2013-ultimate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!