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)); }
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)); }