Skip to content

Commit b24f248

Browse files
committed
Simplify MethodFinder.GetAllInstanceMethods
Outside of the test suite, this method only gets called to find both public and non-public instance methods; the `flags` parameter is there- fore in essence constant and can be optimized away, along with several assertions, tests, and the `MakeFilteredCopy` method.
1 parent 7e062ea commit b24f248

File tree

5 files changed

+6
-79
lines changed

5 files changed

+6
-79
lines changed

src/Castle.Core.Tests/DynamicProxy.Tests/GenericInterfaceProxyTestCase.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ namespace Castle.DynamicProxy.Tests
1616
{
1717
using System;
1818
using System.Collections.Generic;
19-
using System.Reflection;
2019

2120
using Castle.DynamicProxy.Generators;
2221
using Castle.DynamicProxy.Tests.GenInterfaces;
@@ -418,8 +417,7 @@ public void TypeGetMethodsIsStable()
418417
public void MethodFinderIsStable()
419418
{
420419
ProxyWithGenInterfaceWithBase();
421-
Assert.AreEqual(4, MethodFinder.GetAllInstanceMethods(
422-
typeof(IGenInterfaceHierarchyBase<int>), BindingFlags.Public | BindingFlags.Instance).Length);
420+
Assert.AreEqual(4, MethodFinder.GetAllInstanceMethods(typeof(IGenInterfaceHierarchyBase<int>)).Length);
423421
}
424422

425423
#if FEATURE_APPDOMAIN

src/Castle.Core.Tests/DynamicProxy.Tests/MethodFinderTestCase.cs

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414

1515
namespace Castle.DynamicProxy.Tests
1616
{
17-
using System;
18-
using System.Collections;
19-
using System.Collections.Generic;
2017
using System.Reflection;
2118

2219
using Castle.DynamicProxy.Generators;
@@ -26,51 +23,14 @@ namespace Castle.DynamicProxy.Tests
2623
[TestFixture]
2724
public class MethodFinderTestCase
2825
{
29-
[Test]
30-
public void GetMethodsForPublic()
31-
{
32-
MethodInfo[] methods =
33-
MethodFinder.GetAllInstanceMethods(typeof(object), BindingFlags.Instance | BindingFlags.Public);
34-
MethodInfo[] realMethods = typeof(object).GetMethods(BindingFlags.Instance | BindingFlags.Public);
35-
CollectionAssert.AreEquivalent(realMethods, methods);
36-
}
37-
38-
[Test]
39-
public void GetMethodsForNonPublic()
40-
{
41-
MethodInfo[] methods =
42-
MethodFinder.GetAllInstanceMethods(typeof(object), BindingFlags.Instance | BindingFlags.NonPublic);
43-
MethodInfo[] realMethods = typeof(object).GetMethods(BindingFlags.Instance | BindingFlags.NonPublic);
44-
CollectionAssert.AreEquivalent(realMethods, methods);
45-
}
46-
4726
[Test]
4827
public void GetMethodsForPublicAndNonPublic()
4928
{
5029
MethodInfo[] methods =
51-
MethodFinder.GetAllInstanceMethods(typeof(object),
52-
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
30+
MethodFinder.GetAllInstanceMethods(typeof(object));
5331
MethodInfo[] realMethods =
5432
typeof(object).GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
5533
CollectionAssert.AreEquivalent(realMethods, methods);
5634
}
57-
58-
[Test]
59-
public void GetMethodsThrowsOnStatic()
60-
{
61-
Assert.Throws<ArgumentException>(() =>
62-
MethodFinder.GetAllInstanceMethods(typeof(object),
63-
BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public)
64-
);
65-
}
66-
67-
[Test]
68-
public void GetMethodsThrowsOnOtherFlags()
69-
{
70-
Assert.Throws<ArgumentException>(() =>
71-
MethodFinder.GetAllInstanceMethods(typeof(object),
72-
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly)
73-
);
74-
}
7535
}
7636
}

src/Castle.Core/DynamicProxy/Contributors/MembersCollector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void CollectEvents()
7272

7373
void CollectMethods()
7474
{
75-
var methodsFound = MethodFinder.GetAllInstanceMethods(type, Flags);
75+
var methodsFound = MethodFinder.GetAllInstanceMethods(type);
7676
foreach (var method in methodsFound)
7777
{
7878
AddMethod(method, true);

src/Castle.Core/DynamicProxy/Generators/MethodFinder.cs

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,14 @@ internal class MethodFinder
2828
private static readonly Dictionary<Type, MethodInfo[]> cachedMethodInfosByType = new Dictionary<Type, MethodInfo[]>();
2929
private static readonly object lockObject = new object();
3030

31-
public static MethodInfo[] GetAllInstanceMethods(Type type, BindingFlags flags)
31+
public static MethodInfo[] GetAllInstanceMethods(Type type)
3232
{
33-
if ((flags & ~(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) != 0)
34-
{
35-
throw new ArgumentException("MethodFinder only supports the Public, NonPublic, and Instance binding flags.", nameof(flags));
36-
}
37-
3833
MethodInfo[] methodsInCache;
3934

4035
lock (lockObject)
4136
{
4237
if (!cachedMethodInfosByType.TryGetValue(type, out methodsInCache))
4338
{
44-
// We always load all instance methods into the cache, we will filter them later
4539
methodsInCache = type.GetMethods(
4640
BindingFlags.Public | BindingFlags.NonPublic
4741
| BindingFlags.Instance)
@@ -52,32 +46,7 @@ public static MethodInfo[] GetAllInstanceMethods(Type type, BindingFlags flags)
5246
methodsInCache);
5347
}
5448
}
55-
return MakeFilteredCopy(methodsInCache, flags & (BindingFlags.Public | BindingFlags.NonPublic));
49+
return methodsInCache;
5650
}
57-
58-
private static MethodInfo[] MakeFilteredCopy(MethodInfo[] methodsInCache, BindingFlags visibilityFlags)
59-
{
60-
if ((visibilityFlags & ~(BindingFlags.Public | BindingFlags.NonPublic)) != 0)
61-
{
62-
throw new ArgumentException("Only supports BindingFlags.Public and NonPublic.", nameof(visibilityFlags));
63-
}
64-
65-
var includePublic = (visibilityFlags & BindingFlags.Public) == BindingFlags.Public;
66-
var includeNonPublic = (visibilityFlags & BindingFlags.NonPublic) == BindingFlags.NonPublic;
67-
68-
// Return a copy of the cached array, only returning the public methods unless requested otherwise
69-
var result = new List<MethodInfo>(methodsInCache.Length);
70-
71-
foreach (var method in methodsInCache)
72-
{
73-
if ((method.IsPublic && includePublic) || (!method.IsPublic && includeNonPublic))
74-
{
75-
result.Add(method);
76-
}
77-
}
78-
79-
return result.ToArray();
80-
}
81-
8251
}
8352
}

src/Castle.Core/DynamicProxy/Internal/InvocationHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ private static MethodInfo ObtainMethod(MethodInfo proxiedMethod, Type type)
7373
else
7474
{
7575
// NOTE: this implementation sucks, feel free to improve it.
76-
var methods = MethodFinder.GetAllInstanceMethods(type, BindingFlags.Public | BindingFlags.NonPublic);
76+
var methods = MethodFinder.GetAllInstanceMethods(type);
7777
foreach (var method in methods)
7878
{
7979
if (MethodSignatureComparer.Instance.Equals(method.GetBaseDefinition(), proxiedMethod))

0 commit comments

Comments
 (0)