コード例 #1
0
ファイル: MemberMapper.cs プロジェクト: JulianR/ThisMember
        public object Map(object source, object destination)
        {
            if (destination == null && this.Options.Safety.ThrowIfDestinationIsNull)
            {
                throw new ArgumentNullException("destination");
            }

            var pair = new TypePair(source.GetType(), destination.GetType());

            MemberMap map;

            if (!this.maps.TryGetValue(pair, out map))
            {
                map = MappingStrategy.CreateMapProposal(pair).FinalizeMap();
            }
            if (BeforeMapping != null)
            {
                BeforeMapping(this, pair, source);
            }

            var result = map.MappingFunction.DynamicInvoke(source, destination);

            if (AfterMapping != null)
            {
                AfterMapping(this, pair, result);
            }

            return(result);
        }
コード例 #2
0
ファイル: MemberMapper.cs プロジェクト: JulianR/ThisMember
        /// <summary>
        /// Creates an expression that returns TDestination as output from a given TSource (a 'projection').
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <typeparam name="TDestination"></typeparam>
        /// <returns></returns>
        public Expression <Func <TSource, TDestination> > Project <TSource, TDestination>()
        {
            var pair = new TypePair(typeof(TSource), typeof(TDestination));

            Projection projection;

            if (!this.projections.TryGetValue(pair, out projection))
            {
                projection = MappingStrategy.CreateMapProposal(pair).FinalizeProjection();
            }
            return((Expression <Func <TSource, TDestination> >)projection.Expression);
        }
コード例 #3
0
ファイル: MemberMapper.cs プロジェクト: JulianR/ThisMember
        public TDestination Map <TDestination>(object source) where TDestination : new()
        {
            if (source == null)
            {
                var option = this.Options.Safety.IfSourceIsNull;

                if (option == SourceObjectNullOptions.ReturnDestinationObject)
                {
                    return(new TDestination());
                }
                else if (option == SourceObjectNullOptions.ReturnNullWhenSourceIsNull)
                {
                    return(default(TDestination));
                }
            }

            var pair = new TypePair(source.GetType(), typeof(TDestination));

            MemberMap map;

            if (!this.maps.TryGetValue(pair, out map))
            {
                map = MappingStrategy.CreateMapProposal(pair).FinalizeMap();
            }

            var destination = new TDestination();

            if (BeforeMapping != null)
            {
                BeforeMapping(this, pair, source);
            }

            var result = (TDestination)map.MappingFunction.DynamicInvoke(source, destination);

            if (AfterMapping != null)
            {
                AfterMapping(this, pair, result);
            }

            return(result);
        }
コード例 #4
0
ファイル: MemberMapper.cs プロジェクト: JulianR/ThisMember
        public TDestination Map <TSource, TDestination>(TSource source, TDestination destination)
        {
            if (destination == null && this.Options.Safety.ThrowIfDestinationIsNull)
            {
                throw new ArgumentNullException("destination");
            }

            var pair = new TypePair(typeof(TSource), typeof(TDestination));

            MemberMap map;

            if (!this.maps.TryGetValue(pair, out map))
            {
                map = MappingStrategy.CreateMapProposal(pair).FinalizeMap();
            }

            if (BeforeMapping != null)
            {
                BeforeMapping(this, pair, source);
            }

            var func = map.MappingFunction as Func <TSource, TDestination, TDestination>;

            if (func == null)
            {
                throw new InvalidOperationException(string.Format("The mapping from {0} to {1} is not configured to be called without parameters. Use another overload of Map or recreate the map without a parameter.", pair.SourceType, pair.DestinationType));
            }

            var result = func(source, destination);

            if (AfterMapping != null)
            {
                AfterMapping(this, pair, result);
            }

            return(result);
        }