Skip to content

Commit

Permalink
Merge pull request #115 from GeirLudvigsen/methodfiltering
Browse files Browse the repository at this point in the history
Added Regex filters for improved weaving targeting 

Unittests are missing, so only a pre-release will be provided
  • Loading branch information
Ralf1108 authored May 4, 2023
2 parents 8789ad3 + 5eaccc1 commit 74ad20e
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 1 deletion.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[![NuGet](https://img.shields.io/nuget/v/MethodBoundaryAspect.Fody.svg)](https://www.nuget.org/packages/MethodBoundaryAspect.Fody/)

## MethodBoundaryAspect.Fody
> A [Fody weaver](https://github.com/Fody/Fody) which allows to decorate methods and hook into method start, method end and method exceptions. Additionally you have access to useful method parameters.
> A [Fody weaver](https://github.com/Fody/Fody) which allows to decorate assemblies, classes and methods and hook into method start, method end and method exceptions. Additionally you have access to useful method parameters.
You can easily write your own aspects for
- transaction handling
Expand All @@ -25,6 +25,10 @@ You can easily write your own aspects for
- globally in `AssemblyInfo.cs`
- on class
- on method
- Filter which methods to include in weaving, using Regex patterns
- NamespaceFilter
- TypeNameFilter
- MethodNameFilter
- Change method behavior (see examples below)
- Overwrite input arguments values (byValue and byRef) to be forwarded to the method.
- no async support
Expand Down
3 changes: 3 additions & 0 deletions src/MethodBoundaryAspect.Fody/AttributeNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ namespace MethodBoundaryAspect.Fody
static class AttributeNames
{
public static string AttributeTargetMemberAttributes => "AttributeTargetMemberAttributes";
public static string NamespaceFilter => "NamespaceFilter";
public static string TypeNameFilter => "TypeNameFilter";
public static string MethodNameFilter => "MethodNameFilter";
}
}
4 changes: 4 additions & 0 deletions src/MethodBoundaryAspect.Fody/ModuleWeaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using Fody;
using MethodBoundaryAspect.Fody.Ordering;
using Mono.Cecil;
Expand Down Expand Up @@ -204,6 +205,9 @@ private void WeaveType(
.Where(IsMethodBoundaryAspect)
.Select(x => new AspectInfo(x))
.Where(info => info.HasTargetMemberAttribute(methodVisibility))
.Where(info => string.IsNullOrEmpty(info.NamespaceFilter) || Regex.IsMatch(type.Namespace, info.NamespaceFilter))
.Where(info => string.IsNullOrEmpty(info.TypeNameFilter) || Regex.IsMatch(type.Name, info.TypeNameFilter))
.Where(info => string.IsNullOrEmpty(info.MethodNameFilter) || Regex.IsMatch(method.Name, info.MethodNameFilter))
.Where(x => !IsSelfWeaving(type, x))
.ToList();
if (aspectInfos.Count == 0)
Expand Down
50 changes: 50 additions & 0 deletions src/MethodBoundaryAspect.Fody/Ordering/AspectInfo.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Mono.Cecil;
Expand Down Expand Up @@ -27,6 +28,9 @@ public AspectInfo(CustomAttribute aspectAttribute)
InitSkipProperties(aspectAttributes);
InitTargetMembers();
InitChangingInputArguments(aspectAttributes);
InitNamespaceFilter();
InitTypeNameFilter();
InitMethodNameFilter();
}

public TypeDefinition AspectTypeDefinition { get; }
Expand All @@ -49,6 +53,10 @@ public AspectInfo(CustomAttribute aspectAttribute)

public bool AllowChangingInputArguments { get; private set; }

public string NamespaceFilter { get; set; }
public string TypeNameFilter { get; set; }
public string MethodNameFilter { get; set; }

public IEnumerable<MethodAttributes> AttributeTargetMemberAttributes { get; set; } =
new List<MethodAttributes>
{
Expand Down Expand Up @@ -199,5 +207,47 @@ private void InitChangingInputArguments(IEnumerable<CustomAttribute> aspectAttri
.Any(c => c.AttributeType.FullName == AttributeFullNames.AllowChangingInputArguments);

}

private void InitNamespaceFilter()
{
var namespaceFilterArgument = AspectAttribute.Properties
.FirstOrDefault(property => property.Name == AttributeNames.NamespaceFilter);

if (namespaceFilterArgument.Equals(default(CustomAttributeNamedArgument)))
return;

if (!(namespaceFilterArgument.Argument.Value is string argumentValue))
return;

NamespaceFilter = argumentValue;
}

private void InitTypeNameFilter()
{
var typeNameFilterArgument = AspectAttribute.Properties
.FirstOrDefault(property => property.Name == AttributeNames.TypeNameFilter);

if (typeNameFilterArgument.Equals(default(CustomAttributeNamedArgument)))
return;

if (!(typeNameFilterArgument.Argument.Value is string argumentValue))
return;

TypeNameFilter = argumentValue;
}

private void InitMethodNameFilter()
{
var methodNameFilterArgument = AspectAttribute.Properties
.FirstOrDefault(property => property.Name == AttributeNames.MethodNameFilter);

if (methodNameFilterArgument.Equals(default(CustomAttributeNamedArgument)))
return;

if (!(methodNameFilterArgument.Argument.Value is string argumentValue))
return;

MethodNameFilter = argumentValue;
}
}
}
4 changes: 4 additions & 0 deletions src/MethodBoundaryAspect/Attributes/OnMethodBoundaryAspect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ public abstract class OnMethodBoundaryAspect : Attribute
public MulticastAttributes AttributeTargetMemberAttributes { get; set; } =
MulticastAttributes.AnyVisibility;

public virtual string NamespaceFilter { get; set; }
public virtual string TypeNameFilter { get; set; }
public virtual string MethodNameFilter { get; set; }

public virtual void OnEntry(MethodExecutionArgs arg)
{
}
Expand Down

0 comments on commit 74ad20e

Please sign in to comment.