Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Bugfixes:
- `InvalidProgramException` when proxying `MemoryStream` with .NET 7 (@stakx, #651)
- `invocation.MethodInvocationTarget` throws `ArgumentNullException` for default interface method (@stakx, #684)
- `DynamicProxyException` ("duplicate element") when type to proxy contains members whose names differ only in case (@stakx, #691)
- `AmbiguousMatchException` when using a proxy generation hook that is implemented as a `record class` (@stakx, #720)

## 5.2.1 (2025-03-09)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace Castle.DynamicProxy.Tests
{
using System;
using System.Reflection;

using Castle.DynamicProxy.Tests.Interceptors;
using Castle.DynamicProxy.Tests.Interfaces;
Expand All @@ -29,6 +30,47 @@ public class ProxyTypeCachingWithDifferentHooksTestCase : BasePEVerifyTestCase
#endif
public class CustomHook : AllMethodsHook { }

#if FEATURE_SERIALIZATION
[Serializable]
#endif
public class EquatableHook : IProxyGenerationHook, IEquatable<EquatableHook>
{
public override bool Equals(object obj) => Equals(obj as EquatableHook);
public override int GetHashCode() => GetType().GetHashCode();

public bool Equals(EquatableHook other) => other is EquatableHook;

public void MethodsInspected() { }
public void NonProxyableMemberNotification(Type type, MemberInfo memberInfo) { }
public bool ShouldInterceptMethod(Type type, MethodInfo methodInfo) => false;
}

#if FEATURE_SERIALIZATION
[Serializable]
#endif
public record class RecordClassHook : IProxyGenerationHook
{
public RecordClassHook(string id)
{
Id = id;
}

public string Id { get; }

public void MethodsInspected() { }
public void NonProxyableMemberNotification(Type type, MemberInfo memberInfo) { }
public bool ShouldInterceptMethod(Type type, MethodInfo methodInfo) => false;
}

// `IEquatable<>` can lead to there being more than one `Equals` method.
// This is relevant for hook classes because DynamicProxy checks whether they
// override the inherited `Equals` method for proper equality semantics.
[Test]
public void Can_use_hook_that_implements_IEquatable()
{
_ = CreateProxyWithHook<EquatableHook>();
}

[Test]
public void Proxies_with_same_hook_will_use_cached_proxy_type()
{
Expand All @@ -45,9 +87,42 @@ public void Proxies_with_different_hooks_will_use_different_proxy_types()
Assert.AreNotEqual(first.GetType(), second.GetType());
}

[Test]
public void Proxies_with_different_but_equal_record_class_hooks_will_use_cached_proxy_type()
{
var firstHook = new RecordClassHook("1");
var secondHook = new RecordClassHook("1");
Assume.That(firstHook, Is.Not.SameAs(secondHook));
Assume.That(firstHook, Is.EqualTo(secondHook));

var first = CreateProxyWithHook(firstHook);
var second = CreateProxyWithHook(secondHook);

Assert.AreEqual(first.GetType(), second.GetType());
}

[Test]
public void Proxies_with_different_and_unequal_record_class_hooks_will_use_different_proxy_types()
{
var firstHook = new RecordClassHook("1");
var secondHook = new RecordClassHook("2");
Assume.That(firstHook, Is.Not.SameAs(secondHook));
Assume.That(firstHook, Is.Not.EqualTo(secondHook));

var first = CreateProxyWithHook(firstHook);
var second = CreateProxyWithHook(secondHook);

Assert.AreNotEqual(first.GetType(), second.GetType());
}

private object CreateProxyWithHook<THook>() where THook : IProxyGenerationHook, new()
{
return generator.CreateInterfaceProxyWithoutTarget(typeof(IEmpty), new ProxyGenerationOptions(new THook()), new DoNothingInterceptor());
return CreateProxyWithHook(new THook());
}

private object CreateProxyWithHook(IProxyGenerationHook hook)
{
return generator.CreateInterfaceProxyWithoutTarget(typeof(IEmpty), new ProxyGenerationOptions(hook), new DoNothingInterceptor());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ protected void InitializeStaticFields(Type builtType)

private bool OverridesEqualsAndGetHashCode(Type type)
{
var equalsMethod = type.GetMethod("Equals", BindingFlags.Public | BindingFlags.Instance);
var equalsMethod = type.GetMethod("Equals", BindingFlags.Public | BindingFlags.Instance, null, [ typeof(object) ], null);
if (equalsMethod == null || equalsMethod.DeclaringType == typeof(object) || equalsMethod.IsAbstract)
{
return false;
Expand Down
Loading