我有一個UserControl
有幾個子女UserControl
和那些UserControl
的孩子UserControl的。如何找到實現一個接口和調用方法的所有對象
考慮一下:
MainUserControl
TabControl
TabItem
UserControl
UserControl
UserControl : ISomeInterface
TabItem
UserControl
UserControl
UserControl : ISomeInterface
TabItem
UserControl
UserControl
UserControl : ISomeInterface
TabItem
UserControl
UserControl
UserControl : ISomeInterface
這是我到目前爲止,但沒有發現任何ISomeInterface
:
PropertyInfo[] properties = MainUserControl.GetType().GetProperties();
foreach (PropertyInfo property in properties)
{
if (typeof(ISomeInterface).IsAssignableFrom(property.PropertyType))
{
property.GetType().InvokeMember("SomeMethod", BindingFlags.InvokeMethod, null, null, null);
}
}
是否有可能因此發現所有的孩子UserControl
的從實現ISomeInterface
MainUserControl
通過反射並在該界面上調用方法(void SomeMethod()
)?
爲什麼你認爲的GetProperties()方法將遞歸下去?您正在枚舉該類型的屬性,而不是整個控件層次結構。考慮枚舉Controls集合。 –