public void AutoWireEvents(ILogic logic) { var logicType = logic.GetType(); var methodsForAutoWire = TypeCacheUtils.NeedsAutoWire(logicType); foreach (var methodEx in methodsForAutoWire) { var method = methodEx.method; var attr = methodEx.HandlerAttribute; ILogicWithViewModel <IViewModel> logicWithViewModel = logic as ILogicWithViewModel <IViewModel>; string componentName; string eventId; //We have an attribute with no arguments like [Handler] void MethodName(...) { ... } if (attr.IsDefault) { if (methodEx.MethodAndEventParts != null) { var methodNameParts = methodEx.MethodAndEventParts; Debug.Assert(methodNameParts.Count() == 2); componentName = methodNameParts[0]; eventId = methodNameParts[1]; } else { componentName = null; eventId = method.Name; } } else { componentName = attr.GetControl(); eventId = attr.GetEvent(); } TraceUtil.WriteLine(string.Format("AutoWire Event for {0} {1}", componentName, eventId)); //Is this event on a Form? if (logicWithViewModel != null) { if (logicWithViewModel.ViewModel == null) { Debug.Assert(false, "A Form does not have its corresponding ViewModel"); } else { if (componentName != null) { var componentProp = logicWithViewModel.ViewModel.GetType().Property(componentName); if (componentProp != null) { var component = componentProp.GetValue(logicWithViewModel.ViewModel, null) as IStateObject; var controlArray = component is IList; if (component != null && !controlArray) { RegisterMethodForEvent(logicWithViewModel, method, componentName, eventId, component); } else if (component == null && componentProp.PropertyType.IsArray) { var array = logicWithViewModel.ViewModel.GetPropertyValue(componentName) as IList; if (array != null) { foreach (IStateObject element in array) { RegisterMethodForEvent(logicWithViewModel, method, componentName, eventId, element); } } } } } } } else if (logic is IStateObject) { IStateObject stateObject = (IStateObject)logic; //If this is not a form then it must something like an IModel or an IDependentViewModel if (componentName != null) { var componentProp = logicType.Property(componentName); if (componentProp != null) { var component = componentProp.GetValue(logic, null) as IStateObject; if (component != null) { RegisterMethodForEvent(logic, method, componentName, eventId, component); } else { TraceUtil.TraceError("EventAggregator::AutoWriteEvents failed for method [" + method.Name + "] property not found for [" + componentName + "]"); continue; } } } else { RegisterMethodForEvent(logic, method, NONCOMPONENT_EVENT, eventId, stateObject); } } else { throw new NotSupportedException("Events can only be register on object instances implementing ILogicView<T> or (ILogic and IDependentViewModel) or IModel"); } } }