您可以使用反射来执行此操作。我认为在上面的示例代码中,您使用的是类所在的命名空间,而不是程序集的名称。这是一个松散地基于 Selenium 项目本身的单元测试所做的例程。
// Note that assemblyName should normally be "WebDriver". It's provided
// as an argument here just in case there's a third-party driver
// implementation for a browser not supported by the core project.
// driverClassName should be something like "OpenQA.Selenium.Firefox.FirefoxDriver"
public IWebDriver CreateDriver(string assemblyName, driverClassName)
{
// TODO: Handle argument validation and exception handling
// for error conditions.
Assembly assembly = Assembly.Load(assemblyName);
Type driverType = assembly.GetType(driverClassName);
return (IWebDriver)Activator.CreateInstance(driverType);
}