Esempio n. 1
0
        public IMappingOperation[] GetMappingOperations(Type from, Type to)
        {
            var members = ReflectionUtils.GetPublicFieldsAndProperties(to);

            return(members
                   .Select(m => new DestWriteOperation()
            {
                Destination = new MemberDescriptor(m),
                Getter =
                    (ValueGetter <object>)
                    (
                        (form, status) =>
                {
                    FormCollection forms = new FormCollection((NameValueCollection)form);
                    IValueProvider valueProvider = forms.ToValueProvider();
                    ValueProviderResult res = valueProvider.GetValue(m.Name);
                    if (res != null)
                    {
                        return ValueToWrite <object> .ReturnValue(res.ConvertTo(ReflectionUtils.GetMemberType(m)));
                    }
                    else
                    {
                        return ValueToWrite <object> .Skip();
                    }
                }
                    )
            }
                           )
                   .ToArray());
        }
Esempio n. 2
0
        public IMappingOperation[] GetMappingOperations(Type from, Type to)
        {
            var members = ReflectionUtils.GetPublicFieldsAndProperties(to);

            return(members
                   .Select(
                       m =>
                       new DestWriteOperation()
            {
                Destination = new MemberDescriptor(m),
                Getter =
                    (ValueGetter <object>)
                    (
                        (form, valueProviderObj) =>
                {
                    var valueProvider = valueProviderObj as Func <string, object>;
                    if (valueProvider == null)
                    {
                        valueProvider = (k) => ((NameValueCollection)form)[k];
                    }
                    var val = valueProvider(m.Name);
                    if (val != null)
                    {
                        return ValueToWrite <object> .ReturnValue(val);
                    }
                    else
                    {
                        return ValueToWrite <object> .Skip();
                    }
                }
                    )
            }
                       )
                   .ToArray());
        }
