forked from castleproject/Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Collect interface methods with default implementations
A class implementing an interface with default method implementations will not necessarily override those methods; in such cases, the methods are currently "invisible" to DynamicProxy and thus won't get proxied. This can be solved by an extra pass over all interfaces to collect such methods, as is done in this commit. We should next check whether the same work can be performed in the pre- existing infrastructure, so that we don't need to iterate over all interfaces more than once.
- Loading branch information
Showing
2 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...stle.Core/DynamicProxy/Contributors/InterfaceMembersWithDefaultImplementationCollector.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2004-2022 Castle Project - http://www.castleproject.org/ | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
namespace Castle.DynamicProxy.Contributors | ||
{ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Reflection; | ||
|
||
using Castle.DynamicProxy.Generators; | ||
|
||
internal sealed class InterfaceMembersWithDefaultImplementationCollector : MembersCollector | ||
{ | ||
private readonly InterfaceMapping map; | ||
|
||
public InterfaceMembersWithDefaultImplementationCollector(Type interfaceType, Type classToProxy) | ||
: base(interfaceType) | ||
{ | ||
Debug.Assert(interfaceType != null); | ||
Debug.Assert(interfaceType.IsInterface); | ||
|
||
Debug.Assert(classToProxy != null); | ||
Debug.Assert(classToProxy.IsClass); | ||
|
||
map = classToProxy.GetInterfaceMap(interfaceType); | ||
} | ||
|
||
protected override MetaMethod GetMethodToGenerate(MethodInfo method, IProxyGenerationHook hook, bool isStandalone) | ||
{ | ||
var index = Array.IndexOf(map.InterfaceMethods, method); | ||
Debug.Assert(index >= 0); | ||
|
||
var methodOnTarget = map.TargetMethods[index]; | ||
if (methodOnTarget.DeclaringType.IsInterface == false) | ||
{ | ||
return null; | ||
} | ||
|
||
var proxyable = AcceptMethod(method, true, hook); | ||
if (!proxyable) | ||
{ | ||
return null; | ||
} | ||
|
||
return new MetaMethod(method, methodOnTarget, true, proxyable, !method.IsAbstract); | ||
} | ||
} | ||
} |