public static ReflectiveCommand GetCommandForMember(object view, MemberInfo member)
{
string propertyName = string.Empty;
PropertyInfo propertyInfo = null;
var commandOption = CommandOption.Disable;
var buttonAttribute = member.GetCustomAttribute<ButtonAttribute>();
if (buttonAttribute != null)
{
propertyName = buttonAttribute.CanExecutePropertyName;
commandOption = buttonAttribute.CommandOption;
}
var toolbarButtonAttribute = member.GetCustomAttribute<ToolbarButtonAttribute>();
if (toolbarButtonAttribute != null)
{
propertyName = toolbarButtonAttribute.CanExecutePropertyName;
commandOption = toolbarButtonAttribute.CommandOption;
}
var navbarButtonAttribute = member.GetCustomAttribute<NavbarButtonAttribute>();
if (navbarButtonAttribute != null)
{
propertyName = navbarButtonAttribute.CanExecutePropertyName;
commandOption = navbarButtonAttribute.CommandOption;
}
var methodInfo = member as MethodInfo;
if (methodInfo == null)
throw new Exception(string.Format("Method not found {0}", member.Name));
object source = view;
if (!string.IsNullOrEmpty(propertyName))
{
PropertyInfo property = source.GetType().GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
if (property == null)
{
var dataContext = view as IDataContext<object>;
if (dataContext != null)
{
var vm = dataContext.DataContext;
if (vm != null)
{
source = vm;
property = source.GetType().GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
}
}
}
if (property != null)
{
if (property.PropertyType == typeof(bool) || property.PropertyType == typeof(bool?))
{
propertyInfo = property;
}
else
{
throw new Exception(string.Format("Property {0} cannot be used for CanExecute property because it does not have a return type of bool", property.Name));
}
}
}
return new ReflectiveCommand(view, methodInfo, source, propertyInfo) { CommandOption = commandOption };
}