Esempio n. 3
0
    /// <summary>
    ///   Gets the mapping operations.
    /// </summary>
    /// <param name="from">The type from.</param>
    /// <param name="to">To type to.</param>
    /// <returns>The mapping operations.</returns>
    public override IEnumerable <IMappingOperation> GetMappingOperations(Type from, Type to)
    {
        return(FilterOperations(
                   from,
                   to,
                   ReflectionHelper.GetTypeDataContainerDescription(to).Select(
                       fieldsDescription =>
        {
            var fieldName = fieldsDescription.Key;
            var destinationMember = fieldsDescription.Value.Item1;
            var fieldType = fieldsDescription.Value.Item2;

            return new DestWriteOperation
            {
                Destination = new MemberDescriptor(destinationMember),
                Getter = (ValueGetter <object>)((item, state) =>
                {
                    if (item is not DataContainer container)
                    {
                        return ValueToWrite <object> .Skip();
                    }

                    if (container.Fields == null ||
                        !container.Fields.TryGetValue(
                            fieldName,
                            out var value))
                    {
                        return ValueToWrite <object> .Skip();
                    }

                    var destinationType =
                        ReflectionHelper.GetMemberReturnType(destinationMember);

                    var destinationMemberValue = ReflectionHelper.ConvertValue(
                        value,
                        fieldType,
                        destinationType);

                    return ValueToWrite <object> .ReturnValue(
                        destinationMemberValue);
                })
            };
        })));
    }
    /// <summary>
    ///   Gets the mapping operations.
    /// </summary>
    /// <param name="from">The type from.</param>
    /// <param name="to">To type to.</param>
    /// <returns>The mapping operations.</returns>
    public override IEnumerable <IMappingOperation> GetMappingOperations(Type from, Type to)
    {
        return(FilterOperations(
                   from,
                   to,
                   ReflectionHelper.GetPublicFieldsAndProperties(to)
                   .Where(
                       member => (member.MemberType == MemberTypes.Field || member.MemberType == MemberTypes.Property) &&
                       ((PropertyInfo)member).GetSetMethod() != null).Select(
                       destinationMember => (IMappingOperation) new DestWriteOperation
        {
            Destination = new MemberDescriptor(destinationMember),
            Getter = (ValueGetter <object>)((item, state) =>
            {
                if (item is not DataContainer value)
                {
                    return ValueToWrite <object> .Skip();
                }

                var destinationType =
                    ReflectionHelper.GetMemberReturnType(
                        destinationMember);

                var fieldDescription =
                    ReflectionHelper.GetDataMemberDefinition(
                        destinationMember);

                var destinationMemberValue =
                    ConvertFieldToDestinationProperty(
                        value,
                        destinationType,
                        fieldDescription.FirstOrDefault());

                return destinationMemberValue == null
                ? ValueToWrite <object> .Skip()
                : ValueToWrite <object> .ReturnValue(
                    destinationMemberValue);
            })
        })));
Esempio n. 5
0
        /// <summary>
        /// Gets the mapping operations.
        /// </summary>
        /// <param name="from">The type from.</param>
        /// <param name="to">To type to.</param>
        /// <returns>The mapping operations.</returns>
        public override IMappingOperation[] GetMappingOperations(Type from, Type to)
        {
            return(this.FilterOperations(from, to, EmitMapper.Utils.ReflectionUtils.GetPublicFieldsAndProperties(to)
                                         .Where(member => (member.MemberType == MemberTypes.Field || member.MemberType == MemberTypes.Property) && ((PropertyInfo)member).GetSetMethod() != null)
                                         .Select(destinationMember => (IMappingOperation) new DestWriteOperation
            {
                Destination = new MemberDescriptor(destinationMember),
                Getter = (ValueGetter <object>)((item, state) =>
                {
                    if (item == null || !(item is DataContainer))
                    {
                        return ValueToWrite <object> .Skip();
                    }
                    var destinationType = EmitMapper.Utils.ReflectionUtils.GetMemberType(destinationMember);

                    var fieldDescription = ReflectionUtils.GetDataMemberDefinition(destinationMember);
                    var destinationMemberValue = ConvertFieldToDestinationProperty((DataContainer)item, destinationType, fieldDescription.FirstOrDefault());

                    return destinationMemberValue == null ? ValueToWrite <object> .Skip() : ValueToWrite <object> .ReturnValue(destinationMemberValue);
                })
            })).ToArray());
        }
    /// <summary>
    ///   Gets the mapping operations.
    /// </summary>
    /// <param name="from">The from.</param>
    /// <param name="to">The to.</param>
    /// <returns><![CDATA[IEnumerable<IMappingOperation>]]></returns>
    public override IEnumerable <IMappingOperation> GetMappingOperations(Type from, Type to)
    {
        var members = ReflectionHelper.GetPublicFieldsAndProperties(to);

        return(members.Select(
                   m => (IMappingOperation) new DestWriteOperation
        {
            Destination = new MemberDescriptor(m),
            Getter = (ValueGetter <object>)((form, valueProviderObj) =>
            {
                if (((FormCollection)form).TryGetValue(
                        m.Name,
                        out var res))
                {
                    return ValueToWrite <object> .ReturnValue(
                        Convert(
                            new ValueProviderResult(res),
                            ReflectionHelper.GetMemberReturnType(m)));
                }

                return ValueToWrite <object> .Skip();
            })
        }).ToArray());
Esempio n. 7
0
        public IMappingOperation[] GetMappingOperations(Type from, Type to)
        {
            var members = ReflectionUtils.GetPublicFieldsAndProperties(to);

            return(members
                   .Select(
                       m =>
                       new DestWriteOperation()
            {
                Destination = new MemberDescriptor(m),
                Getter =
                    (ValueGetter <object>)
                    (
                        (form, valueProviderObj) =>
                {
                    var valueProvider = valueProviderObj as IValueProvider;
                    if (valueProvider == null)
                    {
                        valueProvider = ((FormCollection)form).ToValueProvider();
                    }

                    ValueProviderResult res = valueProvider.GetValue(m.Name);
                    // here need to check the value of res is not null, then can convert the value
                    if (res != null)
                    {
                        return ValueToWrite <object> .ReturnValue(res.ConvertTo(ReflectionUtils.GetMemberType(m)));
                    }
                    else
                    {
                        return ValueToWrite <object> .Skip();
                    }
                }
                    )
            }
                       )
                   .ToArray());
        }
Esempio n. 8
0
        public IMappingOperation[] GetMappingOperations(Type from, Type to)
        {
            var members = ReflectionUtils.GetPublicFieldsAndProperties(to);

            return(members
                   .Select(
                       m =>
                       new DestWriteOperation()
            {
                Destination = new MemberDescriptor(m),
                Getter =
                    (ValueGetter <object>)
                    (
                        (form, valueProviderObj) =>
                {
                    var valueProvider = valueProviderObj as IDictionary <string, ValueProviderResult>;
                    if (valueProvider == null)
                    {
                        valueProvider = ((FormCollection)form).ToValueProvider();
                    }

                    ValueProviderResult res;
                    if (valueProvider.TryGetValue(m.Name, out res))
                    {
                        return ValueToWrite <object> .ReturnValue(res.ConvertTo(ReflectionUtils.GetMemberType(m)));
                    }
                    else
                    {
                        return ValueToWrite <object> .Skip();
                    }
                }
                    )
            }
                       )
                   .ToArray());
        }
        private ValueToWrite <object> GetReturnValue(dynamic value, MemberInfo destMember)
        {
            Type type = value.GetType();
            List <MemberInfo> _sourceMembers = GetSourceMemebers(type);

            var matchedChain = GetMatchedChain(destMember.Name, _sourceMembers);

            if (matchedChain == null || matchedChain.Count == 0)
            {
                return(ValueToWrite <object> .Skip());
            }
            if (matchedChain[0] is PropertyInfo)
            {
                var val = (matchedChain[0] as PropertyInfo).GetValue(value, null);
                return(ValueToWrite <object> .ReturnValue(val));
            }
            else if (matchedChain[0] is FieldInfo)
            {
                var val = (matchedChain[0] as FieldInfo).GetValue(value);
                return(ValueToWrite <object> .ReturnValue(val));
            }

            return(ValueToWrite <object> .Skip());
        }
Esempio n. 10
0
        /// <summary>
        ///     Gets the mapping operations.
        /// </summary>
        /// <param name="from">The type from.</param>
        /// <param name="to">To type to.</param>
        /// <returns>The mapping operations.</returns>
        public override IMappingOperation[] GetMappingOperations(Type from, Type to)
        {
            return(FilterOperations(from, to, ReflectionUtils.GetTypeDataContainerDescription(to)
                                    .Select(fieldsDescription =>
            {
                string fieldName = fieldsDescription.Key;
                MemberInfo destinationMember =
                    fieldsDescription.Value.Item1;
                Type fieldType =
                    fieldsDescription.Value.Item2;
                return new DestWriteOperation
                {
                    Destination =
                        new MemberDescriptor(
                            destinationMember),
                    Getter =
                        (ValueGetter <object>)
                            ((item, state) =>
                    {
                        if (item ==
                            null ||
                            !(item is
                              DataContainer))
                        {
                            return
                            ValueToWrite
                            <
                                object
                            >
                            .Skip
                                ();
                        }

                        var container =
                            item as
                            DataContainer;
                        string value;
                        if (
                            container
                            .Fields ==
                            null ||
                            !container
                            .Fields
                            .TryGetValue
                                (fieldName,
                                out
                                value))
                        {
                            return
                            ValueToWrite
                            <
                                object
                            >
                            .Skip
                                ();
                        }
                        Type
                        destinationType
                            =
                                EmitMapper
                                .Utils
                                .ReflectionUtils
                                .GetMemberType
                                    (destinationMember);
                        object
                        destinationMemberValue
                            =
                                ReflectionUtils
                                .ConvertValue
                                    (value,
                                    fieldType,
                                    destinationType);

                        return
                        ValueToWrite
                        <object
                        >
                        .ReturnValue
                            (destinationMemberValue);
                    })
                };
            })).ToArray());
        }