public void Invoke(object instance, params object[] parameter) { if (instance == null) { throw new ArgumentException("Argument must not be null.", "instance"); } ViewModelMappingAttribute mappingAttribute = ViewModelMappingAttribute.GetFromType(ParentViewItem.PageViewType); Type viewModelType = mappingAttribute.ViewModelType; if (instance.GetType() != viewModelType) { throw new ArgumentException(String.Format("Argument must be of type '{0}'.", ParentViewItem.PageViewType.Name), "instance"); } MethodInfo method = viewModelType.GetMethod(MethodName); if (method != null) { method.Invoke(instance, parameter); } else { App.AppLogger.Logger.Log(devoctomy.DFramework.Logging.Interfaces.LoggerMessageType.Error, "Could not find the method named '{0}' for the type '{1}'", MethodName, viewModelType.Name); } }
public Object GetViewModel(View view) { App.AppLogger.Logger.Log(devoctomy.DFramework.Logging.Interfaces.LoggerMessageType.VerboseMed | devoctomy.DFramework.Logging.Interfaces.LoggerMessageType.Information, "Getting view model for view type '{0}'.", view.GetType().Name); Object[] mappingAttributes = view.GetType().GetCustomAttributes(typeof(ViewModelMappingAttribute), true); if (mappingAttributes != null && mappingAttributes.Length == 1) { ViewModelMappingAttribute mappingAttribute = (ViewModelMappingAttribute)mappingAttributes[0]; List <Object> modelList; if (!_viewModelCache.ContainsKey(view)) { modelList = new List <Object>(); _viewModelCache.Add(view, modelList); } else { modelList = _viewModelCache[view]; } switch (mappingAttribute.Instancing) { case ViewModelMappingAttribute.InstancingType.Single: { if (modelList.Count == 1) { App.AppLogger.Logger.Log(devoctomy.DFramework.Logging.Interfaces.LoggerMessageType.VerboseMed | devoctomy.DFramework.Logging.Interfaces.LoggerMessageType.Information, "Returning existing single instance of view model type '{0}'.", mappingAttribute.ViewModelType); return(modelList[0]); } else { App.AppLogger.Logger.Log(devoctomy.DFramework.Logging.Interfaces.LoggerMessageType.VerboseMed | devoctomy.DFramework.Logging.Interfaces.LoggerMessageType.Information, "Creating first single instance of view model type '{0}'.", mappingAttribute.ViewModelType); Object viewModel = Activator.CreateInstance(mappingAttribute.ViewModelType, view); modelList.Add(viewModel); return(viewModel); } } case ViewModelMappingAttribute.InstancingType.Multiple: { DLoggerManager.Instance.Logger.Log(devoctomy.DFramework.Logging.Interfaces.LoggerMessageType.VerboseMed | devoctomy.DFramework.Logging.Interfaces.LoggerMessageType.Information, "Creating new multi-instance of view model type '{0}'.", mappingAttribute.ViewModelType); Object viewModel = Activator.CreateInstance(mappingAttribute.ViewModelType, view); modelList.Add(viewModel); return(viewModel); } default: { throw new InvalidOperationException(String.Format("Instancing type of '{0}' is invalid.", view.GetType().Name)); } } } else { return(null); } }