示例#1
0
        public void Using <TFactory>(TFactory factory) where TFactory : class
        {
            var factoryInfo = ConfiguredLambdaInfo.ForFunc(factory, typeof(TSource), typeof(TTarget));

            if (factoryInfo == null)
            {
                var contextTypeName = typeof(IMappingData <TSource, TTarget>).GetFriendlyName();
                var sourceTypeName  = typeof(TSource).GetFriendlyName();
                var targetTypeName  = typeof(TTarget).GetFriendlyName();
                var objectTypeName  = typeof(TObject).GetFriendlyName();

                string[] validSignatures =
                {
                    "Func<" + objectTypeName + ">",
                    $"Func<{contextTypeName}, {objectTypeName}>",
                    $"Func<{sourceTypeName}, {targetTypeName}, {objectTypeName}>",
                    $"Func<{sourceTypeName}, {targetTypeName}, int?, {objectTypeName}>"
                };

                throw new MappingConfigurationException(string.Format(
                                                            CultureInfo.InvariantCulture,
                                                            "Unable to create objects of type {0} using factory {1}: valid function signatures are {2}",
                                                            objectTypeName,
                                                            factory,
                                                            string.Join(", ", validSignatures)));
            }

            var objectFactory = ConfiguredObjectFactory.For(_configInfo, typeof(TObject), factoryInfo);

            _configInfo.MapperContext.UserConfigurations.Add(objectFactory);
        }
示例#2
0
        public CustomDataSourceTargetMemberSpecifier <TSource, TTarget> Map <TSourceValue>(TSourceValue value)
        {
            var valueLambdaInfo = ConfiguredLambdaInfo.ForFunc(value, typeof(TSource), typeof(TTarget));

            return((valueLambdaInfo != null)
                ? new CustomDataSourceTargetMemberSpecifier <TSource, TTarget>(
                       _configInfo.ForSourceValueType(valueLambdaInfo.ReturnType),
                       valueLambdaInfo)
                : GetConstantValueTargetMemberSpecifier(value));
        }
示例#3
0
        private CustomDataSourceTargetMemberSpecifier <TSource, TTarget> GetConstantValueTargetMemberSpecifier <TSourceValue>(
            TSourceValue value)
        {
            var valueLambdaInfo = ConfiguredLambdaInfo.ForFunc(value, typeof(TSource), typeof(TTarget));

            if (valueLambdaInfo != null)
            {
                return(new CustomDataSourceTargetMemberSpecifier <TSource, TTarget>(
                           ConfigInfo.ForSourceValueType(valueLambdaInfo.ReturnType),
                           valueLambdaInfo));
            }
#if NET35
            var valueConstant = Expression.Constant(value, typeof(TSourceValue));
#else
            var valueConstant = value.ToConstantExpression();
#endif
            var valueLambda = Expression.Lambda <Func <TSourceValue> >(valueConstant);

            return(new CustomDataSourceTargetMemberSpecifier <TSource, TTarget>(
                       ConfigInfo.ForSourceValueType(valueConstant.Type),
                       valueLambda));
        }