public IClassMappingResolver GetConfiuration(Type sourceType, Type targetType)
        {
            ObjectMappingSignature mappingSignature = new ObjectMappingSignature();

            mappingSignature.SourceType = sourceType;
            mappingSignature.TargetType = targetType;
            if (_mappingDictionary.ContainsKey(mappingSignature))
            {
                IClassMappingResolver mappingConfig = _mappingDictionary[mappingSignature];
                return(mappingConfig);
            }
            return(null);
        }
Exemplo n.º 2
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);
        }