sharp-oodb/mapping/ListMapping.cs

62 lines
1.7 KiB
C#

using System;
using System.Xml;
using System.Collections;
using System.Collections.Generic;
namespace oodb.mapping
{
public class ListMapping : NaiveMapping
{
public FlatTypeMapping ElementMapping { get; }
public ListMapping(OODB oodb, Type type)
: base(oodb, type)
{
ElementMapping = OODB.GetFlatTypeMapping(type.GetGenericArguments()[0]);
}
public override object FromXml(XmlElement xmlValue)
{
if (!NativeType.FullName.Equals(xmlValue.GetAttribute("Type")))
throw new ArgumentException("Type != NativeType", nameof(xmlValue));
IList list = Activator.CreateInstance(typeof(List<>).MakeGenericType(ElementMapping.NativeType)) as IList;
foreach (XmlNode elementNode in xmlValue.ChildNodes)
{
if ((elementNode is XmlElement) && ((((XmlElement)elementNode).Name.Equals("Value"))))
{
list.Add(ElementMapping.FromXml(elementNode as XmlElement));
}
}
return list;
}
public override XmlElement ToXml(XmlDocument xmlDocument, object value)
{
XmlElement valueElement = CreateEmptyValueElement(xmlDocument);
IList list = value as IList;
for (int n = 0; n < list.Count; n++)
{
valueElement.AppendChild(ElementMapping.ToXml(xmlDocument, list[n]));
}
return valueElement;
}
public override string ToText(object value)
{
throw new NotImplementedException();
}
public override object FromText(string text)
{
throw new NotImplementedException();
}
}
}