Skip to content

Commit 222b1b8

Browse files
authored
Merge pull request castleproject#666 from stakx/refactor/accessibility-check
Remove redundant method accessibility checks & optimize `ProxyUtil.IsAccessibleMethod`
2 parents 57e666d + c662c90 commit 222b1b8

File tree

6 files changed

+17
-44
lines changed

6 files changed

+17
-44
lines changed

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ public ClassMembersCollector(Type targetType)
2828

2929
protected override MetaMethod GetMethodToGenerate(MethodInfo method, IProxyGenerationHook hook, bool isStandalone)
3030
{
31-
if (ProxyUtil.IsAccessibleMethod(method) == false)
32-
{
33-
return null;
34-
}
35-
3631
var accepted = AcceptMethod(method, true, hook);
3732
if (!accepted && !method.IsAbstract)
3833
{

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ public InterfaceMembersCollector(Type @interface)
2828

2929
protected override MetaMethod GetMethodToGenerate(MethodInfo method, IProxyGenerationHook hook, bool isStandalone)
3030
{
31-
if (ProxyUtil.IsAccessibleMethod(method) == false)
32-
{
33-
return null;
34-
}
35-
3631
var proxyable = AcceptMethod(method, true, hook);
3732
if (!proxyable && !method.IsAbstract)
3833
{

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ public InterfaceMembersOnClassCollector(Type type, bool onlyProxyVirtual, Interf
3232

3333
protected override MetaMethod GetMethodToGenerate(MethodInfo method, IProxyGenerationHook hook, bool isStandalone)
3434
{
35-
if (ProxyUtil.IsAccessibleMethod(method) == false)
36-
{
37-
return null;
38-
}
39-
4035
if (onlyProxyVirtual && IsVirtuallyImplementedInterfaceMethod(method))
4136
{
4237
return null;

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

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ MetaMethod AddMethod(MethodInfo method, bool isStandalone)
143143
return null;
144144
}
145145

146+
if (ProxyUtil.IsAccessibleMethod(method) == false)
147+
{
148+
return null;
149+
}
150+
146151
var methodToGenerate = GetMethodToGenerate(method, hook, isStandalone);
147152
if (methodToGenerate != null)
148153
{
@@ -173,10 +178,7 @@ protected bool AcceptMethod(MethodInfo method, bool onlyVirtuals, IProxyGenerati
173178
/// </remarks>
174179
protected bool AcceptMethodPreScreen(MethodInfo method, bool onlyVirtuals, IProxyGenerationHook hook)
175180
{
176-
if (IsInternalAndNotVisibleToDynamicProxy(method))
177-
{
178-
return false;
179-
}
181+
// NOTE: at this point, the method's accessibility should already have been checked (see `AddMethod` above)
180182

181183
var isOverridable = method.IsVirtual && !method.IsFinal;
182184
if (onlyVirtuals && !isOverridable)
@@ -200,12 +202,6 @@ protected bool AcceptMethodPreScreen(MethodInfo method, bool onlyVirtuals, IProx
200202
return false;
201203
}
202204

203-
//can only proxy methods that are public or protected (or internals that have already been checked above)
204-
if ((method.IsPublic || method.IsFamily || method.IsAssembly || method.IsFamilyOrAssembly || method.IsFamilyAndAssembly) == false)
205-
{
206-
return false;
207-
}
208-
209205
if (method.DeclaringType == typeof(MarshalByRefObject))
210206
{
211207
return false;
@@ -218,11 +214,5 @@ protected bool AcceptMethodPreScreen(MethodInfo method, bool onlyVirtuals, IProx
218214

219215
return true;
220216
}
221-
222-
private static bool IsInternalAndNotVisibleToDynamicProxy(MethodInfo method)
223-
{
224-
return ProxyUtil.IsInternal(method) &&
225-
ProxyUtil.AreInternalsVisibleToDynamicProxy(method.DeclaringType.Assembly) == false;
226-
}
227217
}
228218
}

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ public override void CollectMembersToProxy(IProxyGenerationHook hook, IMembersCo
3737

3838
protected override MetaMethod GetMethodToGenerate(MethodInfo method, IProxyGenerationHook hook, bool isStandalone)
3939
{
40-
if (ProxyUtil.IsAccessibleMethod(method) == false)
41-
{
42-
return null;
43-
}
44-
4540
var interceptable = AcceptMethodPreScreen(method, true, hook);
4641
if (!interceptable)
4742
{

src/Castle.Core/DynamicProxy/ProxyUtil.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,17 +194,20 @@ internal static bool IsAccessibleType(Type target)
194194
/// <returns><c>true</c> if the method is accessible to DynamicProxy, <c>false</c> otherwise.</returns>
195195
internal static bool IsAccessibleMethod(MethodBase method)
196196
{
197-
if (method.IsPublic || method.IsFamily || method.IsFamilyOrAssembly)
197+
switch (method.Attributes & MethodAttributes.MemberAccessMask)
198198
{
199-
return true;
200-
}
199+
case MethodAttributes.Assembly:
200+
case MethodAttributes.FamANDAssem:
201+
return AreInternalsVisibleToDynamicProxy(method.DeclaringType.Assembly);
201202

202-
if (method.IsAssembly || method.IsFamilyAndAssembly)
203-
{
204-
return AreInternalsVisibleToDynamicProxy(method.DeclaringType.Assembly);
205-
}
203+
case MethodAttributes.Family:
204+
case MethodAttributes.FamORAssem:
205+
case MethodAttributes.Public:
206+
return true;
206207

207-
return false;
208+
default: // `MethodAttributes.Private` or `MethodAttributes.PrivateScope`
209+
return false;
210+
}
208211
}
209212

210213
/// <summary>

0 commit comments

Comments
 (0)