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