Exemple #1
0
		private void WriteCollection(XPathResult result, ref object value, IDictionaryAdapter dictionaryAdapter)
		{
			if (value is byte[])
			{
				var node = result.GetNavigator(true);
				node.SetValue(Convert.ToBase64String((byte[])value));
				return;
			}

			result.Remove(value == null);

			if (value != null)
			{
				if (result.Type.IsArray)
				{
					WriteArray(result, value, dictionaryAdapter);
				}
				else if (result.Type.IsGenericType)
				{
					WriteList(result, value, dictionaryAdapter);
				}
				if (result.Property != null)
				{
					value = ((IDictionaryPropertyGetter)this).GetPropertyValue(dictionaryAdapter, result.Key, null, result.Property, false);
				}
			}
		}
Exemple #2
0
		private object ReadList(XPathResult result, IDictionaryAdapter dictionaryAdapter)
		{
			Type listType = null, initializerType = null;
			var arguments = result.Type.GetGenericArguments();
			var genericDef = result.Type.GetGenericTypeDefinition();
			var itemType = arguments[0];

			Func<Type, XmlMetadata> getXmlMeta = type => dictionaryAdapter.GetXmlMeta(type);
			var itemNodes = result.GetNodes(itemType, getXmlMeta);
			if (itemNodes == null) return null;

			if (genericDef == typeof(IEnumerable<>) || genericDef == typeof(ICollection<>) || genericDef == typeof(List<>))
			{
				listType = typeof(EditableList<>).MakeGenericType(arguments);
			}
			else if (
#if !DOTNET35
				//NOTE: what about SortedSet?
				genericDef == typeof(ISet<>) || 
#endif
				genericDef == typeof(HashSet<>))
			{
				listType = typeof(List<>).MakeGenericType(arguments);
			}
			else
			{
				listType = typeof(EditableBindingList<>).MakeGenericType(arguments);
				initializerType = typeof(BindingListInitializer<>).MakeGenericType(arguments);
			}
			
			var list = (IList)Activator.CreateInstance(listType);

			foreach (var item in itemNodes)
			{
				list.Add(ReadProperty(item, false, dictionaryAdapter));
			}

			if (
#if !DOTNET35
				//NOTE: what about SortedSet?
				genericDef == typeof(ISet<>) ||
#endif
				genericDef == typeof(HashSet<>))
			{
				return Activator.CreateInstance(typeof(HashSet<>).MakeGenericType(arguments), list);
			}

			if (initializerType != null)
			{
				Func<object> addNew = () =>
				{
					var node = result.CreateNode(itemType, null, getXmlMeta);
					return ReadProperty(node, false, dictionaryAdapter);
				};

				Func<int, object, object> addAt = (index, item) =>
				{
					var node = result.CreateNode(itemType, item, getXmlMeta);
					WriteProperty(node, ref item, dictionaryAdapter);
					return item;
				};

				Func<int, object, object> setAt = (index, item) =>
				{
					var node = result.GetNodeAt(itemType, index);
					WriteProperty(node, ref item, dictionaryAdapter);
					return item;
				};

				Action<int> removeAt = index =>
				{
					object value = list;
					if (dictionaryAdapter.ShouldClearProperty(result.Property, value))
					{
						result.Remove(true);
						return;
					}
					result.RemoveAt(index);
				};

				Action reset = () =>
				{
					object value = list;
					if (dictionaryAdapter.ShouldClearProperty(result.Property, value))
					{
						result.Remove(true);
						return;
					}
					WriteCollection(result, ref value, dictionaryAdapter);
				};

				var initializer = (IValueInitializer)Activator.CreateInstance(
					initializerType, addAt, addNew, setAt, removeAt, reset);
				initializer.Initialize(dictionaryAdapter, list);
			}

			return list;
		}
