So I have a load of instance class's in a namespace I can't change. These are actually exported selenium tests but that detail is not relevant.
Each class has several methods I need to call. Some of the method names are based on the class name. So the methods look like
public void The[Class]Test{ blah blah}
I have code to get all the class's a list of types like this
var tests = (from t in Assembly.GetExecutingAssembly().GetTypes()
where t.IsClass && t.Namespace == "SeleniumTests"
select t).ToList();
I then want to loop over these types and execute the method.
I couldn't figure out how to dynamically inherit the test class with a dynamically created object which has an override or alternative methods for the ones I want to call.
I then tried using an expando object and copying over the bits of the class. this didn't work either because some of the properties I need remain in the class instance and are private so can only be set in the method I need to override.
So basically I think I need some way to modify the behavior of class whose name is known at runtime.
EDIT
The method I want to override sets private properties on the instance. The base class looks something like this like this.
[TestFixture]
public class Login
{
private IWebDriver driver;
[SetUp]
public void SetupTest()
{
driver = new FirefoxDriver();
}
[TearDown]
public void TeardownTest()
{
}
[Test]
public void TheLoginTest()
{
}
}
I want to change the FirefoxDriver() to be Chrome or IE drivers.
So I have tried
static void Main(string[] args)
{
var tests = (from t in Assembly.GetExecutingAssembly().GetTypes()
where t.IsClass && t.Namespace == "SeleniumTests"
select t).ToList();
foreach (var t in tests)
{
var test = (dynamic)Activator.CreateInstance(t);
test.driver = new ChromeDriver();
test.SetupTest();
t.GetMethod(String.Format("The{0}Test", t.Name)).Invoke(test, null);
test.TeardownTest();
}
Console.ReadLine();
}
but this won't work because the driver property is private. This means I think I need to override the SetupTest() method. Which would be fine if I could inherit from Login but I am not sure how to do this when the type is only known through reflection.