【发布时间】:2010-11-15 07:35:06
【问题描述】:
我正在做一个小项目,我试图弄清楚是否有可能获得从特定接口继承的每个类的实例。
这是我想要完成的一个简化示例:
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?
}
}
GetExamples 方法是否有任何方法(没有硬编码)来返回一个列表,该列表包含从接口 IExample 继承的每个类的实例?非常感谢您提供的任何见解。
【问题讨论】:
标签: c# inheritance instance