コード例 #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 }));
 }
コード例 #2
0
        /// <summary>
        /// Tries to get a class instance for a given attribute type. All loaded assemblies are searched for classes that attributeType applies to,
        /// then for all found classes the instantiation of a class is tried, until a instance is created successfully. Here, the attributeType has
        /// to implement <see cref="IClassForClassAttribute" />, and creationArg[0] has to match the type in <see cref="IClassForClassAttribute.TargetType" />
        /// </summary>
        /// <param name="attributeType">The type of attribute  the class(es) to instantiate must be assigned to.</param>
        /// <param name="expectedTypes">The expected type of return value.</param>
        /// <param name="creationArgs">The creation arguments used to instantiate a class.</param>
        /// <param name="overrideArgs0Type">Usually null. If you provide a type here, it has to be a base type of the typeof(creationArgs[0]). By this you
        /// can "downgrade" creationArgs[0], so that only attributes for the base type are looked for.</param>
        /// <returns>The instance of the first class for which the instantiation was successfull and results in the expectedType. Otherwise null.</returns>
        /// <remarks>The instantiation is tried first with the full argument list. If that fails, the last element of the argument list is chopped and the instantiation is tried again.
        /// This process is repeated until the instantiation was successfull or the argument list is empty (empty constructor is tried at last).</remarks>
        public static object GetClassForClassInstanceByAttribute(System.Type attributeType, System.Type[] expectedTypes, object[] creationArgs, System.Type overrideArgs0Type)
        {
            // 1st search for all classes that wear the UserControllerForObject attribute
            IAttributeForClassList list = ReflectionService.GetAttributeInstancesAndClassTypesForClass(attributeType, creationArgs[0], overrideArgs0Type);

            return(CreateInstanceFromList(list, expectedTypes, creationArgs));
        }
コード例 #3
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;
            }
        }
コード例 #4
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);
        }
コード例 #5
0
        /// <summary>
        /// Gets an <see cref="IMVCController" />  for a given document type, and finding the right GUI user control for it.
        /// </summary>
        /// <param name="args">The argument list. The first element args[0] is the document for which the controller is searched. The following elements are
        /// optional, and are usually the parents of this document.</param>
        /// <param name="overrideArg0Type">If this parameter is not null, this given type is used instead of determining the type of the <c>arg[0]</c> argument.</param>
        /// <param name="expectedControllerType">Type of controller that you expect to return.</param>
        /// <param name="copyDocument">Determines whether to use the document directly or a cloned copy.</param>
        /// <returns>The controller for that document when found. The controller is already initialized with the document. If no controller is found for the document, or if no GUI control is found for the controller, the return value is null.</returns>
        public IMVCController GetControllerAndControl(object[] args, System.Type overrideArg0Type, System.Type expectedControllerType, UseDocument copyDocument)
        {
            if (!ReflectionService.IsSubClassOfOrImplements(expectedControllerType, typeof(IMVCController)))
            {
                throw new ArgumentException("Expected controller type has to be IMVCController or a subclass or derived class of this");
            }

            IMVCController controller = GetController(args, overrideArg0Type, typeof(IMVCController), copyDocument);

            if (controller == null)
            {
                return(null);
            }

            FindAndAttachControlTo(controller);

            return(controller);
        }
コード例 #6
0
        /// <summary>
        /// Try to attach a control to the controller. To determine the type of gui, the viewTemplate is analysed.
        /// </summary>
        /// <param name="controller"></param>
        /// <param name="viewTemplate"></param>
        public void FindAndAttachControlUsingGuiTemplate(IMVCController controller, object viewTemplate)
        {
            foreach (var guiType in RegisteredGuiTechnologies)
            {
                if (ReflectionService.IsSubClassOfOrImplements(viewTemplate.GetType(), guiType))
                {
                    InternalFindAndAttachControlUsingGuiType(controller, guiType);
                    if (controller.ViewObject != null)
                    {
                        return;
                    }
                }
            }

            if (controller.ViewObject == null)
            {
                FindAndAttachControlTo(controller);
            }
        }
