How to pass a type into a method that inherits a specific interface.
Example
I have an interface
public interface iBase
{
void DoWork(object t);
}
I have a class that inherits the interface
public class WorkerClass : iBase
{
public void DoWork(object t)
{
}
}
The following code uses the 2 classes above
class Program
{
static void Main(string[] args)
{
Start(typeof(WorkerClass));
}
public static void Start(Type type)
{
iBase workerClass = (iBase)Activator.CreateInstance(type);
workerClass.DoWork("tst");
}
}
The above code works, but I want to make it so the Start's input parameter (Type type) only accepts type’s that are inherited from iBase. At present "type" will accept any type which is not what I want.
Aucun commentaire:
Enregistrer un commentaire