Skip to content

Commit 4b85681

Browse files
authored
Merge pull request castleproject#621 from stakx/bugfix/sealed-interface-methods
Fix `MissingMethodException` when proxying interfaces containing sealed methods
2 parents a133ee3 + 4c5e227 commit 4b85681

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Enhancements:
99
Bugfixes:
1010
- DynamicProxy emits invalid metadata for redeclared event (@stakx, #590)
1111
- Proxies using records with a base class broken using .NET 6 compiler (@ajcvickers, #601)
12+
- `MissingMethodException` when proxying interfaces containing sealed methods (@stakx, #621)
1213

1314
## 5.0.0 (2022-05-11)
1415

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2004-2022 Castle Project - http://www.castleproject.org/
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#if NETCOREAPP3_0_OR_GREATER
16+
17+
using System.Reflection;
18+
19+
using NUnit.Framework;
20+
21+
namespace Castle.DynamicProxy.Tests
22+
{
23+
[TestFixture]
24+
public class DefaultInterfaceMembersTestCase : BasePEVerifyTestCase
25+
{
26+
[Test]
27+
public void Can_proxy_interface_with_sealed_method()
28+
{
29+
_ = generator.CreateInterfaceProxyWithoutTarget<IHaveSealedMethod>();
30+
}
31+
32+
[Test]
33+
public void Can_invoke_sealed_method_in_proxied_interface()
34+
{
35+
var proxy = generator.CreateInterfaceProxyWithoutTarget<IHaveSealedMethod>();
36+
var invokedMethod = proxy.SealedMethod();
37+
Assert.AreEqual(typeof(IHaveSealedMethod).GetMethod(nameof(IHaveSealedMethod.SealedMethod)), invokedMethod);
38+
}
39+
40+
public interface IHaveSealedMethod
41+
{
42+
sealed MethodBase SealedMethod()
43+
{
44+
return MethodBase.GetCurrentMethod();
45+
}
46+
}
47+
}
48+
}
49+
50+
#endif

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ protected override MetaMethod GetMethodToGenerate(MethodInfo method, IProxyGener
3333
return null;
3434
}
3535

36-
var proxyable = AcceptMethod(method, false, hook);
36+
var proxyable = AcceptMethod(method, true, hook);
37+
if (!proxyable && !method.IsAbstract)
38+
{
39+
// we don't need to do anything
40+
return null;
41+
}
42+
3743
return new MetaMethod(method, method, isStandalone, proxyable, false);
3844
}
3945
}

0 commit comments

Comments
 (0)