如何在项目中使用 C# 类库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1228335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 13:57:11  来源:igfitidea点击:

How do I use a C# Class Library in a project?

c#class

提问by Nosrama

I've created a new Class Library in C# and want to use it in one of my other C# projects - how do I do this?

我在 C# 中创建了一个新的类库,并想在我的其他 C# 项目之一中使用它 - 我该怎么做?

采纳答案by Michael Todd

Add a reference to it in your project and a using clause at the top of the CS file where you want to use it.

在您的项目中添加对它的引用,并在要使用它的 CS 文件顶部添加一个 using 子句。

Adding a reference:

添加引用:

  1. In Visual Studio, click Project, and then Add Reference.
  2. Click the Browse tab and locate the DLL you want to add a reference to.
    NOTE: Apparently using Browse is bad form if the DLL you want to use is in the same project. Instead, right-click the Project and then click Add Reference, then select the appropriate class from the Project tab:
    enter image description here
  3. Click OK.
  1. 在 Visual Studio 中,单击“项目”,然后单击“添加引用”。
  2. 单击浏览选项卡并找到要添加引用的 DLL。
    注意:如果您要使用的 DLL 位于同一个项目中,则显然使用 Browse 是错误的形式。相反,右键单击项目,然后单击添加引用,然后从项目选项卡中选择适当的类:
    在此处输入图片说明
  3. 单击确定。

Adding a using clause:

添加 using 子句:

Add "using [namespace];" to the CS file where you want to reference your library. So, if the library you want to reference has a namespace called MyLibrary, add the following to the CS file:

添加“使用[命名空间];” 到要引用库的 CS 文件。因此,如果您要引用的库具有名为 MyLibrary 的命名空间,请将以下内容添加到 CS 文件中:

using MyLibrary;

回答by Kent Boogaart

  1. Add a reference to your library
  2. Import the namespace
  3. Consume the types in your library
  1. 添加对您的库的引用
  2. 导入命名空间
  3. 使用库中的类型

回答by Daniel May

Add it as a reference.

添加它作为参考。

References > Add Reference > Browse for your DLL.

参考 > 添加参考 > 浏览您的 DLL。

You will then need to add a using statement to the top of your code.

然后,您需要在代码顶部添加 using 语句。

回答by Max Schmeling

In the Solution Explorer window, right click the project you want to use your class library from and click the 'Add Reference' menu item. Then if the class library is in the same solution file, go to the projects tab and select it; if it's not in the same tab, you can go to the Browse tab and find it that way.

在解决方案资源管理器窗口中,右键单击要从中使用类库的项目,然后单击“添加引用”菜单项。然后如果类库在同一个解决方案文件中,进入项目选项卡并选择它;如果它不在同一个选项卡中,您可以转到“浏览”选项卡并以这种方式找到它。

Then you can use anything in that assembly.

然后你可以在那个程序集中使用任何东西。

回答by Charlie

You need to add a reference to your class library from your project. Right click on the references folder and click add reference. You can either browse for the DLL or, if your class libaray is a project in your solution you can add a project reference.

您需要从项目中添加对类库的引用。右键单击引用文件夹,然后单击添加引用。您可以浏览 DLL,或者,如果您的类 libaray 是您的解决方案中的一个项目,您可以添加一个项目引用。

回答by Adam Robinson

I'm not certain why everyone is claiming that you need a usingstatement at the top of your file, as this is entirely unnecessary.

我不确定为什么每个人都声称您需要using在文件顶部添加声明,因为这完全没有必要。

Right-click on the "References" folder in your project and select "Add Reference". If your new class library is a project in the same solution, select the "Project" tab and pick the project. If the new library is NOT in the same solution, click the "Browse" tab and find the .dll for your new project.

右键单击项目中的“引用”文件夹,然后选择“添加引用”。如果您的新类库是同一解决方案中的项目,请选择“项目”选项卡并选择该项目。如果新库不在同一个解决方案中,请单击“浏览”选项卡并找到新项目的 .dll。

回答by SwDevMan81

Hereis a good article on creating and adding a class library. Even shows how to create Methods through the method wizard and how to use it in the application

是一篇关于创建和添加类库的好文章。甚至展示了如何通过方法向导创建方法以及如何在应用程序中使用它

回答by usefulBee

There are necessary steps that are missing in the above answers to work for all levels of devs:

上述答案中缺少适用于所有级别的开发人员的必要步骤:

  1. compile your class library project
  2. the dll file will be available in the bin folder
  3. in another project, right click ProjectName and select "Add" => "Existing Item"
  4. Browser to the bin folder of the class library project and select the dll file (3 & 4 steps are important if you plan to ship your app to other machines)
  5. as others mentioned, add reference to the dll file you "just" added to your project
  6. as @Adam mentioned, just call the library name from anywhere in your program, you do not need a using statement
  1. 编译你的类库项目
  2. dll 文件将在 bin 文件夹中可用
  3. 在另一个项目中,右键单击 ProjectName 并选择“添加”=>“现有项目”
  4. 浏览到类库项目的 bin 文件夹并选择 dll 文件(如果您打算将应用程序发送到其他机器,则 3 和 4 个步骤很重要
  5. 正如其他人提到的,添加对您“刚刚”添加到项目中的 dll 文件的引用
  6. 正如@Adam 提到的,只需从程序中的任何位置调用库名称,您就不需要 using 语句

回答by sohan yadav

Right Click on Project--> Add--> New Project-->click on Class Library.

右键单击项目--> 添加--> 新建项目--> 单击类库。

Now your class library is created asclass1.cs

现在您的类库创建为class1.cs

Right Click on References(of your program/console app)

右键单击引用(您的程序/控制台应用程序的)

-->Add Reference-->classLibrary1(whatever you named) Now mention "using ClassLibrary1" in your program/console app

-->添加引用-->classLibrary1(无论您命名什么)现在在您的程序/控制台应用程序中提及“使用ClassLibrary1”

Now u can easily call the method/property in your console app

现在您可以轻松调用控制台应用程序中的方法/属性