Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resharper - Go To Implementation listing reference twice

Tags:

c#

resharper

In one of my solutions, when I right click a symbol and choose "Go To Implementation" for an object defined in one of the other solution projects, it lists the reference twice and forces me to choose one.

Based on the icons, it appears that one of the items in the list represents the project, and the other represents a dll. It doesn't matter which one I click - it goes to the same source file.

I only have the library reference once in this particular project - it is referencing the project.

What would cause this to happen? Some sort of circular reference issue perhaps?

like image 356
Shaun Rowan Avatar asked Jun 18 '13 01:06

Shaun Rowan


3 Answers

As far as I can tell, this can also happen if you have a solution with several projects, where a certain project is referenced as project and also as pure file by two other projects in the solution.

Another advice that I can give if something is broken with ReSharper, is to clear the cache.

like image 51
Matthias Avatar answered Oct 19 '22 10:10

Matthias


I had this problem and I just fixed it.

First, try do a Clean Solution and then a Build.

In my case, one rogue Project in my solution was compiled using an older version of the .NET framework than the other Projects, so when Resharper added a reference to my other Projects for me, it must have added it as a dll reference instead of as a Project reference.

My fix was

  1. Upgrade old Project to the same version of .NET framework as the other Projects
  2. Remove references to other Projects from that old Project
  3. Add references to the other Projects again (as Project references this time)
  4. Clean solution
  5. Build solution

Done.

like image 38
Matt Frear Avatar answered Oct 19 '22 09:10

Matt Frear


I've found a couple different cases that cause this problem, and got so annoyed that I wrote a little console app to scan my solution and find the problems for me. Here it is for anyone who might find this useful. To run it pass it the path to your solution folder and it will print out the issues on the console. It's very "quick and dirty" but it found the issues for me.

class Program
{
    static void Main(string[] args)
    {
        if (args != null && args.Any())
        {
            foreach (var s in args)
            {
                Console.WriteLine("Checking " + s);
                DirectoryInfo dir = new DirectoryInfo(s);
                var files = dir.GetFiles("*.csproj", SearchOption.AllDirectories);

                var projects = files.Select(x => new Project(x)).ToList();

                var grouped = projects.GroupBy(x => x.TargetFrameworkVersion);
                if(grouped.Count()>1)
                {
                    Console.WriteLine("Solution contains multiple versions of Target Frameworks, this may cause duplicate assemblies in R# cache");
                    foreach (var group in grouped)
                    {
                        Console.WriteLine(group.Key);
                        foreach (var project in group)
                        {
                            Console.WriteLine(project.AssemblyName);
                        }
                    }
                }

                //loop through for debugging
                foreach (var project in projects)
                {
                    foreach (var reference in project.References)
                    {
                        foreach (var checkProject in projects)
                        {
                            if (checkProject.AssemblyName == reference)
                            {
                                Console.WriteLine("Reference in" + project.FileName + " referencing " +
                                                  reference+" that should be a ProjectReference, this may cause duplicate entries in R# Cache");
                            }
                        }
                    }
                }
            }
            Console.WriteLine("Complete");
            Console.ReadLine();
        }
        else
        {
            Console.WriteLine("You must provide a path to scan for csproj files");
        }
    }


}

public class Project
{
    public string FileName { get; set; }
    public string AssemblyName { get; set; }
    public string ProjectGuid { get; set; }
    public string TargetFrameworkVersion { get; set; }
    public IList<string> References { get; set; }
    private FileInfo _file;
    private XmlDocument _document;
    private XmlNamespaceManager _namespaceManager;

    public Project(FileInfo file)
    {
        _file = file;

        FileName = _file.FullName;

        _document = new XmlDocument();
        _document.Load(_file.FullName);

        _namespaceManager = new XmlNamespaceManager(_document.NameTable);
        _namespaceManager.AddNamespace("msbld", "http://schemas.microsoft.com/developer/msbuild/2003");

        var projectGuidNode = _document.SelectSingleNode("//msbld:ProjectGuid", _namespaceManager);
        ProjectGuid = projectGuidNode.InnerText;

        var assemblyNameNode = _document.SelectSingleNode("//msbld:AssemblyName", _namespaceManager);
        AssemblyName = assemblyNameNode.InnerText;

        var targetFrameworkNode = _document.SelectSingleNode("//msbld:TargetFrameworkVersion", _namespaceManager);
        TargetFrameworkVersion = targetFrameworkNode.InnerText;

        References = new List<string>();
        var referenceNodes = _document.SelectNodes("//msbld:Reference", _namespaceManager);
        foreach (var node in referenceNodes)
        {
            var element = (XmlElement) node;

            //file references
            if (element.HasChildNodes)
            {
                foreach (var child in element.ChildNodes)
                {
                    var childElement = (XmlElement)child;
                    if (childElement.Name == "HintPath")
                    {
                        var value = childElement.InnerText;
                        value = value.Substring(value.LastIndexOf("\\") + 1);
                        value = value.Replace(".dll", "");
                        References.Add(value);
                    }
                }
            }
            //gac references
            else
            {
                foreach (var attr in element.Attributes)
                {
                    var attribute = (XmlAttribute)attr;
                    if (attribute.Name == "Include")
                    {
                        var value = attribute.Value;
                        string reference = value;
                        if (value.Contains(','))
                        {
                            reference = value.Substring(0, value.IndexOf(','));
                        }
                        References.Add(reference);
                        break;
                    }
                }
            }


        }

    }
}
like image 1
Brook Avatar answered Oct 19 '22 09:10

Brook