【发布时间】:2012-03-13 07:04:43
【问题描述】:
我正在使用 Prism 和 MVVM 开发 WPF 应用程序。
应用程序的要求之一是能够以不同的用户身份登录(具有不同的权限)。
现在大多数权限都是简单的允许或禁止显示特定视图。
它们都被实现为DelegateCommand 或有时作为DelegateCommand<T>
如果用户有权显示特定视图,则这些命令的 CanExecute 将返回 true。 我也有一个持有用户信息和权限的单例 Sessionmanager。
当用户登录时,我正在使用 EventAggregator 触发一个事件。 在所有 ViewModel 的基类中,我订阅了该事件并使用反射 遍历 VM 类型为 DelegateCommand 的所有公共属性,并为该命令调用 RaiseCanExecuteChanged。
Type myType = this.GetType();
IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());
foreach (PropertyInfo prop in props)
{
if (prop.PropertyType == typeof(DelegateCommand))
{
var cmd = (DelegateCommand)prop.GetValue(this, null);
cmd.RasieCanExecuteChanged();
}
}
这适用于所有非泛型 DelegateCommand 属性,但当然不会影响 DelegateCommand<T>。
我的问题是如何确定该属性的类型为 DelegateCommand<T> 并转换为该特定类型以便能够调用 RasieCanExecuteChanged?
【问题讨论】:
标签: c# wpf reflection mvvm delegatecommand