public ResolutionResult Map(ResolutionContext context)
        {
            ResolutionResult result = new ResolutionResult(context);

            result.MemberBindings.AddRange(this.AllResolvers[0].resolve(context).MemberBindings);
            return(result);
        }
        public ResolutionResult resolve(ResolutionContext context)
        {
            ResolutionResult result = new ResolutionResult(context);

            List<MemberBinding> memberBindings = new List<MemberBinding>();

            var targetProperties = from property in context.TargetType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                                   where property.CanWrite
                                   select property;

            foreach (PropertyInfo targetProperty in targetProperties)
            {
                MappingAttribute attribute = targetProperty.GetAttribute();
                if ((attribute == null) || !attribute.Ignored)
                {
                    string columnName = attribute == null
                        ? targetProperty.Name
                        : attribute.Name;

                    // Create expression representing r.CellValue<property.PropertyType>(property.Name)
                    MethodCallExpression propertyValueExpr = Expression.Call(
                        typeof(DataRowExtensions).GetMethod("CellValue").MakeGenericMethod(targetProperty.PropertyType),
                        context.Parameter, Expression.Constant(columnName));

                    MemberBinding memberBinding = Expression.Bind(targetProperty, propertyValueExpr);
                    memberBindings.Add(memberBinding);
                }
            }
            result.MemberBindings = memberBindings;
            return result;
        }
Esempio n. 3
0
        public ResolutionResult resolve(ResolutionContext context)
        {
            ResolutionResult result = new ResolutionResult(context);

            List <MemberBinding> memberBindings = new List <MemberBinding>();

            var targetProperties = from property in context.TargetType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                                   where property.CanWrite
                                   select property;

            foreach (PropertyInfo targetProperty in targetProperties)
            {
                MappingAttribute attribute = targetProperty.GetAttribute();
                if ((attribute == null) || !attribute.Ignored)
                {
                    string fieldName = attribute == null
                        ? targetProperty.Name
                        : attribute.Name;

                    // Create expression representing r.Field<property.PropertyType>(property.Name)
                    MethodCallExpression propertyValueExpr = Expression.Call(
                        typeof(DataRecordExtensions).GetMethod("Field").MakeGenericMethod(targetProperty.PropertyType),
                        context.Parameter, Expression.Constant(fieldName));

                    MemberBinding memberBinding = Expression.Bind(targetProperty, propertyValueExpr);
                    memberBindings.Add(memberBinding);
                }
            }
            result.MemberBindings = memberBindings;
            return(result);
        }
 public ResolutionResult Map(ResolutionContext context)
 {
     ResolutionResult result = new ResolutionResult(context);
     foreach (var resolver in this.AllResolvers)
     {
         result.MemberBindings.AddRange(resolver.resolve(context).MemberBindings);
     }
     return result;
 }
        public ResolutionResult Map(ResolutionContext context)
        {
            ResolutionResult result = new ResolutionResult(context);

            foreach (var resolver in this.AllResolvers)
            {
                result.MemberBindings.AddRange(resolver.resolve(context).MemberBindings);
            }
            return(result);
        }
