예제 #1
0
        /// <summary>
        /// 写入类型转换的 IL 指令。
        /// </summary>
        /// <param name="generator">IL 的指令生成器。</param>
        /// <param name="inputType">要转换的对象的类型。</param>
        /// <param name="outputType">要将输入对象转换到的类型。</param>
        /// <param name="isChecked">是否执行溢出检查。</param>
        public override void Emit(ILGenerator generator, Type inputType, Type outputType, bool isChecked)
        {
            Type       methodType = method.GetParametersNoCopy()[0].ParameterType;
            Conversion conv       = ConversionFactory.GetPreDefinedConversion(inputType, methodType);

            conv.Emit(generator, inputType, methodType, isChecked);
            generator.Emit(OpCodes.Call, method);
            methodType = method.ReturnType;
            conv       = ConversionFactory.GetPreDefinedConversion(methodType, outputType);
            if (conv is FromNullableConversion)
            {
                generator.EmitGetAddress(methodType);
            }
            conv.Emit(generator, methodType, outputType, isChecked);
        }
예제 #2
0
        /// <summary>
        /// 写入类型转换的 IL 指令。
        /// </summary>
        /// <param name="generator">IL 的指令生成器。</param>
        /// <param name="inputType">要转换的对象的类型。</param>
        /// <param name="outputType">要将输入对象转换到的类型。</param>
        /// <param name="isChecked">是否执行溢出检查。</param>
        public override void Emit(ILGenerator generator, Type inputType, Type outputType, bool isChecked)
        {
            Contract.Assume(inputType.IsNullable());
            Type inputUnderlyingType = Nullable.GetUnderlyingType(inputType);

            generator.EmitCall(inputType.GetMethod("get_Value"));
            if (inputUnderlyingType != outputType)
            {
                Conversion conversion = ConversionFactory.GetConversion(inputUnderlyingType, outputType);
                Contract.Assume(conversion != null);
                conversion.Emit(generator, inputUnderlyingType, outputType, isChecked);
            }
        }
예제 #3
0
        /// <summary>
        /// 写入类型转换的 IL 指令。
        /// </summary>
        /// <param name="generator">IL 的指令生成器。</param>
        /// <param name="inputType">要转换的对象的类型。</param>
        /// <param name="outputType">要将输入对象转换到的类型。</param>
        /// <param name="isChecked">是否执行溢出检查。</param>
        public override void Emit(ILGenerator generator, Type inputType, Type outputType, bool isChecked)
        {
            Contract.Assume(outputType.IsNullable());
            Type outputUnderlyingType = Nullable.GetUnderlyingType(outputType);

            if (inputType != outputUnderlyingType)
            {
                Conversion conversion = ConversionFactory.GetConversion(inputType, outputUnderlyingType);
                Contract.Assume(conversion != null);
                conversion.Emit(generator, inputType, outputUnderlyingType, isChecked);
            }
            ConstructorInfo ctor = outputType.GetConstructor(new[] { outputUnderlyingType });

            Contract.Assume(ctor != null);
            generator.Emit(OpCodes.Newobj, ctor);
        }