Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove redundant method accessibility checks & optimize ProxyUtil.IsAccessibleMethod #666

Merged
merged 3 commits into from
Sep 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ public ClassMembersCollector(Type targetType)

protected override MetaMethod GetMethodToGenerate(MethodInfo method, IProxyGenerationHook hook, bool isStandalone)
{
if (ProxyUtil.IsAccessibleMethod(method) == false)
{
return null;
}

var accepted = AcceptMethod(method, true, hook);
if (!accepted && !method.IsAbstract)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ public InterfaceMembersCollector(Type @interface)

protected override MetaMethod GetMethodToGenerate(MethodInfo method, IProxyGenerationHook hook, bool isStandalone)
{
if (ProxyUtil.IsAccessibleMethod(method) == false)
{
return null;
}

var proxyable = AcceptMethod(method, true, hook);
if (!proxyable && !method.IsAbstract)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ public InterfaceMembersOnClassCollector(Type type, bool onlyProxyVirtual, Interf

protected override MetaMethod GetMethodToGenerate(MethodInfo method, IProxyGenerationHook hook, bool isStandalone)
{
if (ProxyUtil.IsAccessibleMethod(method) == false)
{
return null;
}

if (onlyProxyVirtual && IsVirtuallyImplementedInterfaceMethod(method))
{
return null;
Expand Down
22 changes: 6 additions & 16 deletions src/Castle.Core/DynamicProxy/Contributors/MembersCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ MetaMethod AddMethod(MethodInfo method, bool isStandalone)
return null;
}

if (ProxyUtil.IsAccessibleMethod(method) == false)
{
return null;
}

var methodToGenerate = GetMethodToGenerate(method, hook, isStandalone);
if (methodToGenerate != null)
{
Expand Down Expand Up @@ -173,10 +178,7 @@ protected bool AcceptMethod(MethodInfo method, bool onlyVirtuals, IProxyGenerati
/// </remarks>
protected bool AcceptMethodPreScreen(MethodInfo method, bool onlyVirtuals, IProxyGenerationHook hook)
{
if (IsInternalAndNotVisibleToDynamicProxy(method))
{
return false;
}
// NOTE: at this point, the method's accessibility should already have been checked (see `AddMethod` above)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only code execution paths leading here look like this:

AddMethod → the various collectors' GetMethodToGenerate methods ( → AcceptMethod ) → AcceptMethodPreScreen


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

//can only proxy methods that are public or protected (or internals that have already been checked above)
if ((method.IsPublic || method.IsFamily || method.IsAssembly || method.IsFamilyOrAssembly || method.IsFamilyAndAssembly) == false)
{
return false;
}

if (method.DeclaringType == typeof(MarshalByRefObject))
{
return false;
Expand All @@ -218,11 +214,5 @@ protected bool AcceptMethodPreScreen(MethodInfo method, bool onlyVirtuals, IProx

return true;
}

private static bool IsInternalAndNotVisibleToDynamicProxy(MethodInfo method)
{
return ProxyUtil.IsInternal(method) &&
ProxyUtil.AreInternalsVisibleToDynamicProxy(method.DeclaringType.Assembly) == false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ public override void CollectMembersToProxy(IProxyGenerationHook hook, IMembersCo

protected override MetaMethod GetMethodToGenerate(MethodInfo method, IProxyGenerationHook hook, bool isStandalone)
{
if (ProxyUtil.IsAccessibleMethod(method) == false)
{
return null;
}

var interceptable = AcceptMethodPreScreen(method, true, hook);
if (!interceptable)
{
Expand Down
19 changes: 11 additions & 8 deletions src/Castle.Core/DynamicProxy/ProxyUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,20 @@ internal static bool IsAccessibleType(Type target)
/// <returns><c>true</c> if the method is accessible to DynamicProxy, <c>false</c> otherwise.</returns>
internal static bool IsAccessibleMethod(MethodBase method)
{
if (method.IsPublic || method.IsFamily || method.IsFamilyOrAssembly)
switch (method.Attributes & MethodAttributes.MemberAccessMask)
{
return true;
}
case MethodAttributes.Assembly:
case MethodAttributes.FamANDAssem:
return AreInternalsVisibleToDynamicProxy(method.DeclaringType.Assembly);

if (method.IsAssembly || method.IsFamilyAndAssembly)
{
return AreInternalsVisibleToDynamicProxy(method.DeclaringType.Assembly);
}
case MethodAttributes.Family:
case MethodAttributes.FamORAssem:
case MethodAttributes.Public:
return true;

return false;
default: // `MethodAttributes.Private` or `MethodAttributes.PrivateScope`
return false;
}
}

/// <summary>
Expand Down