Esempio n. 6
0
        private static Func <TSource, TTarget> BuildMapper()
        {
            Type sourceType = typeof(TSource);
            Type targetType = typeof(TTarget);
            ParameterExpression sourceParameter = Expression.Parameter(sourceType, "source");
            IMappingRunner      runner          = DefaultConfigurationProvider.Current.FindRunner(sourceType);
            ResolutionContext   context         = new ResolutionContext(sourceType, targetType, sourceParameter);
            ResolutionResult    result          = runner.Map(context);

            return(Expression.Lambda <Func <TSource, TTarget> >(Expression.MemberInit(Expression.New(targetType), result.MemberBindings), new ParameterExpression[] { sourceParameter }).Compile());
        }
        public ResolutionResult resolve(ResolutionContext context)
        {
            ResolutionResult result = new ResolutionResult(context);

            List<MemberBinding> memberBindings = new List<MemberBinding>();

            var targetProperties = from property in context.TargetType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                                   where property.CanWrite
                                   select property;

            foreach (PropertyInfo targetProperty in targetProperties)
            {
                MappingAttribute attribute = targetProperty.GetAttribute();
                if ((attribute == null) || !attribute.Ignored)
                {
                    PropertyInfo sourceProperty = context.SourceType.GetProperty(targetProperty.Name);
                    if (sourceProperty == null)
                    {
                        if (attribute != null)
                        {
                            sourceProperty = context.SourceType.GetProperty(attribute.Name);
                        }
                        if (sourceProperty == null)
                        {
                            continue;
                        }
                    }
                    if (sourceProperty.CanRead)
                    {
                        MemberBinding memberBinding;
                        if (!targetProperty.PropertyType.IsAssignableFrom(sourceProperty.PropertyType))
                        {
                            memberBinding = Expression.Bind(targetProperty, Expression.Convert(Expression.Property(context.Parameter, sourceProperty), targetProperty.PropertyType));
                        }
                        else
                        {
                            memberBinding = Expression.Bind(targetProperty, Expression.Property(context.Parameter, sourceProperty));
                        }

                        memberBindings.Add(memberBinding);
                    }
                }
            }
            result.MemberBindings = memberBindings;
            return result;
        }
        public ResolutionResult resolve(ResolutionContext context)
        {
            ResolutionResult result = new ResolutionResult(context);

            List <MemberBinding> memberBindings = new List <MemberBinding>();

            var targetProperties = from property in context.TargetType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                                   where property.CanWrite
                                   select property;

            foreach (PropertyInfo targetProperty in targetProperties)
            {
                MappingAttribute attribute = targetProperty.GetAttribute();
                if ((attribute == null) || !attribute.Ignored)
                {
                    PropertyInfo sourceProperty = context.SourceType.GetProperty(targetProperty.Name);
                    if (sourceProperty == null)
                    {
                        if (attribute != null)
                        {
                            sourceProperty = context.SourceType.GetProperty(attribute.Name);
                        }
                        if (sourceProperty == null)
                        {
                            continue;
                        }
                    }
                    if (sourceProperty.CanRead)
                    {
                        MemberBinding memberBinding;
                        if (!targetProperty.PropertyType.IsAssignableFrom(sourceProperty.PropertyType))
                        {
                            memberBinding = Expression.Bind(targetProperty, Expression.Convert(Expression.Property(context.Parameter, sourceProperty), targetProperty.PropertyType));
                        }
                        else
                        {
                            memberBinding = Expression.Bind(targetProperty, Expression.Property(context.Parameter, sourceProperty));
                        }

                        memberBindings.Add(memberBinding);
                    }
                }
            }
            result.MemberBindings = memberBindings;
            return(result);
        }
Esempio n. 9
0
        public ResolutionResult resolve(ResolutionContext context)
        {
            ResolutionResult     fieldResult = new ResolutionResult(context);
            List <MemberBinding> bindings    = new List <MemberBinding>();

            foreach (FieldInfo targetField in from field in context.TargetType.GetFields(BindingFlags.Public | BindingFlags.Instance) select field)
            {
                MappingAttribute attribute = targetField.GetAttribute();
                if ((attribute == null) || !attribute.Ignored)
                {
                    MemberBinding memberBinding;
                    FieldInfo     sourceField = context.SourceType.GetField(targetField.Name);
                    if (sourceField == null)
                    {
                        if (attribute != null)
                        {
                            sourceField = context.SourceType.GetField(attribute.Name);
                        }
                        if (sourceField == null)
                        {
                            continue;
                        }
                    }
                    if (!targetField.FieldType.IsAssignableFrom(sourceField.FieldType))
                    {
                        memberBinding = Expression.Bind(targetField, Expression.Convert(Expression.Field(context.Parameter, sourceField), targetField.FieldType));
                    }
                    else
                    {
                        memberBinding = Expression.Bind(targetField, Expression.Field(context.Parameter, sourceField));
                    }
                    bindings.Add(memberBinding);
                }
            }
            fieldResult.MemberBindings = bindings;
            return(fieldResult);
        }
 public ResolutionResult resolve(ResolutionContext context)
 {
     ResolutionResult fieldResult = new ResolutionResult(context);
     List<MemberBinding> bindings = new List<MemberBinding>();
     foreach (FieldInfo targetField in from field in context.TargetType.GetFields(BindingFlags.Public | BindingFlags.Instance) select field)
     {
         MappingAttribute attribute = targetField.GetAttribute();
         if ((attribute == null) || !attribute.Ignored)
         {
             MemberBinding memberBinding;
             FieldInfo sourceField = context.SourceType.GetField(targetField.Name);
             if (sourceField == null)
             {
                 if (attribute != null)
                 {
                     sourceField = context.SourceType.GetField(attribute.Name);
                 }
                 if (sourceField == null)
                 {
                     continue;
                 }
             }
             if (!targetField.FieldType.IsAssignableFrom(sourceField.FieldType))
             {
                 memberBinding = Expression.Bind(targetField, Expression.Convert(Expression.Field(context.Parameter, sourceField), targetField.FieldType));
             }
             else
             {
                 memberBinding = Expression.Bind(targetField, Expression.Field(context.Parameter, sourceField));
             }
             bindings.Add(memberBinding);
         }
     }
     fieldResult.MemberBindings = bindings;
     return fieldResult;
 }
 public ResolutionResult Map(ResolutionContext context)
 {
     ResolutionResult result = new ResolutionResult(context);
     result.MemberBindings.AddRange(this.AllResolvers[0].resolve(context).MemberBindings);
     return result;
 }