Пример #1
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 static MappingAttribute GetAttribute(this MemberInfo member)
        {
            MappingAttribute[] attributes = member.GetCustomAttributes <MappingAttribute>();
            MappingAttribute   attribute  = null;

            if (attributes.Length != 0)
            {
                attribute = attributes[0];
            }
            return(attribute);
        }
        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);
        }
Пример #4
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);
        }