Example #1
0
        /// <summary>
        /// Set value to a property using property path
        /// </summary>
        /// <typeparam name="TObject"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="object"></param>
        /// <param name="propertyPath"></param>
        /// <param name="value"></param>
        public static void SetPropertyValue <TObject, TValue>(this TObject @object, string propertyPath, TValue value)
        {
            if (string.IsNullOrWhiteSpace(propertyPath))
            {
                throw new ArgumentException(nameof(propertyPath));
            }
            string[] pathParts = propertyPath.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
            object   parent    = @object;

            for (int i = 0; i < pathParts.Length; i++)
            {
                string pathPart = pathParts[i];
                if (parent == null)
                {
                    break;
                }
                Type         parentType = parent.GetType();
                PropertyInfo prop       = parentType.GetProperty(pathPart);
                if (prop == null)
                {
                    break;
                }
                if (i + 1 >= pathParts.Length)
                {
                    MappingExpress.SetValue(parent, prop, value);
                    break;
                }
                parent = prop.GetValue(parent, null);
            }
        }
Example #2
0
 /// <summary>
 /// Set value to a property using lamda expression
 /// </summary>
 /// <typeparam name="TObject"></typeparam>
 /// <typeparam name="TValue"></typeparam>
 /// <param name="object"></param>
 /// <param name="expression"></param>
 /// <param name="value"></param>
 /// Source: http://stackoverflow.com/questions/9601707/how-to-set-property-value-using-expressions
 public static void SetPropertyValue <TObject, TValue>(this TObject @object, Expression <Func <TObject, TValue> > expression, TValue value)
 {
     if (expression.Body.NodeType == ExpressionType.Convert)
     {
         UnaryExpression unaryExpression = expression.Body as UnaryExpression;
         if (unaryExpression != null)
         {
             MemberExpression memberSelectorExpression = unaryExpression.Operand as MemberExpression;
             if (memberSelectorExpression != null)
             {
                 PropertyInfo property = memberSelectorExpression.Member as PropertyInfo;
                 if (property != null)
                 {
                     MappingExpress.SetValue(@object, property, value);
                 }
             }
         }
     }
     else
     {
         MemberExpression memberSelectorExpression = expression.Body as MemberExpression;
         if (memberSelectorExpression != null)
         {
             @object.SetPropertyValue(memberSelectorExpression, value);
         }
     }
 }
Example #3
0
        private static void SetPropertyValue <TObject, TValue>(this TObject @object, MemberExpression expression, TValue value)
        {
            IList <Expression> hierarchy    = BuildExpressionHierarchy(expression);
            object             parentObject = @object;

            foreach (Expression subExpression in hierarchy)
            {
                if (subExpression.NodeType == ExpressionType.MemberAccess)
                {
                    PropertyInfo propertyInfo = ((MemberExpression)subExpression).Member as PropertyInfo;
                    if (propertyInfo == null || parentObject == null)
                    {
                        break;
                    }
                    if (subExpression == expression)
                    {
                        MappingExpress.SetValue(@object, propertyInfo, value);
                        continue;
                    }
                    parentObject = propertyInfo.GetValue(parentObject);
                    if (parentObject == null)
                    {
                        parentObject = InitObject(propertyInfo.PropertyType);
                    }
                }
                if (subExpression.NodeType == ExpressionType.Call)
                {
                    MethodInfo methodInfo = ((MethodCallExpression)subExpression).Method;
                    if (methodInfo.ReturnType == typeof(void))
                    {
                        break;
                    }
                    object[] arguments = GetExpressionArguments((MethodCallExpression)subExpression);
                    parentObject = methodInfo.Invoke(@parentObject, arguments);
                }
            }
        }
Example #4
0
        private static object MapClass(object source, Type sourceType, Type resultType, IObjectMappingResolver customMappings, ObjectMappingConflictResolution conflictResolution)
        {
            if (sourceType.IsCollection())
            {
                if (conflictResolution == ObjectMappingConflictResolution.Exception)
                {
                    throw new ClassMappingException(sourceType, resultType);
                }
                return(null);
            }

            object result = Activator.CreateInstance(resultType);

            PropertyInfo[] sourceProperties = sourceType.GetProperties();
            PropertyInfo[] resultProperties = resultType.GetProperties();

            IClassMappingResolver mappingConfig = customMappings.GetConfiuration(sourceType, resultType);

            foreach (PropertyInfo sourceProperty in sourceProperties)
            {
                string targetName = sourceProperty.Name;
                if (mappingConfig != null)
                {
                    string remappedName = mappingConfig.GetTargetName(sourceProperty.Name);
                    if (remappedName != null)
                    {
                        targetName = remappedName;
                    }
                }
                PropertyInfo resultProperty = resultProperties.FirstOrDefault(x => { return(x.Name == targetName); });
                if (resultProperty == null)
                {
                    continue;
                }
                if (!sourceProperty.CanRead)
                {
                    continue;
                }
                if (!resultProperty.CanWrite)
                {
                    continue;
                }

                object value = sourceProperty.GetValue(source, null);
                if (MappingExpress.CanSetValue(sourceProperty, resultProperty))
                {
                    MappingExpress.SetValue(result, resultProperty, value);
                }
                else
                {
                    Type type = sourceProperty.PropertyType;
                    if (type.IsInterface || type.IsClass)
                    {
                        MapProperty(value, result, sourceProperty, resultProperty, customMappings, conflictResolution);
                    }
                    else if (conflictResolution == ObjectMappingConflictResolution.Exception)
                    {
                        throw new PropertyMappingException(sourceProperty, resultProperty);
                    }
                }
            }
            return(result);
        }