简体   繁体   English

在其他项目中使用命名空间

[英]Using Namespaces in Other Projects

As the screenshot demonstrates, I have a project SampleApp that uses the namespaces EDAM and Thrift . 截图显示,我有一个项目SampleApp ,它使用命名空间EDAMThrift I want to replicate the functionalities of SampleApp to another project. 我想将SampleApp的功能复制到另一个项目中。

Do I have to include the two projects ( EDAM and Thrift ) in my other project? 我是否必须在我的其他项目中包含这两个项目( EDAMThrift )? Can I just copy the folders instead of including the actual project files? 我可以只复制文件夹而不是包含实际的项目文件吗? Can I just convert them to some sort of DLL or something? 我可以将它们转换为某种DLL或其他东西吗?

在此输入图像描述

You definitely should add references to the EDAM and Thrift projects from the SampleApp project. 您肯定应该从SampleApp项目添加对EDAM和Thrift项目的引用。

To do so, follow these steps: 为此,请按照下列步骤操作:

  1. Right click the 'References' folder in the SampleApp project. 右键单击SampleApp项目中的“References”文件夹。

  2. Select 'Add Reference...' 选择“添加参考...”

  3. On the popup, go to the 'Solution' tab. 在弹出窗口中,转到“解决方案”选项卡。

  4. Select the EDAM and Thrift projects. 选择EDAM和Thrift项目。

Under no circumstances should you just copy the files. 在任何情况下都不应该只复制文件。

Don't copy the folders, just add references to the projects. 不要复制文件夹,只需添加对项目的引用。 Even if the projects are class libraries you don't want to copy the DLLs, instead you should add references, just in case your DLLs are updated: 即使项目是类库,您也不想复制DLL,而是应该添加引用,以防万一您的DLL更新:

How to: Add or Remove References in Visual Studio 如何:在Visual Studio中添加或删除引用

If the "other" project is in the same Solution, you should be able to reference EDAM, Thrift and even SampleApp from that project much in the same way you set the references up for SampleApp. 如果“其他”项目在同一个解决方案中,您应该可以从该项目中引用EDAM,Thrift甚至SampleApp,就像为SampleApp设置引用一样。

If this "other" project will be in a new solution...I'd have to do a little research and testing. 如果这个“其他”项目将采用新的解决方案......我将不得不做一些研究和测试。

Visual Studio project references are equivalent to referencing an assembly directly, but it has a great advantage: when you build a project, Visual Studio take cares of building its dependent projects too. Visual Studio项目引用等同于直接引用程序集,但它具有很大的优势:当您构建项目时,Visual Studio也会负责构建其依赖项目。

In addition, Visual Studio will prevent circular references. 此外,Visual Studio将阻止循环引用。

There're many other pros, but it's a good summary. 还有很多其他职业选手,但这是一个很好的总结。

Copy-pasting the code files isn't importing a namespace: this is duplicating code! 复制粘贴代码文件不会导入命名空间:这是复制代码! And referencing the assemblies directly is a waste of time and features! 直接引用组件是浪费时间和功能!

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

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