public void EmitFinish(ILGenerator generator) { if (_methodReturn == _delegateReturn) return; if (_methodReturn == typeof (void)) generator.Emit(OpCodes.Ldnull); else if (_methodReturn.IsValueType && !_delegateReturn.IsValueType) generator.EmitBox(_methodReturn); }
public override void EmitLoad(ILGenerator generator) { EmitLoadToStack(generator); var mt = _methodParameter.ParameterType.RemoveByRef(); var dt = _delegateParameter.ParameterType.RemoveByRef(); if (dt.IsValueType && !mt.IsValueType) generator.EmitBox(dt); else if (!mt.IsAssignableFrom(dt)) generator.EmitUnBoxAnyOrCastClass(mt); }
public override void EmitFinish(ILGenerator generator) { if (!_needLocalVariable) return; generator.EmitLdarg(_delegateParameterIndex); generator.EmitLdloc(_localVariable.LocalIndex); var mt = _methodParameter.ParameterType.RemoveByRef(); var dt = _delegateParameter.ParameterType.RemoveByRef(); if (mt.IsValueType && !dt.IsValueType) generator.EmitBox(mt); generator.EmitStind(dt); }
public override void EmitPrepare(ILGenerator generator) { base.EmitPrepare(generator); if (!_needLocalVariable) return; generator.EmitLdarg(_delegateParameterIndex); var dt = _delegateParameter.ParameterType.RemoveByRef(); var mt = _methodParameter.ParameterType.RemoveByRef(); if (_delegateParameter.ParameterType.IsByRef) generator.EmitLdind(dt); if (dt.IsValueType && !mt.IsValueType) generator.EmitBox(dt); else if (!mt.IsAssignableFrom(dt)) generator.EmitUnBoxAnyOrCastClass(mt); generator.EmitStloc(_localVariable.LocalIndex); }
/// <summary> /// Loads the arguments onto the stack. /// </summary> /// <param name="ilGenerator">The intermediate language generator.</param> /// <param name="parameterTypes">The parameter types.</param> /// <param name="parametersLocalBuilder">The parameters local builder.</param> private static void LoadArguments(ILGenerator ilGenerator, IList<Type> parameterTypes, LocalBuilder parametersLocalBuilder) { for (var index = 0; index < parameterTypes.Count; index++) { var parameterType = parameterTypes[index]; ilGenerator.Emit(OpCodes.Ldloc, parametersLocalBuilder); ilGenerator.EmitLoadValue(index); ilGenerator.EmitLoadArgument(index + 1); if (parameterType.IsByRef) { var elementType = parameterType.GetElementType(); ilGenerator.EmitLoadIndirect(parameterType); ilGenerator.EmitBox(elementType); } else { ilGenerator.EmitBox(parameterType); } ilGenerator.Emit(OpCodes.Stelem_Ref); } }
/// <summary> /// Restores the by reference parameter values. /// </summary> /// <param name="ilGenerator">The intermediate language generator.</param> /// <param name="argumentIndex">The argument index.</param> /// <param name="parameterTypes">The parameter types.</param> /// <param name="parameterValueLocalBuilders">The parameter value local builders.</param> private static void RestoreByReferenceParameterValues(ILGenerator ilGenerator, int argumentIndex, IList<Type> parameterTypes, IList<LocalBuilder> parameterValueLocalBuilders) { for (var index = 0; index < parameterTypes.Count; index++) { var parameterType = parameterTypes[index]; if (!parameterType.IsByRef) continue; var parameterLocalBuilder = parameterValueLocalBuilders[index]; var elementType = parameterType.GetElementType(); ilGenerator.EmitLoadArgument(argumentIndex); ilGenerator.EmitLoadValue(index); ilGenerator.Emit(OpCodes.Ldloc, parameterLocalBuilder); ilGenerator.EmitBox(elementType); ilGenerator.Emit(OpCodes.Stelem_Ref); } }