コード例 #1
0
        /// <summary>
        /// Populates an array value.
        /// </summary>
        /// <param name="state">The loader state.</param>
        /// <param name="member">The member to populate.</param>
        /// <param name="element">The element that defines the member's value.</param>
        private void PopulateArray(ObjectLoaderState state, ObjectLoaderMember member, XElement element)
        {
            // Make sure that the array is properly defined.
            var itemsRoot = element.Element("Items");

            if (itemsRoot != null && itemsRoot.Elements().Where(x => x.Name != "Item").Any())
            {
                throw new InvalidOperationException(CoreStrings.NonItemElementsInArrayDef);
            }

            // Create the array.
            var items            = (itemsRoot == null) ? new List <XElement>() : itemsRoot.Elements("Item").ToList();
            var arrayElementType = member.MemberType.GetElementType();
            var array            = Array.CreateInstance(arrayElementType, items.Count);

            member.SetValueFromData(array, element);

            // Populate the array's items.
            for (int i = 0; i < items.Count; i++)
            {
                var value = default(Object);
                var type  = GetTypeFromElement(state, arrayElementType, items[i]);
                if (items[i].Elements().Any())
                {
                    value = CreateObject(state, type, null, GetSpecifiedConstructorArguments(items[i]));
                    value = PopulateObjectFromElements(state, value, items[i]);
                }
                else
                {
                    value = ObjectResolver.FromString(items[i].Value, type);
                }
                array.SetValue(value, i);
            }
        }
コード例 #2
0
        /// <summary>
        /// Populates an enumerable value.
        /// </summary>
        /// <param name="state">The loader state.</param>
        /// <param name="member">The member to populate.</param>
        /// <param name="element">The element that defines the member's value.</param>
        private void PopulateEnumerable(ObjectLoaderState state, ObjectLoaderMember member, XElement element)
        {
            var listElemType = GetEnumerableElementType(element.Name.LocalName, member.MemberType);
            var listType     = typeof(List <>).MakeGenericType(listElemType);
            var listInstance = Activator.CreateInstance(listType);

            PopulateListItems(state, listInstance, listElemType, element);

            member.SetValueFromData(listInstance, element);
        }
コード例 #3
0
        /// <summary>
        /// Populates a list value.
        /// </summary>
        /// <param name="state">The loader state.</param>
        /// <param name="member">The member to populate.</param>
        /// <param name="element">The element that defines the member's value.</param>
        private void PopulateList(ObjectLoaderState state, ObjectLoaderMember member, XElement element)
        {
            // Create the list.
            var listImplType = GetListImplementationType(member.MemberType);
            var listElemType = GetListElementType(element.Name.LocalName, listImplType);
            var list         = Activator.CreateInstance(listImplType);

            // Populate the list's members.
            if (!member.IsIndexer)
            {
                PopulateObjectFromAttributes(state, list, element);
            }
            PopulateObjectFromElements(state, list, element, x => x.Name != "Items");
            PopulateListItems(state, list, listElemType, element);

            // Set the list on the object.
            member.SetValueFromData(list, element);
        }