以下方法或属性错误之间的调用不明确 - c# visual studio 2019

The call is ambiguous between the following methods or properties error- c# visual studio 2019

我收到错误:以下方法或属性之间的调用不明确:'AutElemMethods.Selector(AutomationElement)' 和 'AutElemEx.Selector(AutomationElement)' 来自我正在使用的 类 中的两个。 我该如何解决这个问题? 代码:

public static class AutElemMethods
{       
   public static string Selector(this AutomationElement element)
    {
        AutomationElement parent = TreeWalker.ControlViewWalker.GetParent(element);
        if (parent == AutomationElement.RootElement)
        {
            return $"<{element.Name()}>";
        }
        if (string.IsNullOrWhiteSpace(element.Name()))
        {
            return parent.Selector() + "<>";
        }
        return $"{parent.Selector()}<{element.Name()}>";
    }
}

    public static class AutElemEx
{
    private static string Selector(this AutomationElement element)
    {
        string name = string.IsNullOrWhiteSpace(element.Name()) ? "" : $@"'Name':'{element.Name()}',";
        string type = string.IsNullOrWhiteSpace(element.Type()) ? "" : $@"'Type':'{element.Type()}',";
        return "{" + (name + type).Trim(',') + "}";
    }
}

扩展方法的范围在命名空间内,但不在包含它们的静态 class 内。因此,您的两个扩展方法具有相同的名称和参数,因此被编译器视为相同的签名。

你有几个选择。

  • 重命名其中一种方法。
  • 将它们放在不同的命名空间中 只在给定的代码文件中导入所需的命名空间。
  • 静态调用方法而不是像扩展方法那样使用它,例如:AutElemEx.Selector(myElement);
  • 如果它们必须在同一个命名空间中,您可以 statically import 仅将所需的扩展名 class 放入给定的代码文件中,例如using static MyNamespace.AutElemEx;