简体   繁体   English

不能在 C# 控制台应用程序中使用外部引用

[英]Cannot use external references with C# Console Application

I have tried all the suggestions below but still no joy.我已经尝试了下面的所有建议,但仍然没有快乐。

I'm now trying a console application, and let me explain exactly what I'm doing.我现在正在尝试一个控制台应用程序,让我解释一下我在做什么。

  1. I create a new console project in VS 2010我在 VS 2010 中创建了一个新的控制台项目
  2. I add a number of references (dll's) some that aren't mine such as Castle.Winsor and N2 CMS dlls我添加了一些引用(dll),其中一些不是我的,例如 Castle.Winsor 和 N2 CMS dll
  3. in the console app I can add using statements indicating I am using name spaces within the referenced DLLs在控制台应用程序中,我可以添加 using 语句,指示我在引用的 DLL 中使用名称空间
  4. I start writing code.我开始写代码。
  5. As soon as I compile all the code that uses the referenced DLLs immediately complains with "The type or namespace name '' could not be found (are you missing a using directive or an assembly reference?)"一旦我编译了所有使用引用的 DLL 的代码,就会立即抱怨“找不到类型或命名空间名称 ''(您是否缺少 using 指令或程序集引用?)”

I have tried setting the dlls to copy to local always, I have copied the DLL into the same directory, I have tried added reference by project and adding a reference to the DLL's themselves我曾尝试将 dll 设置为始终复制到本地,我已将 DLL 复制到同一目录中,我已尝试按项目添加引用并添加对 DLL 本身的引用

I don't get this problem with a web application project or a ASP.net project they always work fine, only something that is compiled to an EXE like a console app or windows service.我在 web 应用程序项目或 ASP.net 项目中没有遇到这个问题,它们总是可以正常工作,只有编译为 EXE 的东西,如控制台应用程序或 Windows 服务。

there must be something I'm missing or I would have got this working by now.一定是我遗漏了一些东西,否则我现在已经可以正常工作了。

Change the project Target to a non Client Profile target.将项目目标更改为非客户配置文件目标。 Right click the Project and select Properties, you should see a list of Framework versions.右键单击项目并选择属性,您应该会看到框架版本列表。 As you are using VS2010, the Console project you've created by default targets .NET Framework 4.0 Client Profile, change that to .NET Framework 4.0.当您使用 VS2010 时,您默认创建的 Console 项目面向 .NET Framework 4.0 Client Profile,将其更改为 .NET Framework 4.0。

检查引用程序集的Copy Local是否设置为true

First, as to your question, its hard to know exactly what you're doing wrong, but from the fact that you're using an underscore in an assembly name (and probably in namespaces and type names), it suggests you're rather new to the .NET world.首先,对于您的问题,很难确切地知道您做错了什么,但是从您在程序集名称(可能在命名空间和类型名称中)中使用下划线这一事实来看,这表明您宁愿.NET 世界的新手。

This suggests that you're adding references to other projects in your solution by browsing to the compiled assembly, rather than by adding a Project Reference .这表明您通过浏览到已编译的程序集而不是添加Project Reference来添加对解决方案中其他项目的引用 When adding a reference, you must select the Project tab rather than browsing for the assembly.添加引用时,您必须选择“项目”选项卡,而不是浏览程序集。

Even if you don't believe this is the issue, remove all references and re-add project references to make absolutely sure.即使您不相信这是问题所在,也请删除所有引用并重新添加项目引用以确保绝对确定。 Assumption, asses etc.假设,驴等。

Once you've done that, I'd strongly suggest remove all the underscores from your types, namespaces and assemblies.一旦你这样做了,我强烈建议从你的类型、命名空间和程序集中删除所有下划线。 You might want to go read the framework design guidelines, too.您可能还想阅读框架设计指南。

Open your .Proj(Windows service project file) file in notepad and check whether your assembly location(data_object) is the same which you are pointing.在记事本中打开您的 .Proj(Windows 服务项目文件)文件并检查您的程序集位置(data_object)是否与您指向的相同。

When you open .Proj file in notepad you can check for,当您在记事本中打开 .Proj 文件时,您可以检查,

Project reference,项目参考,

        <ProjectReference Include="C:\StackOverflow\StackOverflow.csproj">

And if you giving dll or exe refrence then如果你给 dll 或 exe 引用然后

 <Reference Include="StackOverflow, Version=1.0.0.0, Culture=neutral,       processorArchitecture=x86">
  <SpecificVersion>False</SpecificVersion>
  <ExecutableExtension>.exe</ExecutableExtension>
  <HintPath>C:\StackOverflow\bin\Debug\StackOverflow.exe</HintPath>
</Reference>

I suggest you to give exact location including drive name like above example.我建议您提供确切的位置,包括驱动器名称,如上例所示。

Other option you may try,您可以尝试其他选项,

 1. Clean and rebuild
 2. Add Project reference if you already tried dll reference
 3. Check whether the folder (referred assembly location) is Read Only then remove it. 

In VS 2019, I had two projects, one C++ and other one C# Console in one solution file.在 VS 2019 中,我在一个解决方案文件中有两个项目,一个 C++ 和另一个 C# 控制台。 When tried to add a dll reference through "Add reference", I was not able to see Browse button in "Reference Manager" window, to select dll file.当尝试通过“添加引用”添加 dll 引用时,我无法在“引用管理器”窗口中看到浏览按钮来选择 dll 文件。

But when I created only C# Console app inside solution then I could add dll reference.但是当我在解决方案中只创建 C# 控制台应用程序时,我可以添加 dll 引用。

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

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