public MappingOperationsProcessor(MappingOperationsProcessor prototype)
		{
			locFrom = prototype.locFrom;
			locTo = prototype.locTo;
			locState = prototype.locState;
			locException = prototype.locException;
			compilationContext = prototype.compilationContext;
			operations = prototype.operations;
			storedObjects = prototype.storedObjects;
			mappingConfigurator = prototype.mappingConfigurator;
			objectsMapperManager = prototype.objectsMapperManager;
			rootOperation = prototype.rootOperation;
			staticConvertersManager = prototype.staticConvertersManager;
		}
        public static void BuildCreateTargetInstanceMethod(Type type, TypeBuilder typeBuilder)
        {
            if (ReflectionUtils.IsNullable(type))
            {
                type = Nullable.GetUnderlyingType(type);
            }

            MethodBuilder methodBuilder = typeBuilder.DefineMethod(
                "CreateTargetInstance",
                MethodAttributes.Assembly | MethodAttributes.Virtual,
                typeof(object),
                null
                );

            ILGenerator ilGen = methodBuilder.GetILGenerator();
            CompilationContext context = new CompilationContext(ilGen);
            IAstRefOrValue returnValue;

            if (type.IsValueType)
            {
                LocalBuilder lb = ilGen.DeclareLocal(type);
                new AstInitializeLocalVariable(lb).Compile(context);

                returnValue =
                    new AstBox()
                    {
                        value = AstBuildHelper.ReadLocalRV(lb)
                    };
            }
            else
            {
                returnValue =
                    ReflectionUtils.HasDefaultConstructor(type)
                        ?
                            new AstNewObject()
                            {
                                objectType = type
                            }
                        :
                            (IAstRefOrValue)new AstConstantNull();
            }
            new AstReturn()
            {
                returnType = type,
                returnValue = returnValue
            }.Compile(context);
        }
示例#3
0
		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);
		}