From 972b968143faeed71a261a04ca287d1194926369 Mon Sep 17 00:00:00 2001 From: Dominique Schuppli Date: Tue, 5 Jan 2021 15:26:45 +0100 Subject: [PATCH 1/3] Extract `ProxyTargetAccessorContributor` from `ProxyInstanceContributor`, and change proxy type generators to use the new contributor for `IProxyTargetAccessor`. Remove everything that has become redundant. --- .../ClassProxyInstanceContributor.cs | 7 -- ...ClassProxyWithTargetInstanceContributor.cs | 37 ---------- .../InterfaceProxyInstanceContributor.cs | 5 -- .../Contributors/ProxyInstanceContributor.cs | 40 ---------- .../ProxyTargetAccessorContributor.cs | 74 +++++++++++++++++++ .../Generators/BaseClassProxyGenerator.cs | 10 ++- .../Generators/BaseInterfaceProxyGenerator.cs | 10 ++- .../Generators/ClassProxyGenerator.cs | 7 ++ .../ClassProxyWithTargetGenerator.cs | 9 ++- .../InterfaceProxyWithTargetGenerator.cs | 7 ++ ...erfaceProxyWithTargetInterfaceGenerator.cs | 7 ++ .../InterfaceProxyWithoutTargetGenerator.cs | 7 ++ 12 files changed, 126 insertions(+), 94 deletions(-) delete mode 100644 src/Castle.Core/DynamicProxy/Contributors/ClassProxyWithTargetInstanceContributor.cs create mode 100644 src/Castle.Core/DynamicProxy/Contributors/ProxyTargetAccessorContributor.cs diff --git a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyInstanceContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyInstanceContributor.cs index f8e27c0399..8434b3fc99 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyInstanceContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyInstanceContributor.cs @@ -49,14 +49,8 @@ public ClassProxyInstanceContributor(Type targetType, IList methodsT #endif } - protected override Reference GetTargetReference(ClassEmitter emitter) - { - return SelfReference.Self; - } - public override void Generate(ClassEmitter @class) { - var interceptors = @class.GetField("__interceptors"); #if FEATURE_SERIALIZATION if (implementISerializable) { @@ -64,7 +58,6 @@ public override void Generate(ClassEmitter @class) Constructor(@class); } #endif - ImplementProxyTargetAccessor(@class, interceptors); foreach (var attribute in targetType.GetNonInheritableAttributes()) { @class.DefineCustomAttribute(attribute.Builder); diff --git a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyWithTargetInstanceContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyWithTargetInstanceContributor.cs deleted file mode 100644 index c3a91012bc..0000000000 --- a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyWithTargetInstanceContributor.cs +++ /dev/null @@ -1,37 +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.Contributors -{ - using System; - using System.Collections.Generic; - using System.Reflection; - - using Castle.DynamicProxy.Generators.Emitters; - using Castle.DynamicProxy.Generators.Emitters.SimpleAST; - - class ClassProxyWithTargetInstanceContributor : ClassProxyInstanceContributor - { - public ClassProxyWithTargetInstanceContributor(Type targetType, IList methodsToSkip, - Type[] interfaces, string typeId) - : base(targetType, methodsToSkip, interfaces, typeId) - { - } - - protected override Reference GetTargetReference(ClassEmitter emitter) - { - return emitter.GetField("__target"); - } - } -} diff --git a/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyInstanceContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyInstanceContributor.cs index e00e143fa6..c7fdfb477a 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyInstanceContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyInstanceContributor.cs @@ -23,11 +23,6 @@ namespace Castle.DynamicProxy.Contributors internal class InterfaceProxyInstanceContributor : ProxyInstanceContributor { - protected override Reference GetTargetReference(ClassEmitter emitter) - { - return emitter.GetField("__target"); - } - public InterfaceProxyInstanceContributor(Type targetType, string proxyGeneratorId, Type[] interfaces) : base(targetType, interfaces, proxyGeneratorId) { diff --git a/src/Castle.Core/DynamicProxy/Contributors/ProxyInstanceContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/ProxyInstanceContributor.cs index 454ecc6173..e86765df91 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/ProxyInstanceContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/ProxyInstanceContributor.cs @@ -43,57 +43,17 @@ protected ProxyInstanceContributor(Type targetType, Type[] interfaces, string pr this.interfaces = interfaces ?? Type.EmptyTypes; } - protected abstract Reference GetTargetReference(ClassEmitter emitter); - - private Expression GetTargetReferenceExpression(ClassEmitter emitter) - { - return GetTargetReference(emitter).ToExpression(); - } - public virtual void Generate(ClassEmitter @class) { - var interceptors = @class.GetField("__interceptors"); #if FEATURE_SERIALIZATION ImplementGetObjectData(@class); #endif - ImplementProxyTargetAccessor(@class, interceptors); foreach (var attribute in targetType.GetNonInheritableAttributes()) { @class.DefineCustomAttribute(attribute.Builder); } } - protected void ImplementProxyTargetAccessor(ClassEmitter emitter, FieldReference interceptorsField) - { - var dynProxyGetTarget = emitter.CreateMethod("DynProxyGetTarget", typeof(object)); - - dynProxyGetTarget.CodeBuilder.AddStatement( - new ReturnStatement(new ConvertExpression(typeof(object), targetType, GetTargetReferenceExpression(emitter)))); - - var dynProxySetTarget = emitter.CreateMethod("DynProxySetTarget", typeof(void), typeof(object)); - - // we can only change the target of the interface proxy - var targetField = GetTargetReference(emitter) as FieldReference; - if (targetField != null) - { - dynProxySetTarget.CodeBuilder.AddStatement( - new AssignStatement(targetField, - new ConvertExpression(targetField.Fieldbuilder.FieldType, dynProxySetTarget.Arguments[0].ToExpression()))); - } - else - { - dynProxySetTarget.CodeBuilder.AddStatement( - new ThrowStatement(typeof(InvalidOperationException), "Cannot change the target of the class proxy.")); - } - - dynProxySetTarget.CodeBuilder.AddStatement(new ReturnStatement()); - - var getInterceptors = emitter.CreateMethod("GetInterceptors", typeof(IInterceptor[])); - - getInterceptors.CodeBuilder.AddStatement( - new ReturnStatement(interceptorsField)); - } - #if FEATURE_SERIALIZATION protected void ImplementGetObjectData(ClassEmitter emitter) { diff --git a/src/Castle.Core/DynamicProxy/Contributors/ProxyTargetAccessorContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/ProxyTargetAccessorContributor.cs new file mode 100644 index 0000000000..f5f363e88d --- /dev/null +++ b/src/Castle.Core/DynamicProxy/Contributors/ProxyTargetAccessorContributor.cs @@ -0,0 +1,74 @@ +// 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.Contributors +{ + using System; + + using Castle.DynamicProxy.Generators; + using Castle.DynamicProxy.Generators.Emitters; + using Castle.DynamicProxy.Generators.Emitters.SimpleAST; + + /// + /// Adds an implementation for to the proxy type. + /// + internal sealed class ProxyTargetAccessorContributor : ITypeContributor + { + private readonly Func getTargetReference; + private readonly Type targetType; + + public ProxyTargetAccessorContributor(Func getTargetReference, Type targetType) + { + this.getTargetReference = getTargetReference; + this.targetType = targetType; + } + + public void CollectElementsToProxy(IProxyGenerationHook hook, MetaType model) + { + } + + public void Generate(ClassEmitter emitter) + { + var interceptorsField = emitter.GetField("__interceptors"); + var targetReference = getTargetReference(); + + var dynProxyGetTarget = emitter.CreateMethod(nameof(IProxyTargetAccessor.DynProxyGetTarget), typeof(object)); + + dynProxyGetTarget.CodeBuilder.AddStatement( + new ReturnStatement(new ConvertExpression(typeof(object), targetType, targetReference.ToExpression()))); + + 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) + { + dynProxySetTarget.CodeBuilder.AddStatement( + new AssignStatement(targetField, + new ConvertExpression(targetField.Fieldbuilder.FieldType, dynProxySetTarget.Arguments[0].ToExpression()))); + } + else + { + dynProxySetTarget.CodeBuilder.AddStatement( + new ThrowStatement(typeof(InvalidOperationException), "Cannot change the target of the class proxy.")); + } + + dynProxySetTarget.CodeBuilder.AddStatement(new ReturnStatement()); + + var getInterceptors = emitter.CreateMethod(nameof(IProxyTargetAccessor.GetInterceptors), typeof(IInterceptor[])); + + getInterceptors.CodeBuilder.AddStatement( + new ReturnStatement(interceptorsField)); + } + } +} diff --git a/src/Castle.Core/DynamicProxy/Generators/BaseClassProxyGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/BaseClassProxyGenerator.cs index 638f32131e..0be4294300 100644 --- a/src/Castle.Core/DynamicProxy/Generators/BaseClassProxyGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/BaseClassProxyGenerator.cs @@ -37,6 +37,8 @@ protected BaseClassProxyGenerator(ModuleScope scope, Type targetType, Type[] int protected abstract CompositeTypeContributor GetProxyTargetContributor(List methodsToSkip, INamingScope namingScope); + protected abstract ProxyTargetAccessorContributor GetProxyTargetAccessorContributor(); + protected sealed override Type GenerateType(string name, INamingScope namingScope) { IEnumerable contributors; @@ -100,7 +102,7 @@ protected sealed override Type GenerateType(string name, INamingScope namingScop private IEnumerable GetTypeImplementerMapping(out IEnumerable contributors, INamingScope namingScope) { - var contributorsList = new List(capacity: 4); + var contributorsList = new List(capacity: 5); var methodsToSkip = new List(); // TODO: the trick with methodsToSkip is not very nice... var targetInterfaces = targetType.GetAllInterfaces(); var typeImplementerMapping = new Dictionary(); @@ -171,6 +173,7 @@ private IEnumerable GetTypeImplementerMapping(out IEnumerable GetTypeImplementerMapping(out IEnumerable typeImplementerMapping, ICollection targetInterfaces); @@ -146,7 +148,7 @@ protected virtual IEnumerable GetTypeImplementerMapping(Type proxyTargetTy out IEnumerable contributors, INamingScope namingScope) { - var contributorsList = new List(capacity: 4); + var contributorsList = new List(capacity: 5); var targetInterfaces = proxyTargetType.GetAllInterfaces(); var typeImplementerMapping = new Dictionary(); @@ -208,14 +210,18 @@ protected virtual IEnumerable GetTypeImplementerMapping(Type proxyTargetTy } // 4. plus special interfaces + var instanceContributor = new InterfaceProxyInstanceContributor(targetType, GeneratorType, interfaces); contributorsList.Add(instanceContributor); #if FEATURE_SERIALIZATION AddMappingForISerializable(typeImplementerMapping, instanceContributor); #endif + + var proxyTargetAccessorContributor = GetProxyTargetAccessorContributor(); + contributorsList.Add(proxyTargetAccessorContributor); try { - AddMappingNoCheck(typeof(IProxyTargetAccessor), instanceContributor, typeImplementerMapping); + AddMappingNoCheck(typeof(IProxyTargetAccessor), proxyTargetAccessorContributor, typeImplementerMapping); } catch (ArgumentException) { diff --git a/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs index 886c06a844..1fbc8d13f9 100644 --- a/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs @@ -45,5 +45,12 @@ protected override CompositeTypeContributor GetProxyTargetContributor(List SelfReference.Self, + targetType); + } } } diff --git a/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs index e4c82645d6..c3da606b5b 100644 --- a/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs @@ -52,7 +52,7 @@ protected override void CreateFields(ClassEmitter emitter) protected override ProxyInstanceContributor GetProxyInstanceContributor(List methodsToSkip) { - return new ClassProxyWithTargetInstanceContributor(targetType, methodsToSkip, interfaces, ProxyTypeConstants.ClassWithTarget); + return new ClassProxyInstanceContributor(targetType, methodsToSkip, interfaces, ProxyTypeConstants.ClassWithTarget); } protected override CompositeTypeContributor GetProxyTargetContributor(List methodsToSkip, INamingScope namingScope) @@ -60,6 +60,13 @@ protected override CompositeTypeContributor GetProxyTargetContributor(List targetField, + targetType); + } + private void CreateTargetField(ClassEmitter emitter) { targetField = emitter.CreateField("__target", targetType); diff --git a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs index ab30754080..dc4ac91c41 100644 --- a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs @@ -37,6 +37,13 @@ protected override CompositeTypeContributor GetProxyTargetContributor(Type proxy return new InterfaceProxyTargetContributor(proxyTargetType, AllowChangeTarget, namingScope) { Logger = Logger }; } + protected override ProxyTargetAccessorContributor GetProxyTargetAccessorContributor() + { + return new ProxyTargetAccessorContributor( + getTargetReference: () => targetField, + proxyTargetType); + } + protected override void AddMappingForAdditionalInterfaces(CompositeTypeContributor contributor, Type[] proxiedInterfaces, IDictionary typeImplementerMapping, ICollection targetInterfaces) diff --git a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetInterfaceGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetInterfaceGenerator.cs index 77091c2052..99b216cd08 100644 --- a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetInterfaceGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetInterfaceGenerator.cs @@ -41,6 +41,13 @@ protected override CompositeTypeContributor GetProxyTargetContributor(Type proxy return new InterfaceProxyWithTargetInterfaceTargetContributor(proxyTargetType, AllowChangeTarget, namingScope) { Logger = Logger }; } + protected override ProxyTargetAccessorContributor GetProxyTargetAccessorContributor() + { + return new ProxyTargetAccessorContributor( + getTargetReference: () => targetField, + proxyTargetType); + } + protected override void AddMappingForAdditionalInterfaces(CompositeTypeContributor contributor, Type[] proxiedInterfaces, IDictionary typeImplementerMapping, ICollection targetInterfaces) diff --git a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithoutTargetGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithoutTargetGenerator.cs index 35fe2497fa..d916e4dc62 100644 --- a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithoutTargetGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithoutTargetGenerator.cs @@ -38,6 +38,13 @@ protected override CompositeTypeContributor GetProxyTargetContributor(Type proxy return new InterfaceProxyWithoutTargetContributor(namingScope, (c, m) => NullExpression.Instance) { Logger = Logger }; } + protected override ProxyTargetAccessorContributor GetProxyTargetAccessorContributor() + { + return new ProxyTargetAccessorContributor( + getTargetReference: () => targetField, + proxyTargetType); + } + protected override void AddMappingForAdditionalInterfaces(CompositeTypeContributor contributor, Type[] proxiedInterfaces, IDictionary typeImplementerMapping, ICollection targetInterfaces) From 05baeb19ead16ccb7163f79bcada4e02dd2867fe Mon Sep 17 00:00:00 2001 From: Dominique Schuppli Date: Tue, 5 Jan 2021 15:43:51 +0100 Subject: [PATCH 2/3] Extract `NonInheritableAttributesContributor` from `ProxyInstanceContributor`. Because it does not contribute a type, it is not added to `GetTypeImplementerMapping`'s list of contributors, but invoked directly in `GenerateType`. --- .../ClassProxyInstanceContributor.cs | 4 -- .../NonInheritableAttributesContributor.cs | 47 +++++++++++++++++++ .../Contributors/ProxyInstanceContributor.cs | 4 -- .../Generators/BaseClassProxyGenerator.cs | 4 ++ .../Generators/BaseInterfaceProxyGenerator.cs | 4 ++ 5 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 src/Castle.Core/DynamicProxy/Contributors/NonInheritableAttributesContributor.cs diff --git a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyInstanceContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyInstanceContributor.cs index 8434b3fc99..0cbc5289d9 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyInstanceContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyInstanceContributor.cs @@ -58,10 +58,6 @@ public override void Generate(ClassEmitter @class) Constructor(@class); } #endif - foreach (var attribute in targetType.GetNonInheritableAttributes()) - { - @class.DefineCustomAttribute(attribute.Builder); - } } #if FEATURE_SERIALIZATION diff --git a/src/Castle.Core/DynamicProxy/Contributors/NonInheritableAttributesContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/NonInheritableAttributesContributor.cs new file mode 100644 index 0000000000..0f30bec9af --- /dev/null +++ b/src/Castle.Core/DynamicProxy/Contributors/NonInheritableAttributesContributor.cs @@ -0,0 +1,47 @@ +// 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.Contributors +{ + using System; + + using Castle.DynamicProxy.Generators; + using Castle.DynamicProxy.Generators.Emitters; + using Castle.DynamicProxy.Internal; + + /// + /// Reproduces the proxied type's non-inheritable custom attributes on the proxy type. + /// + internal sealed class NonInheritableAttributesContributor : ITypeContributor + { + private readonly Type targetType; + + public NonInheritableAttributesContributor(Type targetType) + { + this.targetType = targetType; + } + + public void Generate(ClassEmitter emitter) + { + foreach (var attribute in targetType.GetNonInheritableAttributes()) + { + emitter.DefineCustomAttribute(attribute.Builder); + } + } + + public void CollectElementsToProxy(IProxyGenerationHook hook, MetaType model) + { + } + } +} diff --git a/src/Castle.Core/DynamicProxy/Contributors/ProxyInstanceContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/ProxyInstanceContributor.cs index e86765df91..21902c7b5e 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/ProxyInstanceContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/ProxyInstanceContributor.cs @@ -48,10 +48,6 @@ public virtual void Generate(ClassEmitter @class) #if FEATURE_SERIALIZATION ImplementGetObjectData(@class); #endif - foreach (var attribute in targetType.GetNonInheritableAttributes()) - { - @class.DefineCustomAttribute(attribute.Builder); - } } #if FEATURE_SERIALIZATION diff --git a/src/Castle.Core/DynamicProxy/Generators/BaseClassProxyGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/BaseClassProxyGenerator.cs index 0be4294300..4a77fcd579 100644 --- a/src/Castle.Core/DynamicProxy/Generators/BaseClassProxyGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/BaseClassProxyGenerator.cs @@ -93,6 +93,10 @@ protected sealed override Type GenerateType(string name, INamingScope namingScop // Complete type initializer code body CompleteInitCacheMethod(cctor.CodeBuilder); + // non-inheritable attributes from proxied type + var nonInheritableAttributesContributor = new NonInheritableAttributesContributor(targetType); + nonInheritableAttributesContributor.Generate(emitter); + // Crosses fingers and build type var proxyType = emitter.BuildType(); diff --git a/src/Castle.Core/DynamicProxy/Generators/BaseInterfaceProxyGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/BaseInterfaceProxyGenerator.cs index 6cb35c8538..56960f7e9a 100644 --- a/src/Castle.Core/DynamicProxy/Generators/BaseInterfaceProxyGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/BaseInterfaceProxyGenerator.cs @@ -131,6 +131,10 @@ protected override Type GenerateType(string typeName, INamingScope namingScope) // Complete type initializer code body CompleteInitCacheMethod(cctor.CodeBuilder); + // non-inheritable attributes from proxied type + var nonInheritableAttributesContributor = new NonInheritableAttributesContributor(targetType); + nonInheritableAttributesContributor.Generate(emitter); + // Crosses fingers and build type var generatedType = emitter.BuildType(); From 12f6964d149f693f35250eda7441bba97252118e Mon Sep 17 00:00:00 2001 From: Dominique Schuppli Date: Tue, 5 Jan 2021 15:57:29 +0100 Subject: [PATCH 3/3] `ProxyInstanceContributor` -> `SerializableContributor` as its sole remaining function is to implement `ISerializable`. --- ...s => ClassProxySerializableContributor.cs} | 20 +++++++------------ ... InterfaceProxySerializableContributor.cs} | 12 ++++++----- ...tributor.cs => SerializableContributor.cs} | 20 +++++++------------ .../Generators/BaseClassProxyGenerator.cs | 10 ++++++---- .../Generators/BaseInterfaceProxyGenerator.cs | 6 +++--- .../Generators/ClassProxyGenerator.cs | 6 ++++-- .../ClassProxyWithTargetGenerator.cs | 6 ++++-- 7 files changed, 38 insertions(+), 42 deletions(-) rename src/Castle.Core/DynamicProxy/Contributors/{ClassProxyInstanceContributor.cs => ClassProxySerializableContributor.cs} (96%) rename src/Castle.Core/DynamicProxy/Contributors/{InterfaceProxyInstanceContributor.cs => InterfaceProxySerializableContributor.cs} (92%) rename src/Castle.Core/DynamicProxy/Contributors/{ProxyInstanceContributor.cs => SerializableContributor.cs} (94%) diff --git a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyInstanceContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/ClassProxySerializableContributor.cs similarity index 96% rename from src/Castle.Core/DynamicProxy/Contributors/ClassProxyInstanceContributor.cs rename to src/Castle.Core/DynamicProxy/Contributors/ClassProxySerializableContributor.cs index 0cbc5289d9..891c0eed5f 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyInstanceContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/ClassProxySerializableContributor.cs @@ -12,14 +12,14 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if FEATURE_SERIALIZATION + namespace Castle.DynamicProxy.Contributors { using System; using System.Collections.Generic; using System.Reflection; -#if FEATURE_SERIALIZATION using System.Runtime.Serialization; -#endif using Castle.DynamicProxy.Generators.Emitters; using Castle.DynamicProxy.Generators.Emitters.CodeBuilders; @@ -27,40 +27,33 @@ namespace Castle.DynamicProxy.Contributors using Castle.DynamicProxy.Internal; using Castle.DynamicProxy.Tokens; - internal class ClassProxyInstanceContributor : ProxyInstanceContributor + internal class ClassProxySerializableContributor : SerializableContributor { -#if FEATURE_SERIALIZATION private readonly bool delegateToBaseGetObjectData; private readonly bool implementISerializable; private ConstructorInfo serializationConstructor; private readonly IList serializedFields = new List(); -#endif - public ClassProxyInstanceContributor(Type targetType, IList methodsToSkip, Type[] interfaces, + public ClassProxySerializableContributor(Type targetType, IList methodsToSkip, Type[] interfaces, string typeId) : base(targetType, interfaces, typeId) { -#if FEATURE_SERIALIZATION if (targetType.IsSerializable) { implementISerializable = true; delegateToBaseGetObjectData = VerifyIfBaseImplementsGetObjectData(targetType, methodsToSkip); } -#endif } public override void Generate(ClassEmitter @class) { -#if FEATURE_SERIALIZATION if (implementISerializable) { ImplementGetObjectData(@class); Constructor(@class); } -#endif } -#if FEATURE_SERIALIZATION protected override void AddAddValueInvocation(ArgumentReference serializationInfo, MethodEmitter getObjectData, FieldReference field) { @@ -218,6 +211,7 @@ private bool VerifyIfBaseImplementsGetObjectData(Type baseType, IList methodsToSkip); +#if FEATURE_SERIALIZATION + protected abstract SerializableContributor GetSerializableContributor(List methodsToSkip); +#endif protected abstract CompositeTypeContributor GetProxyTargetContributor(List methodsToSkip, INamingScope namingScope); @@ -178,12 +180,12 @@ private IEnumerable GetTypeImplementerMapping(out IEnumerable GetTypeImplementerMapping(Type proxyTargetTy // 4. plus special interfaces - var instanceContributor = new InterfaceProxyInstanceContributor(targetType, GeneratorType, interfaces); - contributorsList.Add(instanceContributor); #if FEATURE_SERIALIZATION - AddMappingForISerializable(typeImplementerMapping, instanceContributor); + var serializableContributor = new InterfaceProxySerializableContributor(targetType, GeneratorType, interfaces); + contributorsList.Add(serializableContributor); + AddMappingForISerializable(typeImplementerMapping, serializableContributor); #endif var proxyTargetAccessorContributor = GetProxyTargetAccessorContributor(); diff --git a/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs index 1fbc8d13f9..5d69669dfa 100644 --- a/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs @@ -36,10 +36,12 @@ protected override CacheKey GetCacheKey() return new CacheKey(targetType, interfaces, ProxyGenerationOptions); } - protected override ProxyInstanceContributor GetProxyInstanceContributor(List methodsToSkip) +#if FEATURE_SERIALIZATION + protected override SerializableContributor GetSerializableContributor(List methodsToSkip) { - return new ClassProxyInstanceContributor(targetType, methodsToSkip, interfaces, ProxyTypeConstants.Class); + return new ClassProxySerializableContributor(targetType, methodsToSkip, interfaces, ProxyTypeConstants.Class); } +#endif protected override CompositeTypeContributor GetProxyTargetContributor(List methodsToSkip, INamingScope namingScope) { diff --git a/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs index c3da606b5b..647eaee207 100644 --- a/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs @@ -50,10 +50,12 @@ protected override void CreateFields(ClassEmitter emitter) CreateTargetField(emitter); } - protected override ProxyInstanceContributor GetProxyInstanceContributor(List methodsToSkip) +#if FEATURE_SERIALIZATION + protected override SerializableContributor GetSerializableContributor(List methodsToSkip) { - return new ClassProxyInstanceContributor(targetType, methodsToSkip, interfaces, ProxyTypeConstants.ClassWithTarget); + return new ClassProxySerializableContributor(targetType, methodsToSkip, interfaces, ProxyTypeConstants.ClassWithTarget); } +#endif protected override CompositeTypeContributor GetProxyTargetContributor(List methodsToSkip, INamingScope namingScope) {