Exemple #1
0
        /// <summary>
        /// Resolves the mapping between source and destination type. This method only resolves mappings between
        /// public get properties in source type and public set properties on destination type. Possible indexer properties
        /// are ignored. To add additional mappings after initial resolve use <see cref="Map.Add"/> method.
        /// </summary>
        /// <param name="sourceType">A source type.</param>
        /// <param name="destinationType">A destination type.</param>
        /// <returns>A <see cref="Map"/> of resolved member mappings.</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="sourceType"/> or <paramref name="destinationType"/> is <c>null</c>.</exception>
        public static Map Resolve(Type sourceType, Type destinationType, MapResolveOptions options = MapResolveOptions.None)
        {
            if (sourceType == null)
            {
                throw new ArgumentNullException("sourceType");
            }

            if (destinationType == null)
            {
                throw new ArgumentNullException("destinationType");
            }

            string key = sourceType.FullName + ";" + destinationType.FullName;

            Map map;

            if (!maps.TryGetValue(key, out map))
            {
                lock (mutex)
                {
                    if (!maps.TryGetValue(key, out map))
                    {
                        map = new Map(sourceType, destinationType);

                        //// get properties in source type
                        var getProperties = GetProperties(sourceType, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty);

                        //// set properties in destination type
                        var setProperties = GetProperties(destinationType, BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty);

                        foreach (var getProperty in getProperties)
                        {
                            //// get set property matching by name
                            var setProperty = setProperties.Where(x => x.Name.Equals(getProperty.Name))
                                              .FirstOrDefault();

                            //// add mapping only if both properties match and
                            //// set can be written to.
                            if (setProperty != null && setProperty.CanWrite)
                            {
                                //// check if provite setter can be used
                                bool usePrivateSetter = options.HasFlag(MapResolveOptions.UsePrivateSetter);

                                if (!usePrivateSetter && !setProperty.GetSetMethod(true).IsPublic)
                                {
                                    continue;
                                }

                                //// add mapping
                                map.Add <PropertyInfo, PropertyInfo>(getProperty, setProperty);
                            }
                        }

                        maps.Add(key, map);
                    }
                }
            }

            return(map);
        }
Exemple #2
0
        /// <summary>
        /// Resolves the mapping between <typeparamref name="TSource"/> and <typeparamref name="TDestination"/>. This method only resolves mappings between
        /// public get properties in source type and public set properties on destination type. Possible indexer properties
        /// are ignored. To add additional mappings after initial resolve use <see cref="Map.Add"/> method.
        /// </summary>
        /// <typeparam name="TSource">The type of an source object.</typeparam>
        /// <typeparam name="TDestination">The type of an destination object.</typeparam>
        /// <returns>A <see cref="Map"/> of resolved member mappings.</returns>
        public static Map Resolve <TSource, TDestination>(MapResolveOptions options = MapResolveOptions.None) where TDestination : new()
        {
            Type sourceType      = typeof(TSource);
            Type destinationType = typeof(TDestination);

            return(Resolve(sourceType, destinationType, options));
        }
Exemple #3
0
 /// <summary>
 /// Resolves <see cref="IMap"/> between source and destination type.
 /// </summary>
 /// <returns>A resolved map.</returns>
 public static IMap Resolve(Type sourceType, Type destinationType, MapResolveOptions options = MapResolveOptions.None)
 {
     return(MapResolver.Resolve(sourceType, destinationType, options));
 }
Exemple #4
0
 /// <summary>
 /// Resolves <see cref="IMap"/> between source and destination type.
 /// </summary>
 /// <returns>A resolved map.</returns>
 public static IMap Resolve <TSource, TDestination>(MapResolveOptions options = MapResolveOptions.None) where TDestination : new()
 {
     return(MapResolver.Resolve <TSource, TDestination>(options));
 }