简体   繁体   English

如何在C#中显式调用泛型类的构造函数

[英]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.

相关问题 C#如何在没有显式类型转换的情况下调用基类==运算符? - C# How to call the base class == operator, without explicitly typecasting? VS2005中的C#:在继承的类中,您是否需要显式调用超级构造函数? - C# in VS2005: in an inherited class, do you need to explicitly call the super-constructor? C#如何捕获泛型抽象类构造函数的异常 - C# How to catch an exception on generic abstract class constructor C#中类的泛型调用 - generic call of class in c# 对于结构体,我是否必须在C#中显式调用构造函数? - For structs, do I have to call the constructor explicitly in C#? 带有“专用”构造函数的C#Generic Class - C# Generic Class with “specialized” constructor C#通用基类,参数化构造函数 - C# Generic Base Class, Parameterized constructor 如何更改类的构造函数以接收已创建的类和构造函数的对象的通用列表? (C#) - How to change constructor of a class to recieve generic list of objects of already created class and constructor? (C#) 从Type with Reflection获取类并使用Type in C#调用泛型构造函数 - Get class from Type with Reflection and call a generic constructor with Type in C# 如何在C#中向非泛型类的构造函数添加泛型约束 - How to add a generic constraint to a constructor of a non-generic class in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM