简体   繁体   English

无法在 Visual Studio 2015 中添加项目引用

[英]Unable to add project reference in Visual Studio 2015

I previously asked a similar question regarding Visual Studio 2013 but now I'm running into the same problem with Visual Studio 2015 but it seems to work differently, which makes the previous answer not applicable to this question.我之前问过一个关于 Visual Studio 2013 的类似问题,但现在我在 Visual Studio 2015 中遇到了同样的问题,但它的工作方式似乎不同,这使得之前的答案不适用于这个问题。 This is my previous question: Visual Studio 2013 - How to debug a project within a solution这是我之前的问题: Visual Studio 2013 - 如何在解决方案中调试项目

I'm attempting to add a project reference.我正在尝试添加项目参考。 I have a library I'm working on in a separate solution/project and a console application I want to use to test and debug the library.我有一个正在单独解决方案/项目中处理的库和一个控制台应用程序,我想用它来测试和调试该库。

I tried: Add Reference > Projects > Solution > Browse我试过:添加参考>项目>解决方案>浏览

But the open file dialog does not contain .sln files in the filter list and if I force it to display them by typing *.sln and then select the Solution I get:但是打开文件对话框在过滤器列表中不包含 .sln 文件,如果我通过键入 *.sln 强制它显示它们,然后选择我得到的解决方案:

"Please make sure the file is accessible and that it is a valid assembly or COM component." “请确保该文件可访问,并且它是有效的程序集或 COM 组件。”

What am I doing wrong?我究竟做错了什么? How do I properly add a Solution/Project reference?如何正确添加解决方案/项目参考?

在此处输入图片说明

For me issue was due to difference in Target framework.对我来说,问题是由于 Target 框架的差异。 One was .NET Framework 4.5 and other was .NET Framework 4.5.2一个是 .NET Framework 4.5,另一个是 .NET Framework 4.5.2

Verify if the Target framework is same for your project and the project reference that you are trying add reference.验证您的项目和您尝试添加引用的项目引用的目标框架是否相同。

(Right click on project -> properties -> Application Tab(Target frame work)) (右键单击项目 -> 属性 -> 应用程序选项卡(目标框架))

Right click on your solution file in Solution Explorer window, click Add and select Existing Project...在解决方案资源管理器窗口中右键单击您的解决方案文件,单击添加并选择现有项目...

在此处输入图片说明

Once the project is a part of the solution, you will be able to add project reference.一旦项目成为解决方案的一部分,您就可以添加项目引用。

In case anyone else runs into this here is something that I found.万一其他人遇到这个问题,这是我发现的。 I had almost the exact same issue in VS 2015. I created a console app and then a class library project but could not add a reference to the class library.我在 VS 2015 中遇到了几乎完全相同的问题。我创建了一个控制台应用程序,然后创建了一个类库项目,但无法添加对类库的引用。

It turns out I didn't create a class library project as I had thought, but rather a class library (package) project.事实证明,我并没有像我想象的那样创建类库项目,而是创建了一个类库(包)项目。 These are different things and I was unaware of what the (package) part meant when I selected it.这些是不同的东西,当我选择它时,我不知道(包)部分意味着什么。 This is for creating Nuget packages, apparently.显然,这是用于创建 Nuget 包。

I'm not sure if this is something new to VS 2015 but I had not run across it before and it caused me some grief.我不确定这是否是 VS 2015 的新内容,但我之前没有遇到过它,这让我有些悲伤。 The error message ('A reference to ClassLibrary1 could not be added') was totally worthless in trying to debug it.错误消息(“无法添加对 ClassLibrary1 的引用”)在尝试调试时完全没有价值。

You should add project to your solution first;您应该先将项目添加到您的解决方案中; after adding project to your solution, you can reference it.将项目添加到您的解决方案后,您可以参考它。

To add an existing project to your solution:要将现有项目添加到您的解决方案:

您可以从 <code>File > Add > Existing Project</code> 菜单中添加它...

Add reference menu:添加参考菜单:

从解决方案资源管理器中展开项目,右键单击 <code>References</code> 文件夹,然后选择 <code>Add reference...</code>

Add references dialog:添加引用对话框:

从左侧菜单展开<code>Projects</code>,然后选择<code>Projects</code>。

为了添加项目引用,项目必须是解决方案的一部分。

For me issue was due to a difference in the types of projects.对我来说,问题是由于项目类型的不同。 One was a C# Windows forms project and the other project was a C# Universal Windows project.一个是 C# Windows 窗体项目,另一个项目是 C# 通用 Windows 项目。

The Windows Forms project couldn't reference the Universal Windows project. Windows 窗体项目无法引用通用 Windows 项目。 The error was "Unable to add a reference to project "XYZ".错误是“无法添加对项目“XYZ”的引用。

It appears you can have a library shared between both types of projects by folloing this answer: What kind of class library works with both Universal and Winform projects?通过遵循以下答案,您似乎可以在两种类型的项目之间共享一个库:哪种类库适用于 Universal 和 Winform 项目?

I ended up having a different solution.我最终有了不同的解决方案。 I had three projects in the solution which referenced a NuGet package which was no longer available.我在解决方案中有三个项目引用了一个不再可用的 NuGet 包。 I had the library locally but I was unable to add it.我在本地有图书馆,但我无法添加它。 I found that because of the BUILD ORDER and DEPENDENCY of my projects, I had to add it to the project that was built first, then I could add it to the projects that were dependent on that project.我发现由于我的项目的 BUILD ORDER 和 DEPENDENCY,我必须将它添加到首先构建的项目中,然后我才能将它添加到依赖于该项目的项目中。

I had this problem today and I looked at the configuration manager for the solution.我今天遇到了这个问题,我查看了配置管理器的解决方案。 The projects had different platform settings.这些项目有不同的平台设置。 I made them both Any CPU and rebuilt.我让它们都是 Any CPU 并重建。 Then I could reference the project.然后我可以参考该项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM