예제 #1
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));
        }
예제 #2
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);
        }