我有一个非常基本的扩展方法:
namespace PHPImport
{
public static class StringExtensionMethods
{
public static bool IsNullEmptyOrWhiteSpace(this string theString)
{
string trimmed = theString.Trim();
if (trimmed == "\0")
return true;
if (theString != null)
{
foreach (char c in theString)
{
if (Char.IsWhiteSpace(c) == false)
return false;
}
}
return true;
}
}
}
我试图在同一个项目(单独的 .cs 文件)、同一个命名空间中使用它,我得到一个 'string' does not contain a definition for 'IsNullEmptyOrWhiteSpace'
错误。
namespace PHPImport
{
class AClassName: AnInterface
{
private void SomeMethod()
{
if (string.IsNullEmptyOrWhiteSpace(aStringObject)) { ... }
}
}
}
我已经尝试重建/清理解决方案,并重新启动 visual studio 无济于事。
有什么想法吗?
最佳答案
因为你把它作为一个扩展方法,你需要这样调用它:
if (aStringObject.IsNullEmptyOrWhiteSpace())
它将用法“扩展”到字符串 实例,它不会向 String
类添加新的静态方法,这将由您当前的调用语法建议。
关于C# 扩展方法未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18412117/