Esempio n. 1
0
        private bool MapObject(object source, object target, MappingContext context, MappingConfigurationEntry baseEntry)
        {
            var entry = baseEntry as MappingConfigurationObjectEntry;

            if (entry == null)
            {
                return(false);
            }

            var sourceObject = entry.GetSourceProperty.DynamicInvoke(source);
            var targetObject = entry.GetTargetProperty.DynamicInvoke(target);

            if (targetObject == null)
            {
                var newObject = Map(sourceObject, entry.TargetPropertyType, context);
                entry.SetTargetProperty.DynamicInvoke(target, newObject);
            }
            else
            {
                Map(sourceObject, targetObject, context);
            }

            return(true);
        }
Esempio n. 2
0
        private bool MapProperty(object source, object target, MappingContext context, MappingConfigurationEntry baseEntry)
        {
            var entry = baseEntry as MappingConfigurationPropertyEntry;

            if (entry == null)
            {
                return(false);
            }

            var dependencyTuple = GetDependencyTuple(context, entry.DependencyTupleType, entry.NamedResolutions);

            if (dependencyTuple != null)
            {
                entry.MappingAction.DynamicInvoke(source, target, dependencyTuple);
            }
            else
            {
                entry.MappingAction.DynamicInvoke(source, target);
            }

            return(true);
        }
Esempio n. 3
0
        private bool MapCollection(object source, object target, MappingContext context, MappingConfigurationEntry baseEntry)
        {
            var entry = baseEntry as MappingConfigurationCollectionEntry;

            if (entry == null)
            {
                return(false);
            }

            var sourceEnumerable   = (IEnumerable)entry.GetSourceProperty.DynamicInvoke(source);
            var targetCollection   = entry.GetTargetProperty.DynamicInvoke(target);
            var sourceObjects      = sourceEnumerable == null ? new object[0] : sourceEnumerable.Cast <object>().ToArray();
            var collectionItemType = MappingHelper.GetCollectionItemType(entry.TargetPropertyType);
            var mappedObjects      = sourceObjects.Select(x => Map(x, collectionItemType, context)).ToArray();

            if (targetCollection == null)
            {
                if (entry.TargetPropertyType.IsArray)
                {
                    var array = Array.CreateInstance(collectionItemType, mappedObjects.Length);
                    Array.Copy(mappedObjects, array, mappedObjects.Length);
                    entry.SetTargetProperty.DynamicInvoke(target, array);
                }
                else
                {
                    var nonAbstractType = entry.TargetPropertyType;
                    if (nonAbstractType.IsInterface)
                    {
                        nonAbstractType = typeof(List <>).MakeGenericType(collectionItemType);
                    }

                    var list = Activator.CreateInstance(nonAbstractType) as IList;
                    if (list == null)
                    {
                        throw new MappingException(string.Format("Type {0} does not implement IList", entry.TargetPropertyType));
                    }
                    foreach (var obj in mappedObjects)
                    {
                        list.Add(obj);
                    }
                    entry.SetTargetProperty.DynamicInvoke(target, list);
                }
            }
            else
            {
                if (entry.TargetPropertyType.IsArray)
                {
                    throw new MappingException("Cannot map collection to initialized array.");
                }
                var list = targetCollection as IList;
                if (list == null)
                {
                    throw new MappingException("Cannot map collection. Target does not implement IList");
                }
                if (list.IsReadOnly)
                {
                    throw new MappingException("Cannot map collection to readonly IList");
                }
                list.Clear();
                foreach (var obj in mappedObjects)
                {
                    list.Add(obj);
                }
            }

            return(true);
        }