Exemplo n.º 1
0
        private object CreateComponent(Type componentType, ISearchContext globalContext, ISearchContext searchContext, By @by, object[] additionalArgs)
        {
            ComponentBase obj;

            if (typeof(PageBase).IsAssignableFrom(componentType))
            {
                obj = Activator.CreateInstance(componentType, searchContext, by, this) as ComponentBase;
            }
            else
            {
                if (IsPortalComponent(componentType))
                {
                    by = new PortalSelector(globalContext, by);
                }

                additionalArgs = additionalArgs ?? new object[0];
                obj            = Activator.CreateInstance(componentType, new object[] { searchContext, by }.Concat(additionalArgs).ToArray()) as ComponentBase;
            }

            foreach (var property in componentType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty | BindingFlags.GetProperty).Where(x => x.CanWrite))
            {
                var propertyType   = property.PropertyType;
                var cssTidSelector = CreateCssSelectorByTid(property.Name);
                var propertyVal    = TryCreate(propertyType, globalContext, obj?.Container, cssTidSelector, property.GetCustomAttributes <AdditionalConstructorArgsAttribute>().SingleOrDefault()?.Args);
                if (propertyVal != null)
                {
                    property.SetValue(obj, propertyVal);
                }
            }

            return(obj);
        }