简体   繁体   English

将名称空间添加到Visual Studio项目

[英]Adding namespaces to Visual Studio project

I am creating a Visual Studio solution and am trying to add namespaces Kafka.Client, Kafka.Client.Producers.Parititioning, Kafka.Client.IntegrationTests, etc. to a program file I've created with a Main() method. 我正在创建Visual Studio解决方案,并尝试将名称空间Kafka.Client,Kafka.Client.Producers.Parititioning,Kafka.Client.IntegrationTests等添加到使用Main()方法创建的程序文件中。 I have Kafka.Client and Kafka.Client.IntegrationTests in the References of this program file as per Solution Explorer. 根据解决方案资源管理器,此程序文件的引用中有Kafka.Client和Kafka.Client.IntegrationTests The code is as follows: 代码如下:

using Kafka.Client;
using Kafka.Client.IntegrationTests;
using Kafka.Client.Producers.Partitioning;
using Kafka.Client.Utils;
using Kafka.Client.ZooKeeperIntegration;

namespace ConsoleApplication2 {
    class Program {
        static void Main(string[] args) {
        //code here
        }
    }
}

The problem is that when I try to "Rebuild solution" or "debug solution", the aforementioned "using" lines give the error "The type or namespace name 'Kafka' could not be found (are you missing a using directive or an assembly reference?)" I've spent quite a long time on this and would appreciate some pointers about what I would need to do. 问题是,当我尝试“重建解决方案”或“调试解决方案”时,上述“使用”行给出了错误“找不到类型或名称空间名称'Kafka'(您是否缺少using指令或程序集?参考?)”我已经花了很长时间了,希望能对我需要做的事情提供一些指导。

using doesn't actually "include" anything into your project. 使用实际上并没有在项目中“包含”任何内容。 It just makes it so you don't always have to type out the full namespace. 它就是这样做的,因此您不必总是输入完整的名称空间。 So the error is clearly in referencing the other project. 因此,错误显然是在引用其他项目时。

The Kafka project has to be built first. 必须先建立Kafka项目。 So first make sure you can successfully build the Kafka project. 因此,首先请确保您可以成功构建Kafka项目。

if the Kafka is not in the same project, make sure you've add the the reference to the dll, and make sure "copy local" is true 如果Kafka不在同一个项目中,请确保已将引用添加到dll,并确保“ copy local”为true

to add the dll as a reference, right-click ConsoleApplication2 in the solution explorer, click add reference, then browse to and locate the actual dll output by the kafka project. 要添加dll作为参考,请在解决方案资源管理器中右键单击ConsoleApplication2,单击“添加参考”,然后浏览并找到kafka项目输出的实际dll。

Sounds like these are separate class libraries or other projects. 听起来像是单独的类库或其他项目。 If that's the case, add a project reference from your main project and the using statements will then work. 在这种情况下,请从您的主项目中添加一个project reference ,然后using语句将起作用。

The reason you want to add them as a project reference, and not a dll reference is that you'll likely switch back and forth from debug/release mode and you might end up with an outdated reference. 要将它们添加为项目引用而不是dll引用的原因是,您可能会从调试/发布模式来回切换,并且最终可能会过时。

Thanks for trying to answer the questions. 感谢您尝试回答问题。 My problem was that the "Target application" was not the same for all the projects in my solution. 我的问题是解决方案中的所有项目的“目标应用程序”都不相同。 Right-clicking on the referenced projects and selecting "Properties" enabled me to change the target application. 右键单击引用的项目并选择“属性”,使我可以更改目标应用程序。 Then I needed to rebuild the .dll files to run the program properly. 然后,我需要重建.dll文件才能正常运行该程序。

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

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