コード例 #7
0
        /// <summary>
        /// Tries to get a class instance for a given attribute type. All loaded assemblies are searched for classes that attributeType applies to,
        /// then for all found classes the instantiation of a class is tried, until a instance is created successfully.
        /// </summary>
        /// <param name="attributeType">The type of attribute  the class(es) to instantiate must be assigned to.</param>
        /// <param name="expectedType">The expected type of return value.</param>
        /// <param name="creationArgs">The creation arguments used to instantiate a class.</param>
        /// <returns>The instance of the first class for which the instantiation was successfull and results in the expectedType. Otherwise null.</returns>
        public static object GetClassInstanceByAttribute(System.Type attributeType, System.Type expectedType, object[] creationArgs)
        {
            object result = null;
            // 1st search for all classes that wear the UserControllerForObject attribute
            IEnumerable <Type> list = ReflectionService.GetSortedClassTypesHavingAttribute(attributeType, false);

            foreach (Type definedType in list)
            {
                if (IsSubClassOfOrImplements(definedType, expectedType))
                {
                    // try to create the class
                    try
                    {
                        result = Activator.CreateInstance(definedType, creationArgs);
                        break;
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            return(result);
        }
コード例 #8
0
        /// <summary>
        /// Gets an <see cref="IMVCController" />  for a given document type.
        /// </summary>
        /// <param name="creationArgs">The argument list. The first element args[0] is the document for which the controller is searched. The following elements are
        /// optional, and are usually the parents of this document.</param>
        /// <param name="overrideArg0Type">If this parameter is not null, this given type is used instead of determining the type of the <c>arg[0]</c> argument.</param>
        /// <param name="expectedControllerType">Type of controller that you expect to return.</param>
        /// <param name="copyDocument">Determines wether to use the document directly or use a clone of the document.</param>
        /// <returns>The controller for that document when found.</returns>
        public IMVCController GetController(object[] creationArgs, System.Type overrideArg0Type, System.Type expectedControllerType, UseDocument copyDocument)
        {
            if (!ReflectionService.IsSubClassOfOrImplements(expectedControllerType, typeof(IMVCController)))
            {
                throw new ArgumentException("Expected controller type has to be IMVCController or a subclass or derived class of this");
            }

            object result = null;

            // 1st search for all classes that wear the UserControllerForObject attribute
            ReflectionService.IAttributeForClassList list = ReflectionService.GetAttributeInstancesAndClassTypesForClass(typeof(UserControllerForObjectAttribute), creationArgs[0], overrideArg0Type);

            foreach (Type definedType in list.Types)
            {
                if (ReflectionService.IsSubClassOfOrImplements(definedType, typeof(IMVCANController)))
                {
                    IMVCANController mvcan = (IMVCANController)Activator.CreateInstance(definedType);
                    mvcan.UseDocumentCopy = copyDocument;
                    if (mvcan.InitializeDocument(creationArgs))
                    {
                        result = mvcan;
                    }
                }
                else
                {
                    result = ReflectionService.CreateInstanceFromList(list, new Type[] { expectedControllerType }, creationArgs);
                }

                if (result is IMVCController)
                {
                    break;
                }
            }

            return((IMVCController)result);
        }
コード例 #9
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;
            }
        }
コード例 #10
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>
        /// <param name="guiControlType">Base type of the underlying Gui system.</param>
        private void InternalFindAndAttachControlUsingGuiType(IMVCController controller, System.Type guiControlType)
        {
            // if the controller has
            System.Type ct             = controller.GetType();
            object[]    viewattributes = ct.GetCustomAttributes(typeof(ExpectedTypeOfViewAttribute), false);
            if (null == viewattributes || viewattributes.Length == 0)
            {
                viewattributes = ct.GetCustomAttributes(typeof(ExpectedTypeOfViewAttribute), true);
            }

            bool isInvokeRequired = Current.Dispatcher.InvokeRequired;

            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[] { guiControlType, attr.TargetType });
                    if (controltypes.Length > 1)
                    {
                        // retrieve priorities for the types, and then sort controltypes using priority descending

                        var priorities = new int[controltypes.Length];
                        for (int i = 0; i < controltypes.Length; ++i)
                        {
                            object[] pattributes = controltypes[i].GetCustomAttributes(typeof(UserControlPriorityAttribute), false);
                            if (null != pattributes && pattributes.Length > 0)
                            {
                                priorities[i] = -((UserControlPriorityAttribute)pattributes[0]).Priority;
                            }
                        }
                        Array.Sort(priorities, controltypes); // the negation of priority (see above) ensures that the sorting is priority descending
                    }

                    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

                        if (isInvokeRequired)
                        {
                            Current.Dispatcher.InvokeIfRequired(CreateAndAttachControlOfType, controller, controltype);
                        }
                        else
                        {
                            CreateAndAttachControlOfType(controller, controltype);
                        }
                        return;
                    }
                }
            }
            else // Controller has no ExpectedTypeOfView attribute
            {
                if (isInvokeRequired)
                {
                    Current.Dispatcher.InvokeIfRequired(CreateControlForControllerWithNoExpectedTypeOfViewAttribute, controller, guiControlType);
                }
                else
                {
                    CreateControlForControllerWithNoExpectedTypeOfViewAttribute(controller, guiControlType);
                }
            }
        }