I have two classes:
class Car<T>
{
public string color { get; set; }
public virtual T features { get; set; }
public virtual void TestDrive();
}
class Toyota : Car<ToyotaFeatures>
{
public override ToyotaFeatures features { get; set; }
public override void TestDrive()
{
//Logic here...
}
}
Now i have a full string class name: “MySol.MyProj.Toyota”
I want to instantiate a class by my string name and then run TestDrive().
The problem is that when I try to run Activator.CreateInstance(null, "MySol.MyProj.Toyota");
I cannot cast it to a base class and run testDrive because it is expecting ToyotaFeatures class to be passed to it. but I just want to run TestDrive() only having a string class name.
And I don’t want to cast it to specific type. only to base type so it can decide which TestDrive() to run based on the provided string.
For that purposes interfaces are extra good.
Your abstract class implements the interface:
And casting comes very easy: