Exemple #1
0
 /// <summary>
 /// Searchs for a appropriate control for a given controller with restriction to a given type.
 /// The control is not (!) attached to the controller. You have to do this manually.
 /// </summary>
 /// <param name="controller">The controller a control is searched for.</param>
 /// <param name="expectedType">The expected type of the control.</param>
 /// <returns>The control with the type provided as expectedType argument, or null if no such controller was found.</returns>
 /// <remarks>This function is used externally, so we add the restriction here that the expected type
 /// has to be a System.Windows.Forms.Control.</remarks>
 public object FindAndAttachControlTo(IMVCController controller, System.Type expectedType)
 {
     return(ReflectionService.GetClassForClassInstanceByAttribute(
                typeof(UserControlForControllerAttribute),
                new Type[] { typeof(System.Windows.Forms.Control), expectedType },
                new object[] { controller }));
 }
Exemple #2
0
        private static void CreateControlForControllerWithNoExpectedTypeOfViewAttribute(IMVCController controller, System.Type guiControlType)
        {
            object control =
                ReflectionService.GetClassForClassInstanceByAttribute(
                    typeof(UserControlForControllerAttribute),
                    new System.Type[] { guiControlType },
                    new object[] { controller });

            if (null != control)
            {
                controller.ViewObject = control;
            }
        }
Exemple #3
0
        /// <summary>
        /// Searchs for a appropriate control for a given controller with restriction to a given type.
        /// The control is not (!) attached to the controller. You have to do this manually.
        /// </summary>
        /// <param name="controller">The controller a control is searched for.</param>
        /// <param name="expectedType">The expected type of the control.</param>
        /// <returns>The control with the type provided as expectedType argument, or null if no such controller was found.</returns>
        public object FindAndAttachControlTo(IMVCController controller, System.Type expectedType)
        {
            object result = null;

            foreach (var guiControlType in RegisteredGuiTechnologies)
            {
                result = ReflectionService.GetClassForClassInstanceByAttribute(
                    typeof(UserControlForControllerAttribute),
                    new Type[] { guiControlType, expectedType },
                    new object[] { controller });
                if (null != result)
                {
                    break;
                }
            }
            return(result);
        }
Exemple #4
0
        /// <summary>
        /// Searchs for a appropriate control for a given controller and attaches the control to the controller.
        /// </summary>
        /// <param name="controller">The controller a control is searched for.</param>
        public void FindAndAttachControlTo(IMVCController controller)
        {
            // if the controller has
            System.Type ct             = controller.GetType();
            object[]    viewattributes = ct.GetCustomAttributes(typeof(ExpectedTypeOfViewAttribute), false);

            if (viewattributes != null && viewattributes.Length > 0)
            {
                if (viewattributes.Length > 1)
                {
                    Array.Sort(viewattributes);
                }

                foreach (ExpectedTypeOfViewAttribute attr in viewattributes)
                {
                    Type[] controltypes = ReflectionService.GetNonAbstractSubclassesOf(new System.Type[] { typeof(System.Windows.Forms.Control), attr.TargetType });
                    foreach (Type controltype in controltypes)
                    {
                        // test if the control has a special preference for a controller...
                        object[] controlForAttributes = controltype.GetCustomAttributes(typeof(UserControlForControllerAttribute), false);
                        if (controlForAttributes.Length > 0)
                        {
                            bool containsControllerType = false;
                            foreach (UserControlForControllerAttribute controlForAttr in controlForAttributes)
                            {
                                if (ReflectionService.IsSubClassOfOrImplements(ct, controlForAttr.TargetType))
                                {
                                    containsControllerType = true;
                                    break;
                                }
                            }
                            if (!containsControllerType)
                            {
                                continue; // then this view is not intended for our controller
                            }
                        }

                        // all seems ok, so we can try to create the control
                        object controlinstance = System.Activator.CreateInstance(controltype);
                        if (null == controlinstance)
                        {
                            throw new ApplicationException(string.Format("Searching a control for controller of type {0}. Find control type {1}, but it was not possible to create a instance of this type.", ct, controltype));
                        }

                        controller.ViewObject = controlinstance;

                        if (controller.ViewObject == null)
                        {
                            throw new ApplicationException(string.Format("Searching a control for controller of type {0}. Find control type {1}, but the controller did not accept this control.", ct, controltype));
                        }

                        return;
                    }
                }
            }
            else // Controller has no ExpectedTypeOfView attribute
            {
                System.Windows.Forms.UserControl control =
                    (System.Windows.Forms.UserControl)ReflectionService.GetClassForClassInstanceByAttribute(
                        typeof(UserControlForControllerAttribute),
                        new System.Type[] { typeof(System.Windows.Forms.UserControl) },
                        new object[] { controller });

                if (control == null)
                {
                    return;
                }

                controller.ViewObject = control;
            }
        }