/// <summary> /// Rewrites the specified method definition using the specified transform. /// </summary> private static void RewriteMethod(MethodDefinition method, AssemblyTransform transform) { Debug.WriteLine($"........... Method {method.FullName}"); transform.VisitMethod(method); // Only non-abstract method bodies can be rewritten. if (!method.IsAbstract) { foreach (var variable in method.Body.Variables) { transform.VisitVariable(variable); } // Do exception handlers before the method instructions because they are a // higher level concept and it's handy to pre-process them before seeing the // raw instructions. if (method.Body.HasExceptionHandlers) { foreach (var handler in method.Body.ExceptionHandlers) { transform.VisitExceptionHandler(handler); } } // Rewrite the method body instructions. Instruction instruction = method.Body.Instructions.FirstOrDefault(); while (instruction != null) { instruction = transform.VisitInstruction(instruction); instruction = instruction.Next; } } }
/// <summary> /// Rewrites the specified module definition using the specified transform. /// </summary> private static void RewriteModule(ModuleDefinition module, AssemblyTransform transform) { Debug.WriteLine($"....... Module: {module.Name} ({module.FileName})"); transform.VisitModule(module); foreach (var type in module.GetTypes()) { RewriteType(type, transform); } }
/// <summary> /// Rewrites the specified type definition using the specified transform. /// </summary> private static void RewriteType(TypeDefinition type, AssemblyTransform transform) { Debug.WriteLine($"......... Type: {type.FullName}"); transform.VisitType(type); foreach (var field in type.Fields) { Debug.WriteLine($"........... Field: {field.FullName}"); transform.VisitField(field); } foreach (var method in type.Methods) { RewriteMethod(method, transform); } }