Ejemplo n.º 1
0
        public Func <object, object> CreateDelegate(Type fromType, Type toType, IFormatProvider provider)
        {
            var convertDynamicMethod = new DynamicMethod(String.Concat("ConvertFrom", fromType.Name, "To", toType.Name, "NonGeneric"),
                                                         typeof(object), new[] { typeof(IFormatProvider), typeof(object) }, typeof(ConverterFactory).Module, true);

            var il = new ILGeneratorAdapter(this.mapCfg);

            il.Emit(OpCodes.Ldarg_1);
            il.Emit(fromType.IsValueType ? OpCodes.Unbox_Any : OpCodes.Castclass, fromType); // cast input to correct type
            il.EmitConvertValue(fromType, toType, new HashSet <Type>());
            il.Emit(OpCodes.Box, toType);
            il.Emit(OpCodes.Ret);

            return((Func <object, object>)CompileDynamicMethod <Func <object, object> >(convertDynamicMethod, il.Instructions, provider));
        }
Ejemplo n.º 2
0
        public Converter <TFrom, TTo> CreateDelegate <TFrom, TTo>(IFormatProvider provider)
        {
            var toType   = typeof(TTo);
            var fromType = typeof(TFrom);

            var convertDynamicMethod = new DynamicMethod(String.Concat("ConvertFrom", fromType.Name, "To", toType.Name),
                                                         toType, new[] { typeof(IFormatProvider), fromType }, typeof(ConverterFactory).Module, true);

            var il = new ILGeneratorAdapter(this.mapCfg);

            il.Emit(OpCodes.Ldarg_1);
            il.EmitConvertValue(fromType, toType, new HashSet <Type>());
            il.Emit(OpCodes.Ret);

            return((Converter <TFrom, TTo>)CompileDynamicMethod <Converter <TFrom, TTo> >(convertDynamicMethod, il.Instructions, provider));
        }
Ejemplo n.º 3
0
        private ILInstruction[] GetTryParseInstructions <TTo>() where TTo : struct
        {
            var        toType   = typeof(TTo);
            MethodInfo tryParse = toType.GetMethods(BindingFlags.Static | BindingFlags.Public)
                                  .Where(m => m.Name == "TryParse" && m.GetParameters().Length == 2).FirstOrDefault();

            var ilGenerator = new ILGeneratorAdapter(this);

            if (tryParse != null)
            {
                var toLocal = ilGenerator.DeclareLocal(toType);
                ilGenerator.EmitLocal(OpCodes.Ldloca, toLocal);
                ilGenerator.EmitCall(OpCodes.Call, tryParse);
                ilGenerator.Emit(OpCodes.Pop);
                ilGenerator.EmitLocal(OpCodes.Ldloc, toLocal);
            }
            return(ilGenerator.Instructions);
        }
Ejemplo n.º 4
0
 public void Setup()
 {
     this.ilgenerator = new ILGeneratorAdapter(new MapConfiguration());
 }