C# 项目参考在 VisualStudio2010 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9790104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:53:00  来源:igfitidea点击:

Project reference not working in VisualStudio2010

c#visual-studio-2010

提问by Richard

I've got a Solution with lots of projects and all but one of them is behaving. The one that is not working is a ConsoleApplication, and it relies on C# Class Library project. I've added a reference to the library project, and add the namespace (which I've checked is correct), but everywhere I reference the classes in my library, I get:

我有一个包含许多项目的解决方案,除了其中一个之外,其他所有项目都在运行。不起作用的是 ConsoleApplication,它依赖于 C# 类库项目。我添加了对库项目的引用,并添加了命名空间(我已经检查过它是正确的),但是在我引用库中的类的任何地方,我都得到:

The type or namespace 'MyClass' could not be found (are you missing a using directive or an assembly reference?).

找不到类型或命名空间“MyClass”(您是否缺少 using 指令或程序集引用?)。

The library project is building successfully (I can see the DLLs appear in the bin folder) and I've tried a project reference, and also a reference to the DLL itself. Neither works.

库项目正在成功构建(我可以看到 DLL 出现在 bin 文件夹中),我尝试了项目引用,以及对 DLL 本身的引用。两者都不起作用。

Also, all projects are set to build with a platform target of 'Any CPU'.

此外,所有项目都设置为使用“任何 CPU”的平台目标进行构建。

I've tried pretty much every suggestion I've come across on forums with no success. Can anyone shed some light on what's going wrong?

我已经尝试了我在论坛上遇到的几乎所有建议,但都没有成功。任何人都可以阐明发生了什么问题吗?

Thanks

谢谢

This solved the problem:

这解决了这个问题:

The console application had a Target framework of .NET Framework 4 Client Profile, whereas the library just had .NET Framework 4. I set the console app to .NET Framework 4and it all builds perfectly.

控制台应用程序有一个 Target 框架.NET Framework 4 Client Profile,而库只有.NET Framework 4. 我将控制台应用程序设置为.NET Framework 4,一切都完美无缺。

采纳答案by Cédric Rup

My bet is on a framework mismatch between your library and you app... Check if your library is not building with a superior version than you app, or if your app is building with a Client profile flavor

我打赌你的库和你的应用程序之间的框架不匹配......检查你的库是否没有比你的应用程序更好的版本,或者你的应用程序是否使用客户端配置文件风格构建

回答by Ben Robinson

It is probably that one of your DLLs references some part of the .net framework that is not referenced in your console application. For example if one of your class library projects has asp.net server controls in and references System.Web, but your console application does not reference System.Web it will not build and you will get that error. But it is not obvious because the DLLs referenced are stored in the GAC so they would never appear in your bin folder.

可能是您的 DLL 之一引用了控制台应用程序中未引用的 .net 框架的某些部分。例如,如果您的类库项目之一具有 asp.net 服务器控件并引用 System.Web,但您的控制台应用程序不引用 System.Web,则它不会生成,您将收到该错误。但这并不明显,因为引用的 DLL 存储在 GAC 中,因此它们永远不会出现在您的 bin 文件夹中。

回答by antao

Sounds weird,

听起来怪怪的,

Have you tried to remove the reference of the project and add it again? Check if your console app has got all the right references.

您是否尝试删除项目的引用并重新添加?检查您的控制台应用程序是否获得了所有正确的参考。

You could also inspect the .csproj file and see if everything is correct in there.

您还可以检查 .csproj 文件,看看那里的一切是否正确。

回答by DigitalFox

Just Check that you "Class Library"project has classes in it or if it is a data access layer project which include only a .edmx Model check the Model designer is found and it generates fine.

只需检查您的“类库”项目中是否包含类,或者它是否是仅包含 .edmx 模型的数据访问层项目,请检查模型设计器是否已找到并且生成正常。

Good Luck

祝你好运