Beispiel #1
0
        public static IComponent CreateComponent(
            IPage page,
            object componentContainer,
            Type type,
            IComponentAttribute attribute)
        {
            var args = typeof(ItemBase).IsAssignableFrom(type)
                           ? new List <object> {
                componentContainer
            }                                                        // костыль
                           : new List <object> {
                page
            };

            if (attribute.Args != null)
            {
                var container = componentContainer as IContainer;
                if (container != null)
                {
                    // Преобразовать относительные пути в абсолютные
                    for (var i = 0; i < attribute.Args.Length; i++)
                    {
                        attribute.Args[i] = CreateInnerSelector(container, attribute.Args[i]);
                    }
                }
                args.AddRange(attribute.Args);
            }
            var component = (IComponent)Activator.CreateInstance(type, args.ToArray());

            component.ComponentName = attribute.ComponentName;
            return(component);
        }
        public static IComponent CreateComponent(
            IPage page, object componentContainer, Type type,
            IComponentAttribute attribute, string componentFieldName)
        {
            var args = typeof(ItemBase).IsAssignableFrom(type)
                ? new List <object> {
                componentContainer
            }                                             // костыль
                : new List <object> {
                page
            };
            var container = componentContainer as IContainer;

            if (attribute.Args != null)
            {
                // Преобразовать относительные пути в абсолютные
                for (var i = 0; i < attribute.Args.Length; i++)
                {
                    args.Add(CreateInnerSelector(container, attribute.Args[i]));
                }
            }

            var componentName = GetComponentName(attribute.ComponentName, componentFieldName, type.Name);

            if (componentContainer is IItem)
            {
                var itemId = (componentContainer as IItem).ID;
                componentName = $"{componentName} ({itemId})";
            }
            IComponent component;

            try
            {
                _createInstanceStopwatch[Thread.CurrentThread.ManagedThreadId].Start();
                component = (IComponent)Activator.CreateInstance(type, args.ToArray());
                _createInstanceStopwatch[Thread.CurrentThread.ManagedThreadId].Stop();
            }
            catch (MissingMemberException)
            {
                Console.WriteLine($"Can not create instance of component '{componentName}' in '{componentContainer.GetType().Name}'.");
                throw;
            }
            component.ComponentName = componentName;
            component.FrameScss     = attribute.FrameScss ?? container?.FrameScss;
            var hasDefaultAction = component as IHasDefaultAction;

            if (hasDefaultAction != null)
            {
                hasDefaultAction.DefaultActionWaitCondition = attribute.DefaultActionWaitCondition;
                hasDefaultAction.DefaultActionWaitTimeout   = attribute.DefaultActionWaitTimeout;
            }
            return(component);
        }