我正在开发 C#/.NET 3.5 应用程序(并希望继续使用该版本的 .NET),但不知道如何使用反射解决这个问题。我找到了解决方法,但并不“整洁”。如下代码,我需要发现所有的接口实现,这样以后添加更多接口实现时,我不需要更改现有代码。
interface Ii { }
class A : Ii { }
class A1 : A { }
class A2 : A { }
class A3 : A { }
class B : Ii { }
class C : Ii{ }
// maybe in future class D : Ii { }
// maybe in future class E : Ii { }
class Helper
{
static List<Type> GetAllInterfaceImplemenations()
{// do reflection magic and return ["A1","A2","A3","B","C"] ...
// I will use this method to fill comboBox-es , create objects factory, etc...
// there should be no changes if/when in future I add class D etc.
}
}