Skip to content

Commit cdc0c26

Browse files
committed
Only synthesize <Clone>$ for record proxies w/o target
1 parent 2adb7ca commit cdc0c26

3 files changed

Lines changed: 29 additions & 7 deletions

File tree

src/Castle.Core/DynamicProxy/Generators/BaseClassProxyGenerator.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#nullable enable
16+
1517
namespace Castle.DynamicProxy.Generators
1618
{
1719
using System;
@@ -31,7 +33,7 @@ protected BaseClassProxyGenerator(ModuleScope scope, Type targetType, Type[] int
3133
EnsureDoesNotImplementIProxyTargetAccessor(targetType, nameof(targetType));
3234
}
3335

34-
protected abstract FieldReference TargetField { get; }
36+
protected abstract FieldReference? TargetField { get; }
3537

3638
#if FEATURE_SERIALIZATION
3739
protected abstract SerializableContributor GetSerializableContributor();
@@ -41,6 +43,8 @@ protected BaseClassProxyGenerator(ModuleScope scope, Type targetType, Type[] int
4143

4244
protected abstract ProxyTargetAccessorContributor GetProxyTargetAccessorContributor();
4345

46+
protected abstract RecordCloningContributor? GetRecordCloningContributor(INamingScope namingScope);
47+
4448
protected sealed override Type GenerateType(string name, INamingScope namingScope)
4549
{
4650
IEnumerable<ITypeContributor> contributors;
@@ -192,7 +196,11 @@ private IEnumerable<Type> GetTypeImplementerMapping(out IEnumerable<ITypeContrib
192196
}
193197
#endif
194198

195-
contributorsList.Add(new RecordCloningContributor(targetType, namingScope));
199+
var recordCloningContributor = GetRecordCloningContributor(namingScope);
200+
if (recordCloningContributor != null)
201+
{
202+
contributorsList.Add(recordCloningContributor);
203+
}
196204

197205
var proxyTargetAccessorContributor = GetProxyTargetAccessorContributor();
198206
contributorsList.Add(proxyTargetAccessorContributor);

src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2004-2025 Castle Project - http://www.castleproject.org/
1+
// Copyright 2004-2026 Castle Project - http://www.castleproject.org/
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#nullable enable
16+
1517
namespace Castle.DynamicProxy.Generators
1618
{
1719
using System;
@@ -29,7 +31,7 @@ public ClassProxyGenerator(ModuleScope scope, Type targetType, Type[] interfaces
2931
{
3032
}
3133

32-
protected override FieldReference TargetField => null;
34+
protected override FieldReference? TargetField => null;
3335

3436
protected override CacheKey GetCacheKey()
3537
{
@@ -54,5 +56,10 @@ protected override ProxyTargetAccessorContributor GetProxyTargetAccessorContribu
5456
getTarget: () => ThisExpression.Instance,
5557
targetType);
5658
}
59+
60+
protected override RecordCloningContributor? GetRecordCloningContributor(INamingScope namingScope)
61+
{
62+
return new RecordCloningContributor(targetType, namingScope);
63+
}
5764
}
5865
}

src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2004-2025 Castle Project - http://www.castleproject.org/
1+
// Copyright 2004-2026 Castle Project - http://www.castleproject.org/
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#nullable enable
16+
1517
namespace Castle.DynamicProxy.Generators
1618
{
1719
using System;
@@ -29,15 +31,15 @@ namespace Castle.DynamicProxy.Generators
2931

3032
internal sealed class ClassProxyWithTargetGenerator : BaseClassProxyGenerator
3133
{
32-
private FieldReference targetField;
34+
private FieldReference? targetField;
3335

3436
public ClassProxyWithTargetGenerator(ModuleScope scope, Type targetType, Type[] interfaces,
3537
ProxyGenerationOptions options)
3638
: base(scope, targetType, interfaces, options)
3739
{
3840
}
3941

40-
protected override FieldReference TargetField => targetField;
42+
protected override FieldReference? TargetField => targetField;
4143

4244
protected override CacheKey GetCacheKey()
4345
{
@@ -69,6 +71,11 @@ protected override ProxyTargetAccessorContributor GetProxyTargetAccessorContribu
6971
targetType);
7072
}
7173

74+
protected override RecordCloningContributor? GetRecordCloningContributor(INamingScope namingScope)
75+
{
76+
return null;
77+
}
78+
7279
private void CreateTargetField(ClassEmitter emitter)
7380
{
7481
targetField = emitter.CreateField("__target", targetType);

0 commit comments

Comments
 (0)