Skip to content

Commit 74ad20e

Browse files
authored
Merge pull request #115 from GeirLudvigsen/methodfiltering
Added Regex filters for improved weaving targeting Unittests are missing, so only a pre-release will be provided
2 parents 8789ad3 + 5eaccc1 commit 74ad20e

5 files changed

Lines changed: 66 additions & 1 deletion

File tree

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
[![NuGet](https://img.shields.io/nuget/v/MethodBoundaryAspect.Fody.svg)](https://www.nuget.org/packages/MethodBoundaryAspect.Fody/)
33

44
## MethodBoundaryAspect.Fody
5-
> 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.
5+
> 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.
66
77
You can easily write your own aspects for
88
- transaction handling
@@ -25,6 +25,10 @@ You can easily write your own aspects for
2525
- globally in `AssemblyInfo.cs`
2626
- on class
2727
- on method
28+
- Filter which methods to include in weaving, using Regex patterns
29+
- NamespaceFilter
30+
- TypeNameFilter
31+
- MethodNameFilter
2832
- Change method behavior (see examples below)
2933
- Overwrite input arguments values (byValue and byRef) to be forwarded to the method.
3034
- no async support

src/MethodBoundaryAspect.Fody/AttributeNames.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@ namespace MethodBoundaryAspect.Fody
33
static class AttributeNames
44
{
55
public static string AttributeTargetMemberAttributes => "AttributeTargetMemberAttributes";
6+
public static string NamespaceFilter => "NamespaceFilter";
7+
public static string TypeNameFilter => "TypeNameFilter";
8+
public static string MethodNameFilter => "MethodNameFilter";
69
}
710
}

src/MethodBoundaryAspect.Fody/ModuleWeaver.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.IO;
55
using System.Linq;
66
using System.Runtime.CompilerServices;
7+
using System.Text.RegularExpressions;
78
using Fody;
89
using MethodBoundaryAspect.Fody.Ordering;
910
using Mono.Cecil;
@@ -204,6 +205,9 @@ private void WeaveType(
204205
.Where(IsMethodBoundaryAspect)
205206
.Select(x => new AspectInfo(x))
206207
.Where(info => info.HasTargetMemberAttribute(methodVisibility))
208+
.Where(info => string.IsNullOrEmpty(info.NamespaceFilter) || Regex.IsMatch(type.Namespace, info.NamespaceFilter))
209+
.Where(info => string.IsNullOrEmpty(info.TypeNameFilter) || Regex.IsMatch(type.Name, info.TypeNameFilter))
210+
.Where(info => string.IsNullOrEmpty(info.MethodNameFilter) || Regex.IsMatch(method.Name, info.MethodNameFilter))
207211
.Where(x => !IsSelfWeaving(type, x))
208212
.ToList();
209213
if (aspectInfos.Count == 0)

src/MethodBoundaryAspect.Fody/Ordering/AspectInfo.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Linq;
34
using Mono.Cecil;
@@ -27,6 +28,9 @@ public AspectInfo(CustomAttribute aspectAttribute)
2728
InitSkipProperties(aspectAttributes);
2829
InitTargetMembers();
2930
InitChangingInputArguments(aspectAttributes);
31+
InitNamespaceFilter();
32+
InitTypeNameFilter();
33+
InitMethodNameFilter();
3034
}
3135

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

5054
public bool AllowChangingInputArguments { get; private set; }
5155

56+
public string NamespaceFilter { get; set; }
57+
public string TypeNameFilter { get; set; }
58+
public string MethodNameFilter { get; set; }
59+
5260
public IEnumerable<MethodAttributes> AttributeTargetMemberAttributes { get; set; } =
5361
new List<MethodAttributes>
5462
{
@@ -199,5 +207,47 @@ private void InitChangingInputArguments(IEnumerable<CustomAttribute> aspectAttri
199207
.Any(c => c.AttributeType.FullName == AttributeFullNames.AllowChangingInputArguments);
200208

201209
}
210+
211+
private void InitNamespaceFilter()
212+
{
213+
var namespaceFilterArgument = AspectAttribute.Properties
214+
.FirstOrDefault(property => property.Name == AttributeNames.NamespaceFilter);
215+
216+
if (namespaceFilterArgument.Equals(default(CustomAttributeNamedArgument)))
217+
return;
218+
219+
if (!(namespaceFilterArgument.Argument.Value is string argumentValue))
220+
return;
221+
222+
NamespaceFilter = argumentValue;
223+
}
224+
225+
private void InitTypeNameFilter()
226+
{
227+
var typeNameFilterArgument = AspectAttribute.Properties
228+
.FirstOrDefault(property => property.Name == AttributeNames.TypeNameFilter);
229+
230+
if (typeNameFilterArgument.Equals(default(CustomAttributeNamedArgument)))
231+
return;
232+
233+
if (!(typeNameFilterArgument.Argument.Value is string argumentValue))
234+
return;
235+
236+
TypeNameFilter = argumentValue;
237+
}
238+
239+
private void InitMethodNameFilter()
240+
{
241+
var methodNameFilterArgument = AspectAttribute.Properties
242+
.FirstOrDefault(property => property.Name == AttributeNames.MethodNameFilter);
243+
244+
if (methodNameFilterArgument.Equals(default(CustomAttributeNamedArgument)))
245+
return;
246+
247+
if (!(methodNameFilterArgument.Argument.Value is string argumentValue))
248+
return;
249+
250+
MethodNameFilter = argumentValue;
251+
}
202252
}
203253
}

src/MethodBoundaryAspect/Attributes/OnMethodBoundaryAspect.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ public abstract class OnMethodBoundaryAspect : Attribute
88
public MulticastAttributes AttributeTargetMemberAttributes { get; set; } =
99
MulticastAttributes.AnyVisibility;
1010

11+
public virtual string NamespaceFilter { get; set; }
12+
public virtual string TypeNameFilter { get; set; }
13+
public virtual string MethodNameFilter { get; set; }
14+
1115
public virtual void OnEntry(MethodExecutionArgs arg)
1216
{
1317
}

0 commit comments

Comments
 (0)