Example #1
0
 public bool CanMap(Type from, Type to)
 {
     return
         (
         MapperUtils.IsNullable(to) && MapperUtils.IsNullable(from) &&
         _mapper.CanMap(from.GetGenericArguments()[0], to.GetGenericArguments()[0])
         );
 }
Example #2
0
 public void Compile(CompilationContext context)
 {
     if (!(value is IAstRef) && !MapperUtils.IsNullable(value.itemType))
     {
         context.Emit(OpCodes.Ldc_I4_1);
     }
     else if (MapperUtils.IsNullable(value.itemType))
     {
         AstBuildHelper.ReadPropertyRV(
             new AstValueToAddr((IAstValue)value),
             value.itemType.GetProperty("HasValue")
             ).Compile(context);
         context.Emit(OpCodes.Ldc_I4_0);
         context.Emit(OpCodes.Ceq);
     }
     else
     {
         value.Compile(context);
         new AstConstantNull().Compile(context);
         context.Emit(OpCodes.Ceq);
     }
 }
Example #3
0
        public void Compile(CompilationContext context)
        {
            if (MapperUtils.IsNullable(objectType))
            {
                IAstRefOrValue underlyingValue;
                var            underlyingType = Nullable.GetUnderlyingType(objectType);
                if (ConstructorParams == null || ConstructorParams.Length == 0)
                {
                    LocalBuilder temp = context.ilGenerator.DeclareLocal(underlyingType);
                    new AstInitializeLocalVariable(temp).Compile(context);
                    underlyingValue = AstBuildHelper.ReadLocalRV(temp);
                }
                else
                {
                    underlyingValue = (IAstValue)ConstructorParams[0];
                }

                ConstructorInfo constructor = objectType.GetConstructor(
                    BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance,
                    null,
                    new[] { underlyingType },
                    null);

                underlyingValue.Compile(context);
                context.EmitNewObject(constructor);
            }
            else
            {
                Type[] types;
                if (ConstructorParams == null || ConstructorParams.Length == 0)
                {
                    types = new Type[0];
                }
                else
                {
                    types = ConstructorParams.Select(c => c.itemType).ToArray();
                    foreach (var p in ConstructorParams)
                    {
                        p.Compile(context);
                    }
                }

                ConstructorInfo ci = objectType.GetConstructor(types);
                if (ci != null)
                {
                    context.EmitNewObject(ci);
                }
                else if (objectType.IsValueType)
                {
                    LocalBuilder temp = context.ilGenerator.DeclareLocal(objectType);
                    new AstInitializeLocalVariable(temp).Compile(context);
                    AstBuildHelper.ReadLocalRV(temp).Compile(context);
                }
                else
                {
                    throw new Exception(
                              String.Format("Constructor not found in {0}", objectType.FullName)
                              );
                }
            }
        }