【问题标题】:Return a dynamic unknown type返回一个动态的未知类型
【发布时间】:2018-04-25 02:15:42
【问题描述】:

作为使用页面对象模式的一部分,我有一个方法应该在单击按钮时return 类。 问题是我在一个列表中有几个按钮,每个按钮返回一个不同的页面,因此返回一个不同的类。 我尝试使用以下方法,但出现错误。

  public dynamic ClickTheMenuButtonWorkplace<T>(string ElementID, T ClassToReturn)
    {
        WebDriverWait wait = new WebDriverWait(_webdriver, TimeSpan.FromSeconds(10));
        var MenuButton = wait.Until(x => x.FindElement(By.Id(ElementID)));
        MenuButton.Click();

        return ClassToReturn;
    }

T 实际上代表正在返回的类,根据单击的按钮。

使用这种方法的一个例子是:

Lakochot IDDetails = new SargelElyon(_webdriver).ClickTheMenuInabWorkplace("nav_conts", new Lakochot(_webdriver));
IDDetails.foo();

LoginPgae 目前只有一个构造函数来初始化webdriver,并且有一个foo 方法来打印一些东西。

我收到一个错误说: Missing compiler required member Microsoft.CSharp.RuntimeBinder.Binder.Convert

也许有更好的方法来做到这一点..?

【问题讨论】:

  • 为什么不想回T
  • 对不起。你建议退回 T 吗?
  • 您写了“T 实际上代表正在返回的类”,所以看起来这就是您想要的,是的。
  • 可能是。我会尝试并告诉你。10X

标签: c# selenium generics selenium-webdriver


【解决方案1】:

在您的具体情况下,为什么不返回T

T 实际上代表正在返回的类,根据单击的按钮。

让它看起来正是你想要的。

public T ClickTheMenuButtonWorkplace<T>(string ElementID, T ClassToReturn)
{
    WebDriverWait wait = new WebDriverWait(_webdriver, TimeSpan.FromSeconds(10));
    var MenuButton = wait.Until(x => x.FindElement(By.Id(ElementID)));
    MenuButton.Click();

    return ClassToReturn;
}

...

var IDDetails = new SargelElyon(_webdriver)
        .ClickTheMenuButtonWorkplace("nav_conts", new Lakochot(_webdriver));
// ^ is a Lakochot
IDDetails.foo();

如果您想使用dynamic,您可能在这里不需要,请参阅Tom Coldenhoff's answer

【讨论】:

    猜你喜欢
    • 2020-10-24
    • 2016-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多