示例#1
0
        public virtual void Emit(CompilationContext context)
        {
            var sourceType     = SourceType;
            var targetType     = TargetMember.MemberType;
            var targetCanWrite = TargetMember.CanWrite(HasOption(MemberMapOptions.NonPublic));

            if (targetCanWrite && _converter != null)
            {
                EmitSource(context);
                _converter.Emit(sourceType, targetType, context);
                EmitSetTarget(context);
                return;
            }
            if (!targetCanWrite && _mapper != null)
            {
                EmitSource(context);
                EmitSetTarget(context);
                _mapper.Emit(sourceType, targetType, context);
                return;
            }
            if (!HasOption(MemberMapOptions.Hierarchy) ||
                !targetType.GetTypeInfo().IsClass ||
                targetType.IsNullable())
            {
                var converter = GetConvertEmitter(sourceType, targetType);
                if (converter != null)
                {
                    EmitSource(context);
                    converter(context);
                    EmitSetTarget(context);
                }
            }
            else
            {
                var mapperType = _genericMapperTypes[Triplet.Create(_container, sourceType, targetType)];
                if (targetCanWrite)
                {
                    EmitSource(context);
                    context.CurrentType = sourceType;
                    context.EmitCall(mapperType.GetMethod("Convert"));
                    EmitSetTarget(context);
                }
                else
                {
                    var sourceValue = context.DeclareLocal(sourceType);
                    EmitSource(context);
                    context.Emit(OpCodes.Stloc, sourceValue);

                    var targetValue = context.DeclareLocal(targetType);
                    ((IMemberBuilder)TargetMember).EmitGetter(context);
                    context.Emit(OpCodes.Stloc, targetValue);

                    context.Emit(OpCodes.Ldloc, sourceValue);
                    context.Emit(OpCodes.Ldloc, targetValue);
                    context.EmitCall(mapperType.GetMethod("Map"));
                }
            }
        }
示例#2
0
        public virtual void Emit(CompilationContext context)
        {
            var sourceType     = SourceType;
            var targetType     = TargetMember.MemberType;
            var targetCanWrite = TargetMember.CanWrite(HasOption(MemberMapOptions.NonPublic));

            if (targetCanWrite && _converter != null)
            {
                EmitSource(context);
                _converter.Emit(sourceType, targetType, context);
                EmitSetTarget(context);
                return;
            }
            if (!targetCanWrite && _mapper != null)
            {
                EmitSource(context);
                EmitSetTarget(context);
                _mapper.Emit(sourceType, targetType, context);
                return;
            }
            if (!HasOption(MemberMapOptions.Hierarchy) ||
#if NETSTANDARD
                !targetType.GetTypeInfo().IsClass
#else
                !targetType.IsClass
#endif
                || targetType.IsNullable())
            {
                var converter = GetConvertEmitter(sourceType, targetType);
                if (converter != null)
                {
                    EmitSource(context);
                    converter(context);
                    EmitSetTarget(context);
                }
            }