// /** // * File: ListMapping.cs // * Author: haraldwolff // * // * This file and it's content is copyrighted by the Author and / or copyright holder. // * Any use wihtout proper permission is illegal and may lead to legal actions. // * // * // **/ using System; using System.Collections; using System.Linq; using System.Collections.Generic; using System.Reflection; using ln.objects.catalog; namespace ln.types.odb.ng.mappings { public class ListMapping : IODBMapping { public Type TargetType { get; } public ListMapping(Type targetType) { TargetType = targetType; } private Array UnmapArray(Mapper mapper, ODBList list) { Array array = Array.CreateInstance(TargetType.GetElementType(), list.Count); for (int n = 0; n < list.Count; n++) array.SetValue(mapper.UnmapValue(TargetType.GetElementType(), list[n]), n); return array; } private ODBList MapArray(Mapper mapper, Array array) { ODBList list = new ODBList(); for (int n = 0; n < array.Length; n++) { list.Add(mapper.MapValue(array.GetValue(n))); } return list; } public object UnmapList(Mapper mapper, ODBList list) { IList ilist = (IList)Activator.CreateInstance(TargetType, true); for (int n = 0; n < list.Count; n++) { ilist.Add(mapper.UnmapValue(TargetType.GetGenericArguments()[0], list[n])); } return ilist; } public ODBList MapList(Mapper mapper, object value) { ODBList list = new ODBList(); IList ilist = (IList)value; for (int n = 0; n < ilist.Count; n++) list.Add(mapper.MapValue(ilist[n])); return list; } public object UnmapSet(Mapper mapper, ODBList list) { Type entype = typeof(UnmappingEnumeration<>).MakeGenericType(TargetType.GetGenericArguments()); object en = Activator.CreateInstance(entype, mapper, list); return Activator.CreateInstance(TargetType, en ); } public ODBList MapSet(Mapper mapper, object value) { ODBList list = new ODBList(); IEnumerable ienum = (IEnumerable)value; foreach (object item in ienum) list.Add(mapper.MapValue(item)); return list; } public object UnmapValue(Mapper mapper, ODBEntity oval) { if (TargetType.IsArray) return UnmapArray(mapper, (ODBList)oval); if (TargetType.IsGenericType) { if (typeof(List<>).Equals(TargetType.GetGenericTypeDefinition())) { return UnmapList(mapper, (ODBList)oval); } else if (typeof(HashSet<>).Equals(TargetType.GetGenericTypeDefinition())) { return UnmapSet(mapper, (ODBList)oval); } } throw new NotImplementedException(); } public ODBEntity MapValue(Mapper mapper, object value) { if (TargetType.IsArray) return MapArray(mapper, (Array)value); if (TargetType.IsGenericType) { if (typeof(List<>).Equals(TargetType.GetGenericTypeDefinition())) { return MapList(mapper, value); } else if (typeof(HashSet<>).Equals(TargetType.GetGenericTypeDefinition())) { return MapSet(mapper, value); } } throw new NotImplementedException(); } class UnmappingEnumeration : IEnumerable { Type TargetType; Mapper mapper; ODBList list; public UnmappingEnumeration(Mapper mapper,ODBList list) { TargetType = typeof(T); this.mapper = mapper; this.list = list; } public IEnumerator GetEnumerator() { foreach (ODBEntity item in list) yield return (T)mapper.UnmapValue(TargetType, item); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } } }