Beispiel #1
0
        protected void PrepareChildrenElements()
        {
            PropertyInfo[] pInfos = this.GetType().GetProperties();
            this.Items     = null;
            this.ItemTypes = null;

            List <Tuple <int, IEnumerable <Element> > > childElementList = new List <Tuple <int, IEnumerable <Element> > >();

            foreach (PropertyInfo pInfo in pInfos)
            {
                ChildElementAttribute[] attributes = pInfo.GetCustomAttributes(typeof(ChildElementAttribute), true) as ChildElementAttribute[];
                if (attributes.Count() > 0)
                {
                    ChildElementAttribute attribute = attributes[0];

                    object pValue = pInfo.GetValue(this, null);

                    if (pValue != null)
                    {
                        if (attribute.ChildCategory == ChildCategory.Element)
                        {
                            var childElement = pValue as Element;
                            if (childElement != null)
                            {
                                childElement.PrepareChildrenElements();
                            }
                        }
                        else if (attribute.ChildCategory == ChildCategory.ChildrenColection || attribute.ChildCategory == ChildCategory.ChildrenList)
                        {
                            IEnumerable <Element> elements = pValue as IEnumerable <Element>;

                            if (elements != null)
                            {
                                if (elements.Count() == 0)
                                {
                                    pInfo.SetValue(this, null, null);
                                }
                                else
                                {
                                    foreach (var element in elements)
                                    {
                                        element.PrepareChildrenElements();
                                    }
                                    if (attribute.ChildCategory == ChildCategory.ChildrenColection)
                                    {
                                        childElementList.Add(new Tuple <int, IEnumerable <Element> >(attribute.Order, elements));
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (childElementList.Count > 0)
            {
                List <Element> tElements = new List <Element>();
                foreach (var item in childElementList.OrderBy(row => row.Item1))
                {
                    tElements.AddRange(item.Item2);
                }
                this.Items     = tElements.ToArray();
                this.ItemTypes = tElements.Select(row => row.ElementType).ToArray();
            }
        }
Beispiel #2
0
        protected void RevertChildrenElements()
        {
            Dictionary <string, List <Element> > dic = new Dictionary <string, List <Element> >();

            if (this.Items != null)
            {
                var group = this.Items.GroupBy(row => row.GetType().FullName);
                foreach (var item in group)
                {
                    dic.Add(item.Key, item.ToList <Element>());
                }
            }
            PropertyInfo[] pInfos = this.GetType().GetProperties();

            foreach (var pInfo in pInfos)
            {
                object pValue = pInfo.GetValue(this, null);
                ChildElementAttribute[] attributes = pInfo.GetCustomAttributes(typeof(ChildElementAttribute), true) as ChildElementAttribute[];
                if (attributes.Count() > 0)
                {
                    ChildElementAttribute attribute = attributes[0];

                    if (attribute.ChildCategory == ChildCategory.Element)
                    {
                        var childElement = pValue as Element;
                        if (childElement != null)
                        {
                            childElement.RevertChildrenElements();
                        }
                    }
                    else
                    {
                        if (dic.Keys.Contains(attribute.ChildType.ToString()))
                        {
                            Type  t    = attribute.ChildType;
                            IList list = Element.CreateGetInvoker(this.GetType(), pInfo)(this) as IList; //; pInfo.GetGetMethod().Invoke(this, null) as IList;
                            if (list == null)
                            {
                                continue;
                            }
                            IList values = dic[attribute.ChildType.ToString()] as IList;
                            if (values == null)
                            {
                                continue;
                            }
                            foreach (var item in values)
                            {
                                Element.ListAdd(list, item, t);
                            }

                            IEnumerable <Element> elements = pValue as IEnumerable <Element>;

                            if (elements != null)
                            {
                                foreach (var element in elements)
                                {
                                    element.RevertChildrenElements();
                                }
                            }
                        }
                    }
                }
            }
        }