From 6294ad5ad4160078e3cd6aeefe8afe6b91b9c9bf Mon Sep 17 00:00:00 2001 From: Dominique Schuppli Date: Tue, 16 Dec 2025 23:04:22 +0100 Subject: [PATCH 1/4] Downgrade `MethodInvocationExpression.owner` to an expression & rename it --- .../Emitters/SimpleAST/MethodInvocationExpression.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/MethodInvocationExpression.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/MethodInvocationExpression.cs index 55c7ea067..7491258ea 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/MethodInvocationExpression.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/MethodInvocationExpression.cs @@ -23,7 +23,7 @@ internal class MethodInvocationExpression : IExpression, IStatement { protected readonly IExpression[] args; protected readonly MethodInfo method; - protected readonly Reference? owner; + protected readonly IExpression? instance; public MethodInvocationExpression(MethodInfo method, params IExpression[] args) : this(SelfReference.Self, method, args) @@ -35,14 +35,14 @@ public MethodInvocationExpression(MethodEmitter method, params IExpression[] arg { } - public MethodInvocationExpression(Reference? owner, MethodEmitter method, params IExpression[] args) : - this(owner, method.MethodBuilder, args) + public MethodInvocationExpression(IExpression? instance, MethodEmitter method, params IExpression[] args) + : this(instance, method.MethodBuilder, args) { } - public MethodInvocationExpression(Reference? owner, MethodInfo method, params IExpression[] args) + public MethodInvocationExpression(IExpression? instance, MethodInfo method, params IExpression[] args) { - this.owner = owner; + this.instance = instance; this.method = method; this.args = args; } @@ -51,7 +51,7 @@ public MethodInvocationExpression(Reference? owner, MethodInfo method, params IE public void Emit(ILGenerator gen) { - owner?.Emit(gen); + instance?.Emit(gen); foreach (var exp in args) { From ee07fdba248c0ff280b3a55b67c302d9982938ce Mon Sep 17 00:00:00 2001 From: Dominique Schuppli Date: Tue, 16 Dec 2025 23:23:15 +0100 Subject: [PATCH 2/4] `ProxyTargetAccessorContributor.getTarget` does not need a reference --- .../Contributors/ProxyTargetAccessorContributor.cs | 14 +++++++------- .../DynamicProxy/Generators/ClassProxyGenerator.cs | 4 ++-- .../Generators/ClassProxyWithTargetGenerator.cs | 4 ++-- .../InterfaceProxyWithTargetGenerator.cs | 4 ++-- .../InterfaceProxyWithTargetInterfaceGenerator.cs | 4 ++-- .../InterfaceProxyWithoutTargetGenerator.cs | 4 ++-- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/Castle.Core/DynamicProxy/Contributors/ProxyTargetAccessorContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/ProxyTargetAccessorContributor.cs index fbdfbc0b5..55711a2ab 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/ProxyTargetAccessorContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/ProxyTargetAccessorContributor.cs @@ -1,4 +1,4 @@ -// Copyright 2004-2021 Castle Project - http://www.castleproject.org/ +// Copyright 2004-2025 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. @@ -25,12 +25,12 @@ namespace Castle.DynamicProxy.Contributors /// internal sealed class ProxyTargetAccessorContributor : ITypeContributor { - private readonly Func getTargetReference; + private readonly Func getTarget; private readonly Type targetType; - public ProxyTargetAccessorContributor(Func getTargetReference, Type targetType) + public ProxyTargetAccessorContributor(Func getTarget, Type targetType) { - this.getTargetReference = getTargetReference; + this.getTarget = getTarget; this.targetType = targetType; } @@ -41,17 +41,17 @@ public void CollectElementsToProxy(IProxyGenerationHook hook, MetaType model) public void Generate(ClassEmitter emitter) { var interceptorsField = emitter.GetField("__interceptors"); - var targetReference = getTargetReference(); + var target = getTarget(); var dynProxyGetTarget = emitter.CreateMethod(nameof(IProxyTargetAccessor.DynProxyGetTarget), typeof(object)); dynProxyGetTarget.CodeBuilder.AddStatement( - new ReturnStatement(new ConvertExpression(typeof(object), targetType, targetReference))); + new ReturnStatement(new ConvertExpression(typeof(object), targetType, target))); var dynProxySetTarget = emitter.CreateMethod(nameof(IProxyTargetAccessor.DynProxySetTarget), typeof(void), typeof(object)); // we can only change the target of the interface proxy - if (targetReference is FieldReference targetField) + if (target is FieldReference targetField) { dynProxySetTarget.CodeBuilder.AddStatement( new AssignStatement(targetField, diff --git a/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs index 7d713f15d..fed3cd8ca 100644 --- a/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs @@ -1,4 +1,4 @@ -// Copyright 2004-2021 Castle Project - http://www.castleproject.org/ +// Copyright 2004-2025 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. @@ -51,7 +51,7 @@ protected override CompositeTypeContributor GetProxyTargetContributor(INamingSco protected override ProxyTargetAccessorContributor GetProxyTargetAccessorContributor() { return new ProxyTargetAccessorContributor( - getTargetReference: () => SelfReference.Self, + getTarget: () => SelfReference.Self, targetType); } } diff --git a/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs index c7f6fe6ad..bf8299d64 100644 --- a/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs @@ -1,4 +1,4 @@ -// Copyright 2004-2021 Castle Project - http://www.castleproject.org/ +// Copyright 2004-2025 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. @@ -65,7 +65,7 @@ protected override CompositeTypeContributor GetProxyTargetContributor(INamingSco protected override ProxyTargetAccessorContributor GetProxyTargetAccessorContributor() { return new ProxyTargetAccessorContributor( - getTargetReference: () => targetField, + getTarget: () => targetField, targetType); } diff --git a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs index dc4ac91c4..f19c987fc 100644 --- a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs @@ -1,4 +1,4 @@ -// Copyright 2004-2021 Castle Project - http://www.castleproject.org/ +// Copyright 2004-2025 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. @@ -40,7 +40,7 @@ protected override CompositeTypeContributor GetProxyTargetContributor(Type proxy protected override ProxyTargetAccessorContributor GetProxyTargetAccessorContributor() { return new ProxyTargetAccessorContributor( - getTargetReference: () => targetField, + getTarget: () => targetField, proxyTargetType); } diff --git a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetInterfaceGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetInterfaceGenerator.cs index 9ae7b102c..f0f6d2a2b 100644 --- a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetInterfaceGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetInterfaceGenerator.cs @@ -1,4 +1,4 @@ -// Copyright 2004-2021 Castle Project - http://www.castleproject.org/ +// Copyright 2004-2025 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. @@ -44,7 +44,7 @@ protected override CompositeTypeContributor GetProxyTargetContributor(Type proxy protected override ProxyTargetAccessorContributor GetProxyTargetAccessorContributor() { return new ProxyTargetAccessorContributor( - getTargetReference: () => targetField, + getTarget: () => targetField, proxyTargetType); } diff --git a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithoutTargetGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithoutTargetGenerator.cs index d916e4dc6..26313d6e8 100644 --- a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithoutTargetGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithoutTargetGenerator.cs @@ -1,4 +1,4 @@ -// Copyright 2004-2021 Castle Project - http://www.castleproject.org/ +// Copyright 2004-2025 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. @@ -41,7 +41,7 @@ protected override CompositeTypeContributor GetProxyTargetContributor(Type proxy protected override ProxyTargetAccessorContributor GetProxyTargetAccessorContributor() { return new ProxyTargetAccessorContributor( - getTargetReference: () => targetField, + getTarget: () => targetField, proxyTargetType); } From d2d2d96f37261672a517bdd4e54b9c6d57e1fdc7 Mon Sep 17 00:00:00 2001 From: Dominique Schuppli Date: Tue, 16 Dec 2025 23:28:34 +0100 Subject: [PATCH 3/4] Downgrade `SelfReference` to an expression & rename it --- .../ClassProxySerializableContributor.cs | 4 ++-- .../ClassProxyTargetContributor.cs | 4 ++-- .../InterfaceProxyWithoutTargetContributor.cs | 4 ++-- .../Generators/ClassProxyGenerator.cs | 2 +- .../CompositionInvocationTypeGenerator.cs | 4 ++-- .../Emitters/SimpleAST/FieldReference.cs | 6 +++--- .../SimpleAST/MethodInvocationExpression.cs | 4 ++-- .../{SelfReference.cs => ThisExpression.cs} | 19 ++++--------------- .../Generators/InvocationTypeGenerator.cs | 10 +++++----- .../MethodWithInvocationGenerator.cs | 4 ++-- 10 files changed, 25 insertions(+), 36 deletions(-) rename src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/{SelfReference.cs => ThisExpression.cs} (65%) diff --git a/src/Castle.Core/DynamicProxy/Contributors/ClassProxySerializableContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/ClassProxySerializableContributor.cs index d5ba7a8bd..70cd81689 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/ClassProxySerializableContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/ClassProxySerializableContributor.cs @@ -1,4 +1,4 @@ -// Copyright 2004-2021 Castle Project - http://www.castleproject.org/ +// Copyright 2004-2025 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. @@ -129,7 +129,7 @@ private void EmitCustomGetObjectData(CodeBuilder codeBuilder, ArgumentReference var getObjectData = new MethodInvocationExpression( null, FormatterServicesMethods.GetObjectData, - SelfReference.Self, + ThisExpression.Instance, members); codeBuilder.AddStatement(new AssignStatement(data, getObjectData)); diff --git a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyTargetContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyTargetContributor.cs index e60b4d08d..20da243d3 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyTargetContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyTargetContributor.cs @@ -1,4 +1,4 @@ -// Copyright 2004-2021 Castle Project - http://www.castleproject.org/ +// Copyright 2004-2025 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. @@ -122,7 +122,7 @@ private MethodBuilder CreateCallbackMethod(ClassEmitter emitter, MethodInfo meth callBackMethod.CodeBuilder.AddStatement( new ReturnStatement( - new MethodInvocationExpression(SelfReference.Self, + new MethodInvocationExpression(ThisExpression.Instance, targetMethod, callBackMethod.Arguments))); diff --git a/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyWithoutTargetContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyWithoutTargetContributor.cs index d450b39c4..aa465956d 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyWithoutTargetContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyWithoutTargetContributor.cs @@ -1,4 +1,4 @@ -// Copyright 2004-2021 Castle Project - http://www.castleproject.org/ +// Copyright 2004-2025 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. @@ -119,7 +119,7 @@ private MethodInfo CreateCallbackMethod(ClassEmitter emitter, MethodInfo methodI callBackMethod.CodeBuilder.AddStatement( new ReturnStatement( - new MethodInvocationExpression(SelfReference.Self, targetMethod, callBackMethod.Arguments))); + new MethodInvocationExpression(ThisExpression.Instance, targetMethod, callBackMethod.Arguments))); return callBackMethod.MethodBuilder; } diff --git a/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs index fed3cd8ca..8433d300a 100644 --- a/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs @@ -51,7 +51,7 @@ protected override CompositeTypeContributor GetProxyTargetContributor(INamingSco protected override ProxyTargetAccessorContributor GetProxyTargetAccessorContributor() { return new ProxyTargetAccessorContributor( - getTarget: () => SelfReference.Self, + getTarget: () => ThisExpression.Instance, targetType); } } diff --git a/src/Castle.Core/DynamicProxy/Generators/CompositionInvocationTypeGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/CompositionInvocationTypeGenerator.cs index 7222725f3..93bd20b44 100644 --- a/src/Castle.Core/DynamicProxy/Generators/CompositionInvocationTypeGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/CompositionInvocationTypeGenerator.cs @@ -1,4 +1,4 @@ -// Copyright 2004-2021 Castle Project - http://www.castleproject.org/ +// Copyright 2004-2025 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. @@ -62,7 +62,7 @@ protected override void ImplementInvokeMethodOnTarget(AbstractTypeEmitter invoca { invokeMethodOnTarget.CodeBuilder.AddStatement( new MethodInvocationExpression( - SelfReference.Self, + ThisExpression.Instance, InvocationMethods.CompositionInvocationEnsureValidTarget)); base.ImplementInvokeMethodOnTarget(invocation, parameters, invokeMethodOnTarget, targetField); diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/FieldReference.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/FieldReference.cs index e31d5ad17..89c006ea8 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/FieldReference.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/FieldReference.cs @@ -64,7 +64,7 @@ public override void EmitAddress(ILGenerator gen) } else { - SelfReference.Self.Emit(gen); + ThisExpression.Instance.Emit(gen); gen.Emit(OpCodes.Ldflda, Reference); } } @@ -77,7 +77,7 @@ public override void Emit(ILGenerator gen) } else { - SelfReference.Self.Emit(gen); + ThisExpression.Instance.Emit(gen); gen.Emit(OpCodes.Ldfld, Reference); } } @@ -91,7 +91,7 @@ public override void EmitStore(IExpression value, ILGenerator gen) } else { - SelfReference.Self.Emit(gen); + ThisExpression.Instance.Emit(gen); value.Emit(gen); gen.Emit(OpCodes.Stfld, Reference); } diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/MethodInvocationExpression.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/MethodInvocationExpression.cs index 7491258ea..d89a96292 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/MethodInvocationExpression.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/MethodInvocationExpression.cs @@ -26,12 +26,12 @@ internal class MethodInvocationExpression : IExpression, IStatement protected readonly IExpression? instance; public MethodInvocationExpression(MethodInfo method, params IExpression[] args) : - this(SelfReference.Self, method, args) + this(ThisExpression.Instance, method, args) { } public MethodInvocationExpression(MethodEmitter method, params IExpression[] args) : - this(SelfReference.Self, method.MethodBuilder, args) + this(ThisExpression.Instance, method.MethodBuilder, args) { } diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/SelfReference.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ThisExpression.cs similarity index 65% rename from src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/SelfReference.cs rename to src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ThisExpression.cs index f2161a16e..b29c378c8 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/SelfReference.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ThisExpression.cs @@ -21,28 +21,17 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST using System.Reflection.Emit; [DebuggerDisplay("this")] - internal class SelfReference : Reference + internal class ThisExpression : IExpression { - public static readonly SelfReference Self = new SelfReference(); + public static readonly ThisExpression Instance = new ThisExpression(); - protected SelfReference() + protected ThisExpression() { } - public override void EmitAddress(ILGenerator gen) - { - throw new NotSupportedException(); - } - - public override void Emit(ILGenerator gen) + public void Emit(ILGenerator gen) { gen.Emit(OpCodes.Ldarg_0); } - - public override void EmitStore(IExpression value, ILGenerator gen) - { - // It does not make sense to assign a value to the this pointer. - throw new NotSupportedException(); - } } } \ No newline at end of file diff --git a/src/Castle.Core/DynamicProxy/Generators/InvocationTypeGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/InvocationTypeGenerator.cs index 0065584b7..b0c2a13b0 100644 --- a/src/Castle.Core/DynamicProxy/Generators/InvocationTypeGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/InvocationTypeGenerator.cs @@ -1,4 +1,4 @@ -// Copyright 2004-2021 Castle Project - http://www.castleproject.org/ +// Copyright 2004-2025 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. @@ -143,7 +143,7 @@ protected virtual void ImplementInvokeMethodOnTarget(AbstractTypeEmitter invocat #endif { localValue = new ConvertExpression(paramType.GetElementType(), - new MethodInvocationExpression(SelfReference.Self, + new MethodInvocationExpression(ThisExpression.Instance, InvocationMethods.GetArgumentValue, new LiteralIntExpression(i))); } @@ -170,7 +170,7 @@ protected virtual void ImplementInvokeMethodOnTarget(AbstractTypeEmitter invocat { args[i] = new ConvertExpression(paramType, - new MethodInvocationExpression(SelfReference.Self, + new MethodInvocationExpression(ThisExpression.Instance, InvocationMethods.GetArgumentValue, new LiteralIntExpression(i))); } @@ -218,7 +218,7 @@ protected virtual void ImplementInvokeMethodOnTarget(AbstractTypeEmitter invocat } var setRetVal = - new MethodInvocationExpression(SelfReference.Self, + new MethodInvocationExpression(ThisExpression.Instance, InvocationMethods.SetReturnValue, retVal); @@ -260,7 +260,7 @@ private void AssignBackByRefArguments(MethodEmitter invokeMethodOnTarget, Dictio invokeMethodOnTarget.CodeBuilder.AddStatement( new MethodInvocationExpression( - SelfReference.Self, + ThisExpression.Instance, InvocationMethods.SetArgumentValue, new LiteralIntExpression(index), localValue)); diff --git a/src/Castle.Core/DynamicProxy/Generators/MethodWithInvocationGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/MethodWithInvocationGenerator.cs index 184c0ce48..a1d1218d3 100644 --- a/src/Castle.Core/DynamicProxy/Generators/MethodWithInvocationGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/MethodWithInvocationGenerator.cs @@ -1,4 +1,4 @@ -// Copyright 2004-2021 Castle Project - http://www.castleproject.org/ +// Copyright 2004-2025 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. @@ -235,7 +235,7 @@ private IExpression[] GetCtorArguments(ClassEmitter @class, IExpression proxiedM return new[] { getTargetExpression(@class, MethodToOverride), - SelfReference.Self, + ThisExpression.Instance, methodInterceptors ?? interceptors, proxiedMethodTokenExpression, new ReferencesToObjectArrayExpression(dereferencedArguments) From 895f61282ed19cfe045916a847993c94758724e0 Mon Sep 17 00:00:00 2001 From: Dominique Schuppli Date: Tue, 16 Dec 2025 23:36:58 +0100 Subject: [PATCH 4/4] Merge `TypeReference` into `Reference` --- .../Emitters/SimpleAST/ArgumentReference.cs | 2 +- .../Emitters/SimpleAST/AsTypeReference.cs | 1 + .../Emitters/SimpleAST/ByRefReference.cs | 2 +- .../Emitters/SimpleAST/FieldReference.cs | 2 ++ .../Emitters/SimpleAST/IndirectReference.cs | 14 ++++---- .../Emitters/SimpleAST/LocalReference.cs | 2 +- .../Emitters/SimpleAST/Reference.cs | 13 ++++++++ .../ReferencesToObjectArrayExpression.cs | 4 +-- .../Emitters/SimpleAST/TypeReference.cs | 33 ------------------- .../DynamicProxy/Generators/GeneratorUtil.cs | 8 ++--- .../MethodWithInvocationGenerator.cs | 2 +- 11 files changed, 33 insertions(+), 50 deletions(-) delete mode 100644 src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/TypeReference.cs diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ArgumentReference.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ArgumentReference.cs index afa5c9390..e4ab715a3 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ArgumentReference.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ArgumentReference.cs @@ -21,7 +21,7 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST using System.Reflection.Emit; [DebuggerDisplay("argument {Type}")] - internal class ArgumentReference : TypeReference + internal class ArgumentReference : Reference { public ArgumentReference(Type argumentType) : base(argumentType) diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/AsTypeReference.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/AsTypeReference.cs index 5d058f88e..58d7866f7 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/AsTypeReference.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/AsTypeReference.cs @@ -27,6 +27,7 @@ internal class AsTypeReference : Reference private readonly Type type; public AsTypeReference(Reference reference, Type type) + : base(type) { this.reference = reference; this.type = type; diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ByRefReference.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ByRefReference.cs index 4df289d0c..769e97db5 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ByRefReference.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ByRefReference.cs @@ -22,7 +22,7 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST [DebuggerDisplay("&{localReference}")] - internal class ByRefReference : TypeReference + internal class ByRefReference : Reference { private readonly LocalReference localReference; diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/FieldReference.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/FieldReference.cs index 89c006ea8..3ba3d8a90 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/FieldReference.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/FieldReference.cs @@ -28,6 +28,7 @@ internal class FieldReference : Reference private readonly bool isStatic; public FieldReference(FieldInfo field) + : base(field.FieldType) { this.field = field; if ((field.Attributes & FieldAttributes.Static) != 0) @@ -37,6 +38,7 @@ public FieldReference(FieldInfo field) } public FieldReference(FieldBuilder fieldBuilder) + : base(fieldBuilder.FieldType) { this.fieldBuilder = fieldBuilder; field = fieldBuilder; diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/IndirectReference.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/IndirectReference.cs index df6ff6642..8865d9970 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/IndirectReference.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/IndirectReference.cs @@ -26,12 +26,12 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST /// ByRef and provides indirect load/store support. /// [DebuggerDisplay("&{OwnerReference}")] - internal class IndirectReference : TypeReference + internal class IndirectReference : Reference { - private readonly TypeReference byRefReference; + private readonly Reference byRefReference; - public IndirectReference(TypeReference byRefReference) - : base(byRefReference.Type.GetElementType()) + public IndirectReference(Reference byRefReference) + : base(byRefReference.Type.GetElementType()!) { if (!byRefReference.Type.IsByRef) { @@ -59,15 +59,15 @@ public override void EmitStore(IExpression value, ILGenerator gen) OpCodeUtil.EmitStoreIndirectOpCodeForType(gen, Type); } - public static TypeReference WrapIfByRef(TypeReference reference) + public static Reference WrapIfByRef(Reference reference) { return reference.Type.IsByRef ? new IndirectReference(reference) : reference; } // TODO: Better name - public static TypeReference[] WrapIfByRef(TypeReference[] references) + public static Reference[] WrapIfByRef(Reference[] references) { - var result = new TypeReference[references.Length]; + var result = new Reference[references.Length]; for (var i = 0; i < references.Length; i++) { diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/LocalReference.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/LocalReference.cs index db02055a1..450a69c57 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/LocalReference.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/LocalReference.cs @@ -21,7 +21,7 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST using System.Reflection.Emit; [DebuggerDisplay("local {Type}")] - internal class LocalReference : TypeReference + internal class LocalReference : Reference { private LocalBuilder? localBuilder; diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/Reference.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/Reference.cs index 297eee600..168f60357 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/Reference.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/Reference.cs @@ -16,10 +16,23 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST { + using System; using System.Reflection.Emit; internal abstract class Reference : IExpression { + private readonly Type type; + + protected Reference(Type type) + { + this.type = type; + } + + public Type Type + { + get { return type; } + } + public abstract void EmitAddress(ILGenerator gen); public abstract void Emit(ILGenerator gen); diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ReferencesToObjectArrayExpression.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ReferencesToObjectArrayExpression.cs index f67f17418..67cb7b714 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ReferencesToObjectArrayExpression.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ReferencesToObjectArrayExpression.cs @@ -24,9 +24,9 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST internal class ReferencesToObjectArrayExpression : IExpression { - private readonly TypeReference[] args; + private readonly Reference[] args; - public ReferencesToObjectArrayExpression(params TypeReference[] args) + public ReferencesToObjectArrayExpression(params Reference[] args) { this.args = args; } diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/TypeReference.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/TypeReference.cs deleted file mode 100644 index 59a2e9666..000000000 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/TypeReference.cs +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2004-2021 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.Generators.Emitters.SimpleAST -{ - using System; - - internal abstract class TypeReference : Reference - { - private readonly Type type; - - protected TypeReference(Type type) - { - this.type = type; - } - - public Type Type - { - get { return type; } - } - } -} \ No newline at end of file diff --git a/src/Castle.Core/DynamicProxy/Generators/GeneratorUtil.cs b/src/Castle.Core/DynamicProxy/Generators/GeneratorUtil.cs index 97a221a6a..de8245877 100644 --- a/src/Castle.Core/DynamicProxy/Generators/GeneratorUtil.cs +++ b/src/Castle.Core/DynamicProxy/Generators/GeneratorUtil.cs @@ -1,4 +1,4 @@ -// Copyright 2004-2021 Castle Project - http://www.castleproject.org/ +// Copyright 2004-2025 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. @@ -25,7 +25,7 @@ namespace Castle.DynamicProxy.Generators internal static class GeneratorUtil { - public static void CopyOutAndRefParameters(TypeReference[] dereferencedArguments, LocalReference invocation, + public static void CopyOutAndRefParameters(Reference[] dereferencedArguments, LocalReference invocation, MethodInfo method, MethodEmitter emitter) { var parameters = method.GetParameters(); @@ -131,12 +131,12 @@ bool IsReadOnly(ParameterInfo parameter) } } - private static ConvertExpression Argument(int i, LocalReference invocationArgs, TypeReference[] arguments) + private static ConvertExpression Argument(int i, LocalReference invocationArgs, Reference[] arguments) { return new ConvertExpression(arguments[i].Type, new LoadRefArrayElementExpression(i, invocationArgs)); } - private static AssignStatement AssignArgument(TypeReference[] dereferencedArguments, int i, + private static AssignStatement AssignArgument(Reference[] dereferencedArguments, int i, LocalReference invocationArgs) { return new AssignStatement(dereferencedArguments[i], Argument(i, invocationArgs, dereferencedArguments)); diff --git a/src/Castle.Core/DynamicProxy/Generators/MethodWithInvocationGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/MethodWithInvocationGenerator.cs index a1d1218d3..765355b52 100644 --- a/src/Castle.Core/DynamicProxy/Generators/MethodWithInvocationGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/MethodWithInvocationGenerator.cs @@ -230,7 +230,7 @@ private void EmitLoadGenericMethodArguments(MethodEmitter methodEmitter, MethodI genericParamsArrayLocal)); } - private IExpression[] GetCtorArguments(ClassEmitter @class, IExpression proxiedMethodTokenExpression, TypeReference[] dereferencedArguments, IExpression methodInterceptors) + private IExpression[] GetCtorArguments(ClassEmitter @class, IExpression proxiedMethodTokenExpression, Reference[] dereferencedArguments, IExpression methodInterceptors) { return new[] {