【发布时间】:2020-04-20 05:23:27
【问题描述】:
当定义此类的文件位于另一个文件夹中时,如何将扩展方法(见下文)引入当前命名空间?
当StringHandler.cs 位于Product 文件夹中时,我可以将using Utilities; 包含在Product.cs 中,但是当它位于一个单独的文件夹中时,与Project 文件夹一起(在Utilities 文件夹中),我不知道如何包含它。有人可以解释一下在这种情况下如何使用 using 关键字吗?例如,using 真正指向我的文件系统上的哪个位置(我是否需要指定相对于我的 csproj 文件或我的 CallCenter.sln 文件的使用目录?)?
│ CallCenter.sln
│
├───src
│ ├───Project
│ │ │ Product.cs
| | | Project.csproj
│ │ ├───bin
│ │ │ └───...
│ │ │
│ │ └───obj
│ │ └───...
│ │
│ └───Utilities
│ StringHandler.cs
StringHandler.cs
namespace Utilities
{
public static class StringHandler
{
public static string InsertSpaces(this string source)
{
string result = string.Empty;
if (!string.IsNullOrWhiteSpace(source))
{
foreach (char letter in source)
{
if (char.IsUpper(letter))
{
result = result.Trim();
result += " ";
}
result += letter;
}
}
return result.Trim();
}
}
}
【问题讨论】:
-
您必须将cs添加到项目中。 using语句是库名,与文件所在路径无关。使用 VS 菜单将 cs 文件添加到项目中: 项目:添加现有项目:浏览 cs 文件。添加文件后,您可以将命名空间添加到其他模块。
标签: c# namespaces using