/// <summary> /// Populates a list with items. /// </summary> /// <param name="state">The loader state.</param> /// <param name="list">The list to populate.</param> /// <param name="listElemType">The type of elements in the list.</param> /// <param name="element">The element that defines the list.</param> private void PopulateListItems(ObjectLoaderState state, Object list, Type listElemType, XElement element) { // Make sure that the list is properly defined. var itemsRoot = element.Element("Items"); if (itemsRoot != null && itemsRoot.Elements().Where(x => x.Name != "Item").Any()) { throw new InvalidOperationException(NucleusStrings.NonItemElementsInListDef); } // Populate the list's items. if (itemsRoot != null) { var add = list.GetType().GetMethod("Add", new[] { listElemType }); var items = itemsRoot.Elements("Item").ToList(); for (int i = 0; i < items.Count; i++) { var value = default(Object); var type = GetTypeFromElement(state, listElemType, 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); } add.Invoke(list, new[] { value }); } } }
/// <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(NucleusStrings.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); } }
/// <inheritdoc/> public override Object ReadJson(JsonReader reader, Type objectType, Object existingValue, JsonSerializer serializer) { if (reader.TokenType != JsonToken.String) { throw new JsonReaderException(NucleusStrings.JsonObjectResolverRequiresString.Format(reader.TokenType)); } return(ObjectResolver.FromString((String)reader.Value, objectType)); }
/// <summary> /// Converts the given object to the type of this converter, using the specified context and culture information. /// </summary> /// <param name="context">An <see cref="ITypeDescriptorContext"/> that provides a format context.</param> /// <param name="culture">The <see cref="CultureInfo"/> to use as the current culture.</param> /// <param name="value">The <see cref="Object"/> to convert.</param> /// <returns>An <see cref="Object"/> that represents the converted value.</returns> public override Object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) { if (value is String) { if (String.IsNullOrEmpty((String)value)) { return(default(T)); } var type = (context == null) ? typeof(T) : context.PropertyDescriptor.PropertyType; return(ObjectResolver.FromString((String)value, type)); } return(base.ConvertFrom(context, culture, value)); }
/// <summary> /// Populates an object from the attributes defined on the specified data element. /// </summary> /// <param name="state">The loader state.</param> /// <param name="objectInstance">The object instance.</param> /// <param name="objectElement">The object element.</param> /// <returns>The object instance.</returns> private Object PopulateObjectFromAttributes(ObjectLoaderState state, Object objectInstance, XElement objectElement) { foreach (var attr in objectElement.Attributes()) { if (IsReservedKeyword(attr.Name.LocalName)) { continue; } var attrMember = ObjectLoaderMember.Find(objectInstance, attr.Name.LocalName, state.IgnoreMissingMembers); if (attrMember != null) { if (state.Resolver != null && state.Resolver(objectInstance, attr.Name.LocalName, attr.Value)) { continue; } var attrValue = ObjectResolver.FromString(attr.Value, attrMember.MemberType); attrMember.SetValue(attrValue, null); } } return(objectInstance); }
/// <summary> /// Gets a set of index parameters from the specified data element. /// </summary> /// <param name="element">The data element from which to retrieve index parameters.</param> /// <returns>The index parameters that were retrieved.</returns> private Object[] GetIndexParameters(XElement element) { var indexParameterValues = new Object[indexParameters.Length]; for (int i = 0; i < indexParameters.Length; i++) { var indexParameter = indexParameters[i]; var attr = element.Attribute(indexParameter.Name); if (attr == null) { throw new InvalidOperationException(NucleusStrings.DataObjectMissingIndexParam.Format(indexParameter.Name)); } indexParameterValues[i] = ObjectResolver.FromString(attr.Value, indexParameter.ParameterType); } if (element.Attributes().Where(x => !ObjectLoaderXmlSerializer.IsReservedKeyword(x.Name.LocalName)).Count() > indexParameterValues.Length) { throw new InvalidOperationException(NucleusStrings.DataObjectHasTooManyIndexParams); } return(indexParameterValues); }
/// <summary> /// Attempts to parse a string into a simple value. /// </summary> /// <param name="str">The string to parse.</param> /// <param name="type">The type into which to parse the string.</param> /// <param name="provider">An object that provides culture-specific formatting information.</param> /// <returns>The value that was parsed.</returns> private Object ParseValue(String str, Type type, IFormatProvider provider) { return(ObjectResolver.FromString(str, type, provider)); }