简体   繁体   English

如何将函数从外部命名空间导入到我的项目中 c sharp

[英]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.

相关问题 如何将旧代码从不同的命名空间导入到我当前的项目中? - How to Import Old code from a Different namespace into My Current Project? 如何将 function 从 c++.dll 导入 Z4A8A08F09D37B73795649038408B 锋利 - How to import function from c++ .dll into c sharp 如何使用外部项目的名称空间? - how to use namespace of external project? .NET编程-我可以从我的VB.NET项目访问C-Sharp项目的成员吗? - .NET Programming - Can I access members of a C-Sharp project from my VB.NET Project? 如何从我的方法外部访问c sharp中的对象? - How to access an object in c sharp from outside my method? 调用系统命名空间中的函数时,ac Sharp编译器如何工作? - how does a c sharp compiler work when a function from the system namespace is called? 如何在 Xamarin 项目中导入我的 ViewModel 的命名空间? (无法解析符号 XAMLUI) - How do I import the namespace of my ViewModel in a Xamarin project ? (Cannot resolve symbol XAMLUI) openapi-generator 如何在我的解决方案的 MS 测试项目中生成 c-sharp .NET core 3.1 兼容测试? - openapi-generator how to generate a c-sharp .NET core 3.1 compatible test in my solution's MS Test project? 如何将外部.exe添加到我的C#项目中? - How to add External .exe to my C# project? 您如何从Visual Studio中的其他项目/命名空间/解决方案访问数据/功能? - How do you access data/functions from a different project/namespace/solution in Visual Studio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM