I have a small project that I'm making, and I'm trying to figure out if it's possible to get an instance of every class that inherits from a particular interface.
Here's a simplified example of what I'm trying to accomplish:
public interface IExample
{
string Foo();
}
public class Example1 : IExample
{
public string Foo()
{
return "I came from Example1 !";
}
}
public class Example2 : IExample
{
public string Foo()
{
return "I came from Example2 !";
}
}
//Many more ExampleN's go here
public class ExampleProgram
{
public static void Main(string[] args)
{
var examples = GetExamples();
foreach (var example in examples)
{
Console.WriteLine(example.Foo());
}
}
public static List<IExample> GetExamples()
{
//What goes here?
}
}
Is there any way (short of hard-coding it) for the GetExamples method to return a list containing an instance of each class inheriting from the interface IExample? Any insight you can give would be much appreciated.