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
Changes from 1 commit
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
17 changes: 1 addition & 16 deletions src/Castle.Core/DynamicProxy/Contributors/MembersCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,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 @@ -205,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 @@ -223,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;
}
}
}