Exemple #3
0
		private void WriteProperty(XPathResult result, ref object value, IDictionaryAdapter dictionaryAdapter)
		{
			var propertyType = result.Type;
			var shouldRemove = (value == null);

			if (result.Property != null)
			{
				shouldRemove = shouldRemove || dictionaryAdapter.ShouldClearProperty(result.Property, value);
			}

			if (shouldRemove)
			{
				result.Remove(true);
				value = null;
				return;
			}

			if (WriteCustom(result, value, dictionaryAdapter))
			{
				return;
			}

			if (propertyType == typeof(string))
			{
				WriteSimple(result, value, dictionaryAdapter);
			}
			else if (typeof(IXPathNavigable).IsAssignableFrom(propertyType))
			{
				WriteFragment(result, (IXPathNavigable)value);
			}
			else if (propertyType.IsArray || typeof(IEnumerable).IsAssignableFrom(propertyType))
			{
				WriteCollection(result, ref value, dictionaryAdapter);
			}
			else if (propertyType.IsInterface)
			{
				WriteComponent(result, ref value, dictionaryAdapter);
			}
			else
			{
				WriteSimple(result, value, dictionaryAdapter);
			}
		}
		private void WriteCollection(XPathResult result, ref object value, IDictionaryAdapter dictionaryAdapter)
		{
			result.Remove(value == null);

			if (value != null)
			{
				if (result.Type.IsArray)
				{
					WriteArray(result, value, dictionaryAdapter);
				}
				else if (result.Type.IsGenericType)
				{
					WriteList(result, value, dictionaryAdapter);
				}
				if (result.Property != null)
				{
					value = ((IDictionaryPropertyGetter)this).GetPropertyValue(dictionaryAdapter, result.Key, null, result.Property, false);
				}
			}
		}
Exemple #5
0
		private void WriteCollection(XPathResult result, object value, IDictionaryAdapter dictionaryAdapter)
		{
			result.Remove();

			if (value != null)
			{
				if (result.Type.IsArray)
				{
					WriteArray(result, value, dictionaryAdapter);
				}
				else if (result.Type.IsGenericType)
				{
					WriteList(result, value, dictionaryAdapter);
				}
			}
		}
Exemple #6
0
		private void WriteComponent(XPathResult result, object value, IDictionaryAdapter dictionaryAdapter)
		{
			if (result.Property != null)
			{
				dictionaryAdapter.This.ExtendedProperties.Remove(result.Property.PropertyName);
			}

			if (value == null)
			{
				result.Remove();
				return;
			}

			var source = value as IDictionaryAdapter;
			if (source != null)
			{
				var node = result.RemoveChildren();
				if (result.Type != source.Meta.Type && result.OmitPolymorphism == false)
				{
					var xmlType = source.GetXmlMeta().XmlType;
					Context.SetXmlType(xmlType.TypeName, xmlType.Namespace, node);
				}
				var element = (IDictionaryAdapter)ReadComponent(result, false, dictionaryAdapter);
				source.CopyTo(element);
			}
		}
Exemple #7
0
		private void WriteSimple(XPathResult result, object value, IDictionaryAdapter dictionaryAdapter)
		{
			if (value == null)
			{
				result.Remove();
				if (result.Property != null)
					dictionaryAdapter.This.ExtendedProperties.Remove(result.Property.PropertyName);
				return;
			}

			var node = result.GetNavigator(true);

			if (result.Type.IsEnum || result.Type == typeof(Guid))
			{
				node.SetTypedValue(value.ToString());
			}
			else
			{
				try
				{
					node.SetTypedValue(value);
				}
				catch (InvalidCastException)
				{
					if (DefaultXmlSerializer.Instance.WriteObject(result, node, value) && result.Property != null)
					{
						dictionaryAdapter.This.ExtendedProperties[result.Property.PropertyName] = value;
					}
				}
			}
		}
Exemple #8
0
		private void WritePrimitive(XPathResult result, object value)
		{
			if (value == null)
			{
				result.Remove();
			}
			else if (result.Type.IsEnum)
			{
				result.GetNavigator(true).SetTypedValue(value.ToString());
			}
			else
			{
				try
				{
					result.GetNavigator(true).SetTypedValue(value);
				}
				catch (InvalidCastException)
				{
					var converter = TypeDescriptor.GetConverter(result.Type);
					if (converter != null && converter.CanConvertTo(typeof(string)))
					{
						value = converter.ConvertToString(value);
						result.GetNavigator(true).SetTypedValue(value);
					}
				}
			}
		}