/src/LinFu.AOP/Extensions/MethodDefinitionExtensions.cs

http://github.com/philiplaureano/LinFu · C# · 30 lines · 18 code · 3 blank · 9 comment · 0 complexity · 50d147019aa596192bbb945257b77143 MD5 · raw file

  1. using System;
  2. using Mono.Cecil;
  3. using Mono.Cecil.Cil;
  4. namespace LinFu.AOP.Cecil.Extensions
  5. {
  6. /// <summary>
  7. /// Represents an extension class that adds helper methods to the <see cref="MethodDefinition" /> type.
  8. /// </summary>
  9. public static class MethodDefinitionExtensions
  10. {
  11. /// <summary>
  12. /// Adds a local variable to the given method.
  13. /// </summary>
  14. /// <param name="methodDef">The target method.</param>
  15. /// <param name="localType">The variable type.</param>
  16. /// <returns>A local variable definition.</returns>
  17. public static VariableDefinition AddLocal(this MethodDefinition methodDef, Type localType)
  18. {
  19. var declaringType = methodDef.DeclaringType;
  20. var module = declaringType.Module;
  21. var variableType = module.Import(localType);
  22. var result = new VariableDefinition(variableType);
  23. methodDef.Body.Variables.Add(result);
  24. return result;
  25. }
  26. }
  27. }