[英]How to call constructor of the generic class explicitly in c#
I have the below class, how can I invoke the constructor of the class explicitly? 我有下面的类,如何显式调用该类的构造函数?
[TestFixture(typeof(ChromeDriver))]
public class BaseWebDriver<TWebDriver> where TWebDriver : IWebDriver, new()
{
public IWebDriver Driver { get; set; }
public WebDriverWait wait;
public BaseWebDriver()
{
Driver = new TWebDriver();
}
[OneTimeSetUp]
public virtual void SetupTest()
{
// Go to the login page
Driver.Navigate().GoToUrl("LoginUrl");
}
[OneTimeTearDown]
public virtual void TearDownTest()
{
Driver.Quit();
Driver.Dispose();
Console.WriteLine("***TearDown***\n");
}
public void restartBrowser()
{
Driver.Quit();
//Here I have to call the constructor of the class to open the browser again
}
}
Is it possible to call the constructor from a derived class? 是否可以从派生类调用构造函数? I am new to generics, so kindly help. 我是泛型新手,请帮忙。
As Andrew said, Driver = new TWebDriver() works. 如Andrew所说,Driver = new TWebDriver()有效。 Thanks. 谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.