Пример #1
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;
		}
Пример #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];

			if (genericDef == typeof(List<>))
			{
				listType = typeof(EditableList<>).MakeGenericType(arguments);
			}
			else
			{
				listType = typeof(EditableBindingList<>).MakeGenericType(arguments);
				initializerType = typeof(BindingListInitializer<>).MakeGenericType(arguments);
			}
			
			var list = (IList)Activator.CreateInstance(listType);
			Func<Type, XmlMetadata> getXmlMeta = type => dictionaryAdapter.GetXmlMeta(type);

			foreach (var item in result.GetNodes(itemType, getXmlMeta))
			{
				list.Add(ReadProperty(item, false, dictionaryAdapter));
			}

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

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

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

				Action<int> removeAt = index => result.RemoveAt(index);

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

			return list;
		}