|
| 1 | +// Copyright 2004-2026 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 | +#nullable enable |
| 16 | + |
| 17 | +namespace Castle.DynamicProxy.Contributors |
| 18 | +{ |
| 19 | + using System; |
| 20 | + using System.Reflection; |
| 21 | + |
| 22 | + using Castle.DynamicProxy.Generators; |
| 23 | + using Castle.DynamicProxy.Generators.Emitters; |
| 24 | + using Castle.DynamicProxy.Generators.Emitters.SimpleAST; |
| 25 | + |
| 26 | + internal sealed class RecordCloningContributor : ITypeContributor |
| 27 | + { |
| 28 | + private readonly Type targetType; |
| 29 | + private readonly INamingScope namingScope; |
| 30 | + |
| 31 | + private MethodInfo? baseCloneMethod; |
| 32 | + private bool overridable; |
| 33 | + private bool shouldIntercept; |
| 34 | + |
| 35 | + public RecordCloningContributor(Type targetType, INamingScope namingScope) |
| 36 | + { |
| 37 | + this.targetType = targetType; |
| 38 | + this.namingScope = namingScope; |
| 39 | + } |
| 40 | + |
| 41 | + public void CollectElementsToProxy(IProxyGenerationHook hook, MetaType model) |
| 42 | + { |
| 43 | + baseCloneMethod = targetType.GetMethod("<Clone>$", BindingFlags.Public | BindingFlags.Instance); |
| 44 | + if (baseCloneMethod == null) |
| 45 | + { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + var cloneMetaMethod = model.FindMethod(baseCloneMethod); |
| 50 | + if (cloneMetaMethod != null) |
| 51 | + { |
| 52 | + // The target contributor may have chosen to generate interception code for this method. |
| 53 | + // We override that decision here. This effectively renders `<Clone>$` uninterceptable, |
| 54 | + // in favor of some default behavior provided by DynamicProxy. This may be a bad idea. |
| 55 | + cloneMetaMethod.Ignore = true; |
| 56 | + } |
| 57 | + |
| 58 | + overridable = baseCloneMethod.IsVirtual && !baseCloneMethod.IsFinal; |
| 59 | + shouldIntercept = overridable && hook.ShouldInterceptMethod(targetType, baseCloneMethod); |
| 60 | + } |
| 61 | + |
| 62 | + public void Generate(ClassEmitter @class) |
| 63 | + { |
| 64 | + if (baseCloneMethod == null) return; |
| 65 | + |
| 66 | + ImplementCopyConstructor(@class, out var copyCtor); |
| 67 | + ImplementCloneMethod(@class, copyCtor); |
| 68 | + } |
| 69 | + |
| 70 | + private void ImplementCopyConstructor(ClassEmitter @class, out ConstructorInfo copyCtor) |
| 71 | + { |
| 72 | + var other = new ArgumentReference(@class.TypeBuilder); |
| 73 | + var copyCtorEmitter = @class.CreateConstructor(other); |
| 74 | + var baseCopyCtor = targetType.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, [targetType], null); |
| 75 | + |
| 76 | + copyCtorEmitter.CodeBuilder.AddStatement( |
| 77 | + new ConstructorInvocationStatement( |
| 78 | + baseCopyCtor, |
| 79 | + other)); |
| 80 | + |
| 81 | + foreach (var field in @class.GetAllFields()) |
| 82 | + { |
| 83 | + if (field.Reference.IsStatic) continue; |
| 84 | + |
| 85 | + copyCtorEmitter.CodeBuilder.AddStatement( |
| 86 | + new AssignStatement( |
| 87 | + field, |
| 88 | + new FieldReference( |
| 89 | + field.Reference, |
| 90 | + other))); |
| 91 | + } |
| 92 | + |
| 93 | + copyCtorEmitter.CodeBuilder.AddStatement(ReturnStatement.Instance); |
| 94 | + |
| 95 | + copyCtor = copyCtorEmitter.ConstructorBuilder; |
| 96 | + } |
| 97 | + |
| 98 | + private void ImplementCloneMethod(ClassEmitter @class, ConstructorInfo copyCtor) |
| 99 | + { |
| 100 | + if (shouldIntercept) |
| 101 | + { |
| 102 | + var cloneCallbackMethod = CreateCallbackMethod(@class, copyCtor); |
| 103 | + var cloneMetaMethod = new MetaMethod(baseCloneMethod!, cloneCallbackMethod, true, true, true); |
| 104 | + var invocationType = CreateInvocationType(@class, cloneMetaMethod, cloneCallbackMethod); |
| 105 | + |
| 106 | + var cloneMethodGenerator = new MethodWithInvocationGenerator( |
| 107 | + cloneMetaMethod, |
| 108 | + @class.GetField("__interceptors"), |
| 109 | + invocationType, |
| 110 | + (c, m) => new TypeTokenExpression(@class.TypeBuilder), |
| 111 | + @class.CreateMethod, |
| 112 | + null); |
| 113 | + |
| 114 | + cloneMethodGenerator.Generate(@class, namingScope); |
| 115 | + } |
| 116 | + else if (overridable) |
| 117 | + { |
| 118 | + var cloneMethodEmitter = @class.CreateMethod( |
| 119 | + name: baseCloneMethod!.Name, |
| 120 | + attrs: (baseCloneMethod.Attributes & MethodAttributes.MemberAccessMask) | MethodAttributes.ReuseSlot | MethodAttributes.HideBySig | MethodAttributes.Virtual, |
| 121 | + returnType: baseCloneMethod.ReturnType, // no need to use covariant return type |
| 122 | + argumentTypes: []); |
| 123 | + |
| 124 | + cloneMethodEmitter.CodeBuilder.AddStatement( |
| 125 | + new ReturnStatement( |
| 126 | + new NewInstanceExpression( |
| 127 | + copyCtor, |
| 128 | + ThisExpression.Instance))); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private MethodInfo CreateCallbackMethod(ClassEmitter @class, ConstructorInfo copyCtor) |
| 133 | + { |
| 134 | + var callbackMethod = @class.CreateMethod( |
| 135 | + name: baseCloneMethod!.Name + "_callback", |
| 136 | + attrs: MethodAttributes.Public | MethodAttributes.HideBySig, |
| 137 | + returnType: copyCtor.DeclaringType, |
| 138 | + argumentTypes: Type.EmptyTypes); |
| 139 | + |
| 140 | + callbackMethod.CodeBuilder.AddStatement( |
| 141 | + new ReturnStatement( |
| 142 | + new NewInstanceExpression( |
| 143 | + copyCtor, |
| 144 | + ThisExpression.Instance))); |
| 145 | + |
| 146 | + return callbackMethod.MethodBuilder; |
| 147 | + } |
| 148 | + |
| 149 | + private Type CreateInvocationType(ClassEmitter @class, MetaMethod cloneMetaMethod, MethodInfo cloneCallbackMethod) |
| 150 | + { |
| 151 | + var generator = new InheritanceInvocationTypeGenerator( |
| 152 | + targetType, |
| 153 | + cloneMetaMethod, |
| 154 | + cloneCallbackMethod, |
| 155 | + null); |
| 156 | + |
| 157 | + return generator.Generate(@class, namingScope).BuildType(); |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments