/// <summary>
        /// Tries to map the given instance into generic destination type using an specific key service.
        /// </summary>
        /// <typeparam name="TSource">The type of the source.</typeparam>
        /// <typeparam name="TDestination">The type of the destination.</typeparam>
        /// <param name="source">The source.</param>
        /// <param name="keyService">The key service.</param>
        /// <returns></returns>
        public TDestination TryToMap <TSource, TDestination>(TSource source, object keyService)
        {
            object src = source;

            if (src == null)
            {
                return(default(TDestination));
            }

            var serviceMapper =
                this.mapperResolver.FirstOrDefault(
                    transformer => transformer.Match(keyService, typeof(TSource), typeof(TDestination)));

            if (serviceMapper == null)
            {
                return(default(TDestination));
            }

            ISimpleMapper <TSource, TDestination> mapper = serviceMapper.ServiceAs <ISimpleMapper <TSource, TDestination> >();

            if (mapper != null)
            {
                return(mapper.Map(source));
            }

            ISourceMapper mp = serviceMapper.ServiceAs <ISourceMapper>();

            if (mp == null)
            {
                return(default(TDestination));
            }
            return((TDestination)mp.Map(source));
        }
Exemple #2
0
        public override ScriptCode CompileSourceCode(SourceUnit sourceUnit, CompilerOptions options, ErrorSink errorSink)
        {
            using (var reader = sourceUnit.GetReader()) {
                try
                {
                    switch (sourceUnit.Kind)
                    {
                    case SourceCodeKind.SingleStatement:
                    case SourceCodeKind.Expression:
                    case SourceCodeKind.AutoDetect:
                    case SourceCodeKind.InteractiveCode:
                        return(new VBScriptCode(
                                   _vbscript, _vbscript.ParseExprToLambda(reader),
                                   sourceUnit));

                    case SourceCodeKind.Statements:
                    case SourceCodeKind.File:
                        return(new VBScriptCode(
                                   _vbscript,
                                   _vbscript.ParseFileToLambda(sourceUnit.Path, reader),
                                   sourceUnit));

                    default:
                        throw Assert.Unreachable;
                    }
                }
                catch (VBScriptCompilerException ex)
                {
                    VBScriptSourceCodeReader vbscriptReader = reader as VBScriptSourceCodeReader;
                    if (vbscriptReader != null)
                    {
                        ISourceMapper mapper = vbscriptReader.SourceMapper;
                        if (mapper != null)
                        {
                            foreach (VBScriptSyntaxError error in ex.SyntaxErrors)
                            {
                                DocSpan docSpan = mapper.Map(error.Span);
                                error.FileName = docSpan.Uri;
                                error.Span     = docSpan.Span;
                            }
                        }
                    }
                    throw ex;
                }
                catch (Exception e)
                {
                    // Real language implementation would have a specific type
                    // of exception.  Also, they would pass errorSink down into
                    // the parser and add messages while doing tighter error
                    // recovery and continuing to parse.
                    errorSink.Add(sourceUnit, e.Message, SourceSpan.None, 0,
                                  Severity.FatalError);
                    return(null);
                }
            }
        }
        public void TestNonGenericMapper1()
        {
            ISourceMapper    mapper = FactoryMapper.DynamicResolutionMapper(typeof(PersonaGiuridica), typeof(PersonDetails));
            PersonaGiuridica person = new PersonaGiuridica
            {
                Code        = "150",
                Name        = "Sergio",
                Surname     = "Hill",
                AnnoNascita = 1980,
                Parent      = new Person {
                    Name = "fatherName", Surname = "fatherSurname", AnnoNascita = 1950
                }
            };

            var result = mapper.Map(person);

            Assert.IsNotNull(result);
        }
        public void TestMapperNonPublicMembers()
        {
            ISourceMapper <PersonaGiuridica, PersonDetails> mapper = FactoryMapper.DynamicResolutionMapper <PersonaGiuridica, PersonDetails>();
            PersonaGiuridica person = new PersonaGiuridica
            {
                Code        = "150",
                Name        = "Sergio",
                Surname     = "Hill",
                AnnoNascita = 1980,
                Parent      = new Person {
                    Name = "fatherName", Surname = "fatherSurname", AnnoNascita = 1950
                }
            };

            var result = mapper.Map(person);

            Assert.IsNotNull(result);
        }
        /// <summary>
        /// Tries to map the given instance into destination type using an specific key service.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="destinationType">Type of the destination.</param>
        /// <param name="keyService">The key service.</param>
        /// <returns></returns>
        public object TryToMap(object source, Type destinationType, object keyService)
        {
            if (source == null)
            {
                return(null);
            }

            var serviceMapper = this.mapperResolver.FirstOrDefault(
                transformer => transformer.Match(keyService, source.GetType(), destinationType));

            if (serviceMapper == null)
            {
                return(null);
            }

            ISourceMapper mapper = serviceMapper.ServiceAs <ISourceMapper>();

            return(mapper.Map(source));
        }