/src/LinFu.AOP/Emitters/SaveReturnValue.cs
http://github.com/philiplaureano/LinFu · C# · 46 lines · 27 code · 7 blank · 12 comment · 6 complexity · 174b513676503edf25a9ad7f37b4489c MD5 · raw file
- using LinFu.AOP.Cecil.Interfaces;
- using LinFu.Reflection.Emit;
- using Mono.Cecil;
- using Mono.Cecil.Cil;
- namespace LinFu.AOP.Cecil
- {
- /// <summary>
- /// Represents an instruction emitter that saves the return value from a given method call.
- /// </summary>
- public class SaveReturnValue : IInstructionEmitter
- {
- private readonly TypeReference _returnType;
- private readonly VariableDefinition _returnValue;
- /// <summary>
- /// Initializes a new instance of the <see cref="SaveReturnValue" /> class.
- /// </summary>
- /// <param name="returnType">The return type.</param>
- /// <param name="returnValue">The return value.</param>
- public SaveReturnValue(TypeReference returnType, VariableDefinition returnValue)
- {
- _returnType = returnType;
- _returnValue = returnValue;
- }
- /// <summary>
- /// Saves the return value from a given method call.
- /// </summary>
- /// <param name="IL">The <see cref="ILProcessor" /> pointing to the target method body.</param>
- public void Emit(ILProcessor IL)
- {
- var module = IL.Body.Method.DeclaringType.Module;
-
- var voidType = module.ImportType(typeof(void));
- var returnTypeIsValueType = _returnType != voidType && _returnType.IsValueType;
- if (_returnType is GenericParameter || returnTypeIsValueType)
- IL.Create(OpCodes.Box, _returnType);
- if (_returnType != voidType)
- IL.Create(OpCodes.Stloc, _returnValue);
- }
- }
- }