/// <summary>
    /// </summary>
    /// <param name="context">The context.</param>
    public void Compile(CompilationContext context)
    {
        var elseLabel  = context.ILGenerator.DefineLabel();
        var endIfLabel = context.ILGenerator.DefineLabel();

        Condition.Compile(context);
        context.Emit(OpCodes.Brfalse, elseLabel);

        if (TrueBranch != null)
        {
            TrueBranch.Compile(context);
        }

        if (FalseBranch != null)
        {
            context.Emit(OpCodes.Br, endIfLabel);
        }

        context.ILGenerator.MarkLabel(elseLabel);

        if (FalseBranch != null)
        {
            FalseBranch.Compile(context);
        }

        context.ILGenerator.MarkLabel(endIfLabel);
    }
        public void BuildCopyImplMethod()
        {
            if (ReflectionUtils.IsNullable(_from))
            {
                _from = Nullable.GetUnderlyingType(_from);
            }
            if (ReflectionUtils.IsNullable(_to))
            {
                _to = Nullable.GetUnderlyingType(_to);
            }

            MethodBuilder methodBuilder = _typeBuilder.DefineMethod(
                "MapImpl",
                MethodAttributes.Public | MethodAttributes.Virtual,
                typeof(object),
                new Type[] { typeof(object), typeof(object), typeof(object) }
                );

            ILGenerator        ilGen = methodBuilder.GetILGenerator();
            CompilationContext compilationContext = new CompilationContext(ilGen);

            AstComplexNode mapperAst    = new AstComplexNode();
            var            locFrom      = ilGen.DeclareLocal(_from);
            var            locTo        = ilGen.DeclareLocal(_to);
            var            locState     = ilGen.DeclareLocal(typeof(object));
            LocalBuilder   locException = null;

            mapperAst.Nodes.Add(BuilderUtils.InitializeLocal(locFrom, 1));
            mapperAst.Nodes.Add(BuilderUtils.InitializeLocal(locTo, 2));
            mapperAst.Nodes.Add(BuilderUtils.InitializeLocal(locState, 3));

#if DEBUG
            locException = compilationContext.ILGenerator.DeclareLocal(typeof(Exception));
#endif
            var mappingOperations = _mappingConfigurator.GetMappingOperations(_from, _to);
            StaticConvertersManager staticConverter = _mappingConfigurator.GetStaticConvertersManager();
            mapperAst.Nodes.Add(
                new MappingOperationsProcessor()
            {
                LocException            = locException,
                LocFrom                 = locFrom,
                LocState                = locState,
                LocTo                   = locTo,
                ObjectsMapperManager    = _objectsMapperManager,
                CompilationContext      = compilationContext,
                StoredObjects           = StoredObjects,
                Operations              = mappingOperations,
                MappingConfigurator     = _mappingConfigurator,
                RootOperation           = _mappingConfigurator.GetRootMappingOperation(_from, _to),
                StaticConvertersManager = staticConverter ?? StaticConvertersManager.DefaultInstance
            }.ProcessOperations()
                );
            mapperAst.Nodes.Add(
                new AstReturn()
            {
                ReturnType  = typeof(object),
                ReturnValue = AstBuildHelper.ReadLocalRV(locTo)
            }
                );

            mapperAst.Compile(compilationContext);
        }
Exemple #3
0
    /// <summary>
    ///   Builds the copy impl method.
    /// </summary>
    public void BuildCopyImplMethod()
    {
        if (ReflectionHelper.IsNullable(_from))
        {
            _from = _from.GetUnderlyingTypeCache();
        }

        if (ReflectionHelper.IsNullable(_to))
        {
            _to = _to.GetUnderlyingTypeCache();
        }

        var methodBuilder = _typeBuilder.DefineMethod(
            nameof(MapperBase.MapImpl),
            MethodAttributes.Public | MethodAttributes.Virtual,
            Metadata <object> .Type,
            new[] { Metadata <object> .Type, Metadata <object> .Type, Metadata <object> .Type });

        var ilGen = methodBuilder.GetILGenerator();
        var compilationContext = new CompilationContext(ilGen);

        var mapperAst = new AstComplexNode();

        var          locFrom      = ilGen.DeclareLocal(_from);
        var          locTo        = ilGen.DeclareLocal(_to);
        var          locState     = ilGen.DeclareLocal(Metadata <object> .Type);
        LocalBuilder locException = null;

        mapperAst.Nodes.Add(BuilderUtils.InitializeLocal(locFrom, 1));
        mapperAst.Nodes.Add(BuilderUtils.InitializeLocal(locTo, 2));
        mapperAst.Nodes.Add(BuilderUtils.InitializeLocal(locState, 3));

#if DEBUG
        locException = compilationContext.ILGenerator.DeclareLocal(Metadata <Exception> .Type);
#endif

        var mappingOperations = _mappingConfigurator.GetMappingOperations(_from, _to);
        var staticConverter   = _mappingConfigurator.GetStaticConvertersManager();

        mapperAst.Nodes.Add(
            new MappingOperationsProcessor
        {
            LocException            = locException,
            LocFrom                 = locFrom,
            LocState                = locState,
            LocTo                   = locTo,
            ObjectsMapperManager    = _objectsMapperManager,
            CompilationContext      = compilationContext,
            StoredObjects           = StoredObjects,
            Operations              = mappingOperations,
            MappingConfigurator     = _mappingConfigurator,
            RootOperation           = _mappingConfigurator.GetRootMappingOperation(_from, _to),
            StaticConvertersManager = staticConverter ?? StaticConvertersManager.DefaultInstance
        }.ProcessOperations());

        mapperAst.Nodes.Add(
            new AstReturn {
            ReturnType = Metadata <object> .Type, ReturnValue = AstBuildHelper.ReadLocalRV(locTo)
        });

        mapperAst.Compile(compilationContext);
    }