/// <summary> /// Visits a <see cref="MethodDefinition"/> instance. /// </summary> /// <param name="method">The <see cref="MethodDefinition"/> instance that will be modified.</param> public override void VisitMethodDefinition(MethodDefinition method) { if (!_methodWeaver.ShouldWeave(method)) { return; } _methodWeaver.Weave(method); base.VisitMethodDefinition(method); }
/// <summary> /// Modifies the current type using the given method weaver. /// </summary> /// <param name="targetType">The target type to be modified.</param> /// <param name="weaver">The method weaver that will modify the selected methods.</param> /// <param name="methodFilter">The method filter that will determine which methods should be modified.</param> public static void WeaveWith(this TypeDefinition targetType, IMethodWeaver weaver, Func <MethodReference, bool> methodFilter) { var methods = targetType.Methods.Where(m => m.HasBody && weaver.ShouldWeave(m) && methodFilter(m)) .ToArray(); foreach (var method in methods) { weaver.Weave(method); } }
/// <summary> /// Modifies the chosen types in an assembly using the given method weaver. /// </summary> /// <param name="targetAssembly">The target assembly that will be modified.</param> /// <param name="weaver">The method weaver that will be used to rewrite all the target methods.</param> /// <param name="typeFilter">The predicate that will determine which types will be modified.</param> public static void WeaveWith(this AssemblyDefinition targetAssembly, IMethodWeaver weaver, Func <TypeReference, bool> typeFilter) { var module = targetAssembly.MainModule; var types = module.Types.Where(t => typeFilter(t) && t.IsDefinition).ToArray(); var methods = types.SelectMany(t => t.Methods.Where(m => m.HasBody && weaver.ShouldWeave(m))).ToArray(); foreach (var method in methods) { weaver.Weave(method); } }
/// <summary> /// Applies a <see cref="IMethodWeaver"/> instance to all methods /// within the given <paramref name="targetType"/>. /// </summary> /// <param name="targetType">The target module.</param> /// <param name="weaver">The <see cref="ITypeWeaver"/> instance that will modify the methods in the given target type.</param> public static void WeaveWith(this TypeDefinition targetType, IMethodWeaver weaver) { ModuleDefinition module = targetType.Module; IEnumerable <MethodDefinition> targetMethods = from MethodDefinition method in targetType.Methods where weaver.ShouldWeave(method) select method; // Modify the host module weaver.ImportReferences(module); // Add any additional members to the target type weaver.AddAdditionalMembers(targetType); foreach (MethodDefinition item in targetMethods) { weaver.Weave(item); } }
/// <summary> /// Applies a <see cref="IMethodWeaver"/> instance to all methods /// within the given <paramref name="targetType"/>. /// </summary> /// <param name="targetType">The target module.</param> /// <param name="weaver">The <see cref="ITypeWeaver"/> instance that will modify the methods in the given target type.</param> public static void WeaveWith(this TypeDefinition targetType, IMethodWeaver weaver) { var module = targetType.Module; var targetMethods = from MethodDefinition method in targetType.Methods where weaver.ShouldWeave(method) select method; // Modify the host module weaver.ImportReferences(module); // Add any additional members to the target type weaver.AddAdditionalMembers(targetType); foreach (var item in targetMethods) { weaver.Weave(item); } }