C#扩展方法未定义

Pau*_*ski 1 c# extension-methods .net-4.0

我有一个非常基本的扩展方法:

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我试图在同一个项目中使用它(单独的.cs文件),在同一个命名空间中,我收到一个'string' does not contain a definition for 'IsNullEmptyOrWhiteSpace'错误.

namespace PHPImport
{
    class AClassName: AnInterface
    {
        private void SomeMethod()
        {
             if (string.IsNullEmptyOrWhiteSpace(aStringObject)) { ... }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试重建/清理解决方案,并重新启动visual studio无济于事.

有任何想法吗?

Ree*_*sey 5

由于您将此作为扩展方法,因此需要将其称为:

if (aStringObject.IsNullEmptyOrWhiteSpace())
Run Code Online (Sandbox Code Playgroud)

它将用法"扩展"到字符串实例上,它不会向String类添加新的静态方法,这将由您当前的调用语法建议.