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

问题描述:

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

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?

在项目中添加对它的引用,并在 CS 文件顶部要使用它的位置添加 using 子句.

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.

添加引用:

  1. 在 Visual Studio 中,单击项目",然后单击添加引用".
  2. 单击浏览"选项卡并找到要添加引用的 DLL.
    注意:如果您要使用的 DLL 在同一个项目中,显然使用 Browse 是错误的形式.相反,右键单击项目,然后单击添加引用,然后从项目选项卡中选择适当的类:
  3. 点击确定.

添加 using 子句:

Adding a using clause:

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

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:

using MyLibrary;