Skip to content

Commit 653b2ac

Browse files
committed
Added method filtering by namespace, typename and methodname
1 parent 8789ad3 commit 653b2ac

4 files changed

Lines changed: 61 additions & 0 deletions

File tree

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)