【问题标题】:Resharper - Go To Implementation listing reference twiceResharper - Go To Implementation 列表参考两次
【发布时间】:2013-06-18 01:55:10
【问题描述】:

在我的一个解决方案中,当我右键单击一个符号并为在其他解决方案项目之一中定义的对象选择“转到实现”时,它会列出两次引用并强制我选择一个。

根据图标,列表中的一项似乎代表项目,另一项代表一个 dll。我点击哪个无关紧要 - 它会转到同一个源文件。

我在这个特定项目中只有一次库引用 - 它正在引用该项目。

什么会导致这种情况发生?也许是某种循环引用问题?

【问题讨论】:

  • 这很难从你给出的描述中诊断出来。您能否在youtrack.jetbrains.net 详细描述您的情况 - 谢谢!
  • 我也有同样的问题。您是否使用了 EntityFramework 或任何可能导致 DLL 被某些 Visual Studio 工具加载的技术?
  • 是的,首先使用 EF 代码。
  • 我偶尔会遇到同样的问题,我也在使用 EF Code First ......我以前在以前不使用 EF 的项目中没有见过这个问题......
  • 项目之间的文件是否链接(即添加为链接)?

标签: c# resharper


【解决方案1】:

据我所知,如果您有一个包含多个项目的解决方案,其中某个项目被引用为项目,并且解决方案中的其他两个项目也作为纯文件引用,这也可能发生。

如果 ReSharper 出现问题,我可以提供的另一个建议是 clear the cache

【讨论】:

  • 如何通过查看现有引用来判断它是作为项目引用还是作为文件引用? Resharper 可以为我添加一个参考,所以它可能做错了。
  • @MattFrear:你需要看看 csproj 文件。
  • 我清除了 %LOCALAPPDATA%\JetBrains\ReSharper 中的旧 R# 版本,然后清除了当前 8.2 版本中的特定解决方案缓存,重启 VS 后问题就消失了。
【解决方案2】:

我遇到了这个问题,我刚刚解决了它。

首先,尝试做一个干净的解决方案,然后是一个构建。

在我的情况下,我的解决方案中的一个流氓项目是使用比其他项目更旧版本的 .NET 框架编译的,因此当 Resharper 为我添加对我的其他项目的引用时,它必须将其添加为 dll参考而不是作为项目参考。

我的解决方法是

  1. 将旧项目升级到与其他项目相同的 .NET 框架版本
  2. 从旧项目中删除对其他项目的引用
  3. 再次添加对其他项目的引用(这次作为项目引用)
  4. 清洁解决方案
  5. 构建解决方案

完成。

【讨论】:

    【解决方案3】:

    我发现了几个导致此问题的不同案例,并且非常恼火,因此我编写了一个小控制台应用程序来扫描我的解决方案并为我找到问题。这是为任何可能觉得这很有用的人准备的。要运行它,将路径传递给您的解决方案文件夹,它将在控制台上打印出问题。它非常“快速而肮脏”,但它为我找到了问题。

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

    【讨论】:

    • 该代码有效。但对我来说更简单、更快捷的是在我的所有 *.csproj 中简单搜索 TargetFrameworkVersion 并快速查看所有出现的情况。
    猜你喜欢
    • 2014-09-14
    • 2022-12-11
    • 2023-01-18
    • 2012-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-07
    相关资源
    最近更新 更多