当可以作为静态方法访问时,扩展方法会出现编译错误

Cur*_*ous 1 .net c# extension-methods asp.net-core .net-6.0

我声明了一个扩展方法如下

namespace ExtensionMethods
{
    public static class MyExtensions
    {
        const string Nil = "$$$NIL$$$";
        public static bool IsEmptyNullOrNil(this string str)
        {
            if (string.IsNullOrEmpty(str) || str == Nil)
            {
                return true;
            }
            return false;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的控制器类中,我包含了命名空间。但程序无法编译。但我可以将该函数用作普通的静态方法。

在此输入图像描述

任何帮助将不胜感激。

Pet*_*ala 5

您的扩展方法可以通过以下方式访问

if (!employeeFilter.Name.IsEmptyNullOrNil())
Run Code Online (Sandbox Code Playgroud)

或者

if (!MyExtensions.IsEmptyNullOrNil(employeeFilter.Name))
Run Code Online (Sandbox Code Playgroud)