private object GetDestValue(object src, object dest, IDescribeMappableProperty destinationProperty)
        {
            var collectionSpec = new SupportedCollectionTypeSpecification();

            if (collectionSpec.IsSatisfiedBy(src) == false)
            {
                if (collectionSpec.IsElement(destinationProperty))
                {
                    return(activate.CreateInstance(destinationProperty.PropertyType));
                }
                return(invoke.GetValue(destinationProperty.Name, dest) ?? activate.CreateInstance(destinationProperty.PropertyType));
            }
            return(activate.CreateCollectionInstance(destinationProperty.PropertyType, collectionSpec.GetLength(src)));
        }
Beispiel #2
0
        public Result TryResolve(IResolutionContext context, IDescribeMappableProperty destinationProperty)
        {
            var collectionContext = context.Nested(sourceProperty, destinationProperty);
            var src  = (IList)context.GetSourcePropertyValue(sourceProperty.Name);
            var dest = (IList)activate.CreateCollectionInstance(destinationProperty.PropertyType, src.Count);

            for (int i = 0; i < src.Count; i++)
            {
                var sourceElement  = sourceProperty.ElementAt(i);
                var elementContext = collectionContext.Nested(sourceElement, destinationProperty.ElementAt(i));
                var nestedExecutor = this.executor.CreateExecutableMapping(sourceElement.PropertyType);
                nestedExecutor.Execute(elementContext);
                dest.AddElement(elementContext.Destination, i);
            }

            return(new Result(true, dest));
        }
        private ConversionResult TryListConversion(ConversionContext context)
        {
            var collectionTypeSpec = new SupportedCollectionTypeSpecification();

            if (collectionTypeSpec.IsSatisfiedBy(context.DestinationPropertyType) == false || collectionTypeSpec.IsSatisfiedBy(context.Value) == false)
            {
                return(context.Unconverted());
            }

            var destColl    = (IList)activate.CreateCollectionInstance(context.DestinationPropertyType, collectionTypeSpec.GetLength(context.Value));
            var destElement = context.DestinationPropertyType.ElementType();
            var srcList     = (IEnumerable)context.Value;
            var enumerator  = srcList.GetEnumerator();
            int index       = 0;

            while (enumerator.MoveNext())
            {
                object element = enumerator.Current ?? destElement.DefaultValue();
                destColl.AddElement(element, index++);
            }
            return(context.Result(destColl));
        }