【发布时间】:2013-03-22 09:35:05
【问题描述】:
我有一个 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);
}
}
是否有可能从MainUserControl 中找到所有通过反射实现ISomeInterface 并在该接口上调用方法(void SomeMethod()) 的子UserControl?
【问题讨论】:
-
为什么你认为 GetProperties() 方法会递归下去?您正在枚举类型的属性,而不是整个控件层次结构。考虑改为枚举 Controls 集合。
标签: c# .net wpf reflection interface