我正在開發C#/。NET 3.5應用程序(並希望保留在該版本的.NET),並且無法計算如何使用反射來解決此問題。我找到了解決方法,但不是「整潔」。 代碼如下,我需要發現所有的接口實現,以便將來當添加更多的接口實現時,我不需要更改現有的代碼。如何使用反射來解決這個....? (C#/。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.
}
}
既然你想使用類型,你不應該使用ArrayList。最好用列表 –
CSharpie
2013-04-10 17:31:35
這裏有個類似的問題,你可以看看我的答案有:http://stackoverflow.com/questions/13994579/how-can-i-use-reflection-or-alternative-to -create-function-calls-programatically/13994668#13994668 – user287107 2013-04-10 17:29:54