public override void GenerateConversion(IProxyModuleCoderAccess proxyModule, ILGenerator gen) { var toTypeCtor = _toType.GetConstructor(new[] { _coreToType }); if (!_fromTypeNullable) { _coreBinding.GenerateConversion(proxyModule, gen); gen.Emit(OpCodes.Newobj, toTypeCtor); } else { var fromLocal = gen.DeclareLocal(_fromType); var toLocal = gen.DeclareLocal(_toType); var hasValueLabel = gen.DefineLabel(); var doneLabel = gen.DefineLabel(); gen.Emit(OpCodes.Stloc, fromLocal); gen.Emit(OpCodes.Ldloca, fromLocal); gen.Emit(OpCodes.Call, _fromType.GetMethod("get_HasValue")); gen.Emit(OpCodes.Brtrue, hasValueLabel); gen.Emit(OpCodes.Ldloca, toLocal); gen.Emit(OpCodes.Initobj, _toType); gen.Emit(OpCodes.Ldloc, toLocal); gen.Emit(OpCodes.Br, doneLabel); // :HasValue gen.MarkLabel(hasValueLabel); gen.Emit(OpCodes.Ldloca, fromLocal); gen.Emit(OpCodes.Call, _fromType.GetMethod("GetValueOrDefault", Type.EmptyTypes)); _coreBinding.GenerateConversion(proxyModule, gen); gen.Emit(OpCodes.Newobj, toTypeCtor); // :Done gen.MarkLabel(doneLabel); } }
public override void GenerateCall(IProxyModuleCoderAccess proxyModule, ILGenerator gen) { var pars = _adaptee.GetParameters(); var targetPars = _target.GetParameters(); var tokens = new object[pars.Length]; // Generate in conversions for (ushort i = 0; i < pars.Length; ++i) { // ReSharper disable once AccessToModifiedClosure tokens[i] = _paramBindings[i].GenerateInConversion(() => gen.EmitBestLdArg((ushort)(i + 1)), proxyModule, gen); } gen.Emit(OpCodes.Callvirt, _target); // Generate out conversions for (ushort i = 0; i < pars.Length; ++i) { // ReSharper disable once AccessToModifiedClosure _paramBindings[i].GenerateOutConversion(tokens[i], () => gen.EmitBestLdArg((ushort)(i + 1)), proxyModule, gen); } _retValBinding.GenerateConversion(proxyModule, gen); }
public override void GenerateOutConversion(object token, Action load, IProxyModuleCoderAccess proxyModule, ILGenerator gen) { var local = (LocalBuilder)token; load(); gen.Emit(OpCodes.Ldloc, local); _valueBinding.GenerateConversion(proxyModule, gen); gen.EmitStoreToRef(_fromType); }
public override object GenerateInConversion(Action load, IProxyModuleCoderAccess proxyModule, ILGenerator gen) { load(); _valueBinding.GenerateConversion(proxyModule, gen); return(null); }