[英]How to import functions from an external namespace onto my project in c sharp
I want to import functions from a different namespace in an external file into my c sharp project.我想将外部文件中不同命名空间的函数导入到我的 c sharp 项目中。 But what's the right method to do it.但是正确的方法是什么。 I copied the.cs file which contains the namespace into my project directory but "using " keyword showing error even the files are from the same directory.我将包含命名空间的 .cs 文件复制到我的项目目录中,但“使用”关键字显示错误,即使文件来自同一目录。 To be precise i'm trying to import api functions provided by a company for a hardware which are inside a different.cs file.准确地说,我正在尝试导入一家公司为硬件提供的 api 函数,这些函数位于不同的 .cs 文件中。 What's the right way to do it?正确的做法是什么?
Basically, you could create a new project with the same namespace as your imported.cs files have and add it as a project reference.基本上,您可以创建一个与您的 imports.cs 文件具有相同命名空间的新项目,并将其添加为项目引用。 Another method is to modify the namespace in the imported files to match the used namespace in your project.另一种方法是修改导入文件中的命名空间以匹配项目中使用的命名空间。
It really depends on the structure and logic you're importing, personally, I would go with option 1, and maybe even create some interfaces for them, to expose only the functionality I would need to use from them (Facade pattern).这实际上取决于您要导入的结构和逻辑,就个人而言,我会选择 go 和选项 1,甚至可能为它们创建一些接口,以仅公开我需要从中使用的功能(外观模式)。
Try to right click the project which you want to add.尝试右键单击要添加的项目。
Then click on "Add => Existing item...".然后单击“添加 => 现有项目...”。
Select the.cs file you want and click "Add button". Select 您想要的.cs文件,然后单击“添加按钮”。
Now you can using "the.cs file namespace" in your project.现在您可以在项目中使用“the.cs 文件命名空间”。
You can add it to your project reference.您可以将其添加到您的项目参考中。
Run the below in your terminal.在您的终端中运行以下命令。
dotnet add reference [library.csproj]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.