lundi 1 février 2016

How to use Reflection to attach event handler

I need to update the Enabled state of my Ribbon buttons based on my ViewModel's commands. Since VSTO Ribbons do not (yet?) support good old command binding, I'm trying to achieve this by listening to CanExecuteChanged event of all RelayCommand properties of my ViewModel. Instead of attaching every property separately, I thought I'd use Reflection and get all properties of RelayCommand type and attach my handler in a loop.

I can get all properties using following simple code:

var props = VM.GetType().GetProperties();
foreach (var prop in props)
{
  if (prop.PropertyType == typeof(RelayCommand))
     ????.CanExecuteChanged += MyHandler;
}

What do I need to write in place of ?????





Aucun commentaire:

Enregistrer un commentaire