From 9c928ae6362bbf27b0cf55a2b66e87da01fa932e Mon Sep 17 00:00:00 2001 From: Dominique Schuppli Date: Thu, 11 Feb 2021 23:46:31 +0100 Subject: [PATCH 01/13] Define `ProxyGenerationContext` state bag --- .../DynamicProxy/ProxyGenerationContext.cs | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/Castle.Core/DynamicProxy/ProxyGenerationContext.cs diff --git a/src/Castle.Core/DynamicProxy/ProxyGenerationContext.cs b/src/Castle.Core/DynamicProxy/ProxyGenerationContext.cs new file mode 100644 index 0000000000..92dae09528 --- /dev/null +++ b/src/Castle.Core/DynamicProxy/ProxyGenerationContext.cs @@ -0,0 +1,20 @@ +// 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 +{ + internal sealed class ProxyGenerationContext + { + } +} From 506b69971cc46e77cc2754a017b14e0c9170a497 Mon Sep 17 00:00:00 2001 From: Dominique Schuppli Date: Fri, 12 Feb 2021 00:45:43 +0100 Subject: [PATCH 02/13] Inject context into generators, contributors, and collectors --- .../Contributors/ClassMembersCollector.cs | 4 ++-- .../ClassProxyTargetContributor.cs | 9 +++++---- .../ClassProxyWithTargetTargetContributor.cs | 9 +++++---- .../Contributors/CompositeTypeContributor.cs | 9 ++++++++- .../DelegateTypeMembersCollector.cs | 4 ++-- .../Contributors/InterfaceMembersCollector.cs | 4 ++-- .../InterfaceMembersOnClassCollector.cs | 4 +++- .../InterfaceProxyTargetContributor.cs | 7 ++++--- ...rfaceProxyWithOptionalTargetContributor.cs | 5 +++-- ...oxyWithTargetInterfaceTargetContributor.cs | 8 ++++---- .../InterfaceProxyWithoutTargetContributor.cs | 7 ++++--- .../Contributors/MembersCollector.cs | 9 ++++++++- .../Contributors/MixinContributor.cs | 9 +++++---- .../WrappedClassMembersCollector.cs | 3 ++- .../DynamicProxy/DefaultProxyBuilder.cs | 20 ++++++++++++++----- .../Generators/BaseClassProxyGenerator.cs | 10 ++++++---- .../Generators/BaseInterfaceProxyGenerator.cs | 11 +++++----- .../Generators/BaseProxyGenerator.cs | 11 +++++++++- .../Generators/ClassProxyGenerator.cs | 8 +++++--- .../ClassProxyWithTargetGenerator.cs | 7 ++++--- .../InterfaceProxyWithTargetGenerator.cs | 9 +++++---- ...erfaceProxyWithTargetInterfaceGenerator.cs | 11 +++++----- .../InterfaceProxyWithoutTargetGenerator.cs | 9 +++++---- .../Serialization/ProxyObjectReference.cs | 14 ++++++++----- 24 files changed, 128 insertions(+), 73 deletions(-) diff --git a/src/Castle.Core/DynamicProxy/Contributors/ClassMembersCollector.cs b/src/Castle.Core/DynamicProxy/Contributors/ClassMembersCollector.cs index 1200cdae51..47ef965b77 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/ClassMembersCollector.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/ClassMembersCollector.cs @@ -21,8 +21,8 @@ namespace Castle.DynamicProxy.Contributors internal class ClassMembersCollector : MembersCollector { - public ClassMembersCollector(Type targetType) - : base(targetType) + public ClassMembersCollector(ProxyGenerationContext context, Type targetType) + : base(context, targetType) { } diff --git a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyTargetContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyTargetContributor.cs index 6b72d993a3..068cc2074f 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyTargetContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyTargetContributor.cs @@ -31,20 +31,21 @@ internal class ClassProxyTargetContributor : CompositeTypeContributor { private readonly Type targetType; - public ClassProxyTargetContributor(Type targetType, INamingScope namingScope) - : base(namingScope) + public ClassProxyTargetContributor(ProxyGenerationContext context, INamingScope namingScope, + Type targetType) + : base(context, namingScope) { this.targetType = targetType; } protected override IEnumerable GetCollectors() { - var targetItem = new ClassMembersCollector(targetType) { Logger = Logger }; + var targetItem = new ClassMembersCollector(Context, targetType) { Logger = Logger }; yield return targetItem; foreach (var @interface in interfaces) { - var item = new InterfaceMembersOnClassCollector(@interface, true, + var item = new InterfaceMembersOnClassCollector(Context, @interface, true, targetType.GetInterfaceMap(@interface)) { Logger = Logger }; yield return item; } diff --git a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyWithTargetTargetContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyWithTargetTargetContributor.cs index e9ab21fc32..6b32416573 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyWithTargetTargetContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyWithTargetTargetContributor.cs @@ -29,20 +29,21 @@ internal class ClassProxyWithTargetTargetContributor : CompositeTypeContributor { private readonly Type targetType; - public ClassProxyWithTargetTargetContributor(Type targetType, INamingScope namingScope) - : base(namingScope) + public ClassProxyWithTargetTargetContributor(ProxyGenerationContext context, INamingScope namingScope, + Type targetType) + : base(context, namingScope) { this.targetType = targetType; } protected override IEnumerable GetCollectors() { - var targetItem = new WrappedClassMembersCollector(targetType) { Logger = Logger }; + var targetItem = new WrappedClassMembersCollector(Context, targetType) { Logger = Logger }; yield return targetItem; foreach (var @interface in interfaces) { - var item = new InterfaceMembersOnClassCollector(@interface, true, + var item = new InterfaceMembersOnClassCollector(Context, @interface, true, targetType.GetInterfaceMap(@interface)) { Logger = Logger }; yield return item; } diff --git a/src/Castle.Core/DynamicProxy/Contributors/CompositeTypeContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/CompositeTypeContributor.cs index 90fd027a85..f448e95397 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/CompositeTypeContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/CompositeTypeContributor.cs @@ -26,6 +26,7 @@ namespace Castle.DynamicProxy.Contributors internal abstract class CompositeTypeContributor : ITypeContributor { + private readonly ProxyGenerationContext context; protected readonly INamingScope namingScope; protected readonly ICollection interfaces = new HashSet(); @@ -35,8 +36,9 @@ internal abstract class CompositeTypeContributor : ITypeContributor private readonly List events = new List(); private readonly List methods = new List(); - protected CompositeTypeContributor(INamingScope namingScope) + protected CompositeTypeContributor(ProxyGenerationContext context, INamingScope namingScope) { + this.context = context; this.namingScope = namingScope; } @@ -46,6 +48,11 @@ public ILogger Logger set { logger = value; } } + protected ProxyGenerationContext Context + { + get { return context; } + } + public void CollectElementsToProxy(IProxyGenerationHook hook, MetaType model) { Debug.Assert(hook != null); diff --git a/src/Castle.Core/DynamicProxy/Contributors/DelegateTypeMembersCollector.cs b/src/Castle.Core/DynamicProxy/Contributors/DelegateTypeMembersCollector.cs index d3f7970164..44ea7b1426 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/DelegateTypeMembersCollector.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/DelegateTypeMembersCollector.cs @@ -22,8 +22,8 @@ namespace Castle.DynamicProxy.Contributors internal sealed class DelegateTypeMembersCollector : MembersCollector { - public DelegateTypeMembersCollector(Type delegateType) - : base(delegateType) + public DelegateTypeMembersCollector(ProxyGenerationContext context, Type delegateType) + : base(context, delegateType) { } diff --git a/src/Castle.Core/DynamicProxy/Contributors/InterfaceMembersCollector.cs b/src/Castle.Core/DynamicProxy/Contributors/InterfaceMembersCollector.cs index bb0fdc81de..f1e9109bd2 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/InterfaceMembersCollector.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/InterfaceMembersCollector.cs @@ -21,8 +21,8 @@ namespace Castle.DynamicProxy.Contributors internal class InterfaceMembersCollector : MembersCollector { - public InterfaceMembersCollector(Type @interface) - : base(@interface) + public InterfaceMembersCollector(ProxyGenerationContext context, Type @interface) + : base(context, @interface) { } diff --git a/src/Castle.Core/DynamicProxy/Contributors/InterfaceMembersOnClassCollector.cs b/src/Castle.Core/DynamicProxy/Contributors/InterfaceMembersOnClassCollector.cs index 3e643cf802..34f08e0dbf 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/InterfaceMembersOnClassCollector.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/InterfaceMembersOnClassCollector.cs @@ -24,7 +24,9 @@ internal class InterfaceMembersOnClassCollector : MembersCollector private readonly InterfaceMapping map; private readonly bool onlyProxyVirtual; - public InterfaceMembersOnClassCollector(Type type, bool onlyProxyVirtual, InterfaceMapping map) : base(type) + public InterfaceMembersOnClassCollector(ProxyGenerationContext context, Type type, + bool onlyProxyVirtual, InterfaceMapping map) + : base(context, type) { this.onlyProxyVirtual = onlyProxyVirtual; this.map = map; diff --git a/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyTargetContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyTargetContributor.cs index deb0cde955..b767a48b4f 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyTargetContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyTargetContributor.cs @@ -27,8 +27,9 @@ internal class InterfaceProxyTargetContributor : CompositeTypeContributor private readonly bool canChangeTarget; private readonly Type proxyTargetType; - public InterfaceProxyTargetContributor(Type proxyTargetType, bool canChangeTarget, INamingScope namingScope) - : base(namingScope) + public InterfaceProxyTargetContributor(ProxyGenerationContext context, INamingScope namingScope, + Type proxyTargetType, bool canChangeTarget) + : base(context, namingScope) { this.proxyTargetType = proxyTargetType; this.canChangeTarget = canChangeTarget; @@ -46,7 +47,7 @@ protected override IEnumerable GetCollectors() protected virtual MembersCollector GetCollectorForInterface(Type @interface) { - return new InterfaceMembersOnClassCollector(@interface, false, + return new InterfaceMembersOnClassCollector(Context, @interface, false, proxyTargetType.GetInterfaceMap(@interface)); } diff --git a/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyWithOptionalTargetContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyWithOptionalTargetContributor.cs index 858a6fb205..18591768d0 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyWithOptionalTargetContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyWithOptionalTargetContributor.cs @@ -21,9 +21,10 @@ internal class InterfaceProxyWithOptionalTargetContributor : InterfaceProxyWitho { private readonly GetTargetReferenceDelegate getTargetReference; - public InterfaceProxyWithOptionalTargetContributor(INamingScope namingScope, GetTargetExpressionDelegate getTarget, + public InterfaceProxyWithOptionalTargetContributor(ProxyGenerationContext context, INamingScope namingScope, + GetTargetExpressionDelegate getTarget, GetTargetReferenceDelegate getTargetReference) - : base(namingScope, getTarget) + : base(context, namingScope, getTarget) { this.getTargetReference = getTargetReference; canChangeTarget = true; diff --git a/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyWithTargetInterfaceTargetContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyWithTargetInterfaceTargetContributor.cs index b1f4525965..e792e3e810 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyWithTargetInterfaceTargetContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyWithTargetInterfaceTargetContributor.cs @@ -20,15 +20,15 @@ namespace Castle.DynamicProxy.Contributors internal class InterfaceProxyWithTargetInterfaceTargetContributor : InterfaceProxyTargetContributor { - public InterfaceProxyWithTargetInterfaceTargetContributor(Type proxyTargetType, bool allowChangeTarget, - INamingScope namingScope) - : base(proxyTargetType, allowChangeTarget, namingScope) + public InterfaceProxyWithTargetInterfaceTargetContributor(ProxyGenerationContext context, INamingScope namingScope, + Type proxyTargetType, bool allowChangeTarget) + : base(context, namingScope, proxyTargetType, allowChangeTarget) { } protected override MembersCollector GetCollectorForInterface(Type @interface) { - return new InterfaceMembersCollector(@interface); + return new InterfaceMembersCollector(Context, @interface); } } } \ No newline at end of file diff --git a/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyWithoutTargetContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyWithoutTargetContributor.cs index 3910e426e3..95c99ef347 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyWithoutTargetContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyWithoutTargetContributor.cs @@ -27,8 +27,9 @@ internal class InterfaceProxyWithoutTargetContributor : CompositeTypeContributor private readonly GetTargetExpressionDelegate getTargetExpression; protected bool canChangeTarget = false; - public InterfaceProxyWithoutTargetContributor(INamingScope namingScope, GetTargetExpressionDelegate getTarget) - : base(namingScope) + public InterfaceProxyWithoutTargetContributor(ProxyGenerationContext context, INamingScope namingScope, + GetTargetExpressionDelegate getTarget) + : base(context, namingScope) { getTargetExpression = getTarget; } @@ -37,7 +38,7 @@ protected override IEnumerable GetCollectors() { foreach (var @interface in interfaces) { - var item = new InterfaceMembersCollector(@interface); + var item = new InterfaceMembersCollector(Context, @interface); yield return item; } } diff --git a/src/Castle.Core/DynamicProxy/Contributors/MembersCollector.cs b/src/Castle.Core/DynamicProxy/Contributors/MembersCollector.cs index 487a8903f3..a3581ebd17 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/MembersCollector.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/MembersCollector.cs @@ -28,13 +28,20 @@ internal abstract class MembersCollector private const BindingFlags Flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; private ILogger logger = NullLogger.Instance; + private readonly ProxyGenerationContext context; protected readonly Type type; - protected MembersCollector(Type type) + protected MembersCollector(ProxyGenerationContext context, Type type) { + this.context = context; this.type = type; } + protected ProxyGenerationContext Context + { + get { return context; } + } + public ILogger Logger { get { return logger; } diff --git a/src/Castle.Core/DynamicProxy/Contributors/MixinContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/MixinContributor.cs index e6070994ef..a07e906fcd 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/MixinContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/MixinContributor.cs @@ -31,8 +31,9 @@ internal class MixinContributor : CompositeTypeContributor private readonly IDictionary fields = new SortedDictionary(new FieldReferenceComparer()); private readonly GetTargetExpressionDelegate getTargetExpression; - public MixinContributor(INamingScope namingScope, bool canChangeTarget) - : base(namingScope) + public MixinContributor(ProxyGenerationContext context, INamingScope namingScope, + bool canChangeTarget) + : base(context, namingScope) { this.canChangeTarget = canChangeTarget; getTargetExpression = BuildGetTargetExpression(); @@ -76,12 +77,12 @@ protected override IEnumerable GetCollectors() MembersCollector item; if (@interface.IsInterface) { - item = new InterfaceMembersCollector(@interface); + item = new InterfaceMembersCollector(Context, @interface); } else { Debug.Assert(@interface.IsDelegateType()); - item = new DelegateTypeMembersCollector(@interface); + item = new DelegateTypeMembersCollector(Context, @interface); } yield return item; } diff --git a/src/Castle.Core/DynamicProxy/Contributors/WrappedClassMembersCollector.cs b/src/Castle.Core/DynamicProxy/Contributors/WrappedClassMembersCollector.cs index eca221461b..55ad52c232 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/WrappedClassMembersCollector.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/WrappedClassMembersCollector.cs @@ -24,7 +24,8 @@ namespace Castle.DynamicProxy.Contributors internal class WrappedClassMembersCollector : ClassMembersCollector { - public WrappedClassMembersCollector(Type type) : base(type) + public WrappedClassMembersCollector(ProxyGenerationContext context, Type type) + : base(context, type) { } diff --git a/src/Castle.Core/DynamicProxy/DefaultProxyBuilder.cs b/src/Castle.Core/DynamicProxy/DefaultProxyBuilder.cs index c8454aa795..72af39242c 100644 --- a/src/Castle.Core/DynamicProxy/DefaultProxyBuilder.cs +++ b/src/Castle.Core/DynamicProxy/DefaultProxyBuilder.cs @@ -65,7 +65,9 @@ public Type CreateClassProxyType(Type classToProxy, Type[] additionalInterfacesT AssertValidTypes(additionalInterfacesToProxy, nameof(additionalInterfacesToProxy)); AssertValidMixins(options, nameof(options)); - var generator = new ClassProxyGenerator(scope, classToProxy, additionalInterfacesToProxy, options) { Logger = logger }; + var context = new ProxyGenerationContext(); + + var generator = new ClassProxyGenerator(context, scope, classToProxy, additionalInterfacesToProxy, options) { Logger = logger }; return generator.GetProxyType(); } @@ -76,7 +78,9 @@ public Type CreateClassProxyTypeWithTarget(Type classToProxy, Type[] additionalI AssertValidTypes(additionalInterfacesToProxy, nameof(additionalInterfacesToProxy)); AssertValidMixins(options, nameof(options)); - var generator = new ClassProxyWithTargetGenerator(scope, classToProxy, additionalInterfacesToProxy, options) + var context = new ProxyGenerationContext(); + + var generator = new ClassProxyWithTargetGenerator(context, scope, classToProxy, additionalInterfacesToProxy, options) { Logger = logger }; return generator.GetProxyType(); } @@ -89,7 +93,9 @@ public Type CreateInterfaceProxyTypeWithTarget(Type interfaceToProxy, Type[] add AssertValidTypes(additionalInterfacesToProxy, nameof(additionalInterfacesToProxy)); AssertValidMixins(options, nameof(options)); - var generator = new InterfaceProxyWithTargetGenerator(scope, interfaceToProxy, additionalInterfacesToProxy, targetType, options) { Logger = logger }; + var context = new ProxyGenerationContext(); + + var generator = new InterfaceProxyWithTargetGenerator(context, scope, interfaceToProxy, additionalInterfacesToProxy, targetType, options) { Logger = logger }; return generator.GetProxyType(); } @@ -100,7 +106,9 @@ public Type CreateInterfaceProxyTypeWithTargetInterface(Type interfaceToProxy, T AssertValidTypes(additionalInterfacesToProxy, nameof(additionalInterfacesToProxy)); AssertValidMixins(options, nameof(options)); - var generator = new InterfaceProxyWithTargetInterfaceGenerator(scope, interfaceToProxy, additionalInterfacesToProxy, interfaceToProxy, options) { Logger = logger }; + var context = new ProxyGenerationContext(); + + var generator = new InterfaceProxyWithTargetInterfaceGenerator(context, scope, interfaceToProxy, additionalInterfacesToProxy, interfaceToProxy, options) { Logger = logger }; return generator.GetProxyType(); } @@ -111,7 +119,9 @@ public Type CreateInterfaceProxyTypeWithoutTarget(Type interfaceToProxy, Type[] AssertValidTypes(additionalInterfacesToProxy, nameof(additionalInterfacesToProxy)); AssertValidMixins(options, nameof(options)); - var generator = new InterfaceProxyWithoutTargetGenerator(scope, interfaceToProxy, additionalInterfacesToProxy, typeof(object), options) { Logger = logger }; + var context = new ProxyGenerationContext(); + + var generator = new InterfaceProxyWithoutTargetGenerator(context, scope, interfaceToProxy, additionalInterfacesToProxy, typeof(object), options) { Logger = logger }; return generator.GetProxyType(); } diff --git a/src/Castle.Core/DynamicProxy/Generators/BaseClassProxyGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/BaseClassProxyGenerator.cs index 02f6012380..aae5a5b71d 100644 --- a/src/Castle.Core/DynamicProxy/Generators/BaseClassProxyGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/BaseClassProxyGenerator.cs @@ -25,8 +25,10 @@ namespace Castle.DynamicProxy.Generators internal abstract class BaseClassProxyGenerator : BaseProxyGenerator { - protected BaseClassProxyGenerator(ModuleScope scope, Type targetType, Type[] interfaces, ProxyGenerationOptions options) - : base(scope, targetType, interfaces, options) + protected BaseClassProxyGenerator(ProxyGenerationContext context, ModuleScope scope, + Type targetType, Type[] interfaces, + ProxyGenerationOptions options) + : base(context, scope, targetType, interfaces, options) { EnsureDoesNotImplementIProxyTargetAccessor(targetType, nameof(targetType)); } @@ -122,7 +124,7 @@ private IEnumerable GetTypeImplementerMapping(out IEnumerable GetTypeImplementerMapping(out IEnumerable 0) { - var additionalInterfacesContributor = new InterfaceProxyWithoutTargetContributor(namingScope, (c, m) => NullExpression.Instance) { Logger = Logger }; + var additionalInterfacesContributor = new InterfaceProxyWithoutTargetContributor(Context, namingScope, (c, m) => NullExpression.Instance) { Logger = Logger }; contributorsList.Add(additionalInterfacesContributor); foreach (var @interface in interfaces) diff --git a/src/Castle.Core/DynamicProxy/Generators/BaseInterfaceProxyGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/BaseInterfaceProxyGenerator.cs index 84e6b1186b..c1fc79f99c 100644 --- a/src/Castle.Core/DynamicProxy/Generators/BaseInterfaceProxyGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/BaseInterfaceProxyGenerator.cs @@ -33,9 +33,10 @@ internal abstract class BaseInterfaceProxyGenerator : BaseProxyGenerator protected FieldReference targetField; - protected BaseInterfaceProxyGenerator(ModuleScope scope, Type targetType, Type[] interfaces, - Type proxyTargetType, ProxyGenerationOptions options) - : base(scope, targetType, interfaces, options) + protected BaseInterfaceProxyGenerator(ProxyGenerationContext context, ModuleScope scope, + Type targetType, Type[] interfaces, Type proxyTargetType, + ProxyGenerationOptions options) + : base(context, scope, targetType, interfaces, options) { CheckNotGenericTypeDefinition(proxyTargetType, nameof(proxyTargetType)); EnsureValidBaseType(ProxyGenerationOptions.BaseTypeForInterfaceProxy); @@ -145,7 +146,7 @@ protected override Type GenerateType(string typeName, INamingScope namingScope) protected virtual InterfaceProxyWithoutTargetContributor GetContributorForAdditionalInterfaces( INamingScope namingScope) { - return new InterfaceProxyWithoutTargetContributor(namingScope, (c, m) => NullExpression.Instance) { Logger = Logger }; + return new InterfaceProxyWithoutTargetContributor(Context, namingScope, (c, m) => NullExpression.Instance) { Logger = Logger }; } protected virtual IEnumerable GetTypeImplementerMapping(Type proxyTargetType, @@ -164,7 +165,7 @@ protected virtual IEnumerable GetTypeImplementerMapping(Type proxyTargetTy // 2. then mixins if (ProxyGenerationOptions.HasMixins) { - var mixinContributor = new MixinContributor(namingScope, AllowChangeTarget) { Logger = Logger }; + var mixinContributor = new MixinContributor(Context, namingScope, AllowChangeTarget) { Logger = Logger }; contributorsList.Add(mixinContributor); foreach (var mixinInterface in ProxyGenerationOptions.MixinData.MixinInterfaces) diff --git a/src/Castle.Core/DynamicProxy/Generators/BaseProxyGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/BaseProxyGenerator.cs index 2d790409d6..c62e9a62a8 100644 --- a/src/Castle.Core/DynamicProxy/Generators/BaseProxyGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/BaseProxyGenerator.cs @@ -38,15 +38,19 @@ internal abstract class BaseProxyGenerator { protected readonly Type targetType; protected readonly Type[] interfaces; + private readonly ProxyGenerationContext context; private readonly ModuleScope scope; private ILogger logger = NullLogger.Instance; private ProxyGenerationOptions proxyGenerationOptions; - protected BaseProxyGenerator(ModuleScope scope, Type targetType, Type[] interfaces, ProxyGenerationOptions proxyGenerationOptions) + protected BaseProxyGenerator(ProxyGenerationContext context, ModuleScope scope, + Type targetType, Type[] interfaces, + ProxyGenerationOptions proxyGenerationOptions) { CheckNotGenericTypeDefinition(targetType, nameof(targetType)); CheckNotGenericTypeDefinitions(interfaces, nameof(interfaces)); + this.context = context; this.scope = scope; this.targetType = targetType; this.interfaces = TypeUtil.GetAllInterfaces(interfaces); @@ -60,6 +64,11 @@ public ILogger Logger set { logger = value; } } + protected ProxyGenerationContext Context + { + get { return context; } + } + protected ProxyGenerationOptions ProxyGenerationOptions { get { return proxyGenerationOptions; } diff --git a/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs index 7d713f15dc..c099e2c01f 100644 --- a/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs @@ -24,8 +24,10 @@ namespace Castle.DynamicProxy.Generators internal sealed class ClassProxyGenerator : BaseClassProxyGenerator { - public ClassProxyGenerator(ModuleScope scope, Type targetType, Type[] interfaces, ProxyGenerationOptions options) - : base(scope, targetType, interfaces, options) + public ClassProxyGenerator(ProxyGenerationContext context, ModuleScope scope, + Type targetType, Type[] interfaces, + ProxyGenerationOptions options) + : base(context, scope, targetType, interfaces, options) { } @@ -45,7 +47,7 @@ protected override SerializableContributor GetSerializableContributor() protected override CompositeTypeContributor GetProxyTargetContributor(INamingScope namingScope) { - return new ClassProxyTargetContributor(targetType, namingScope) { Logger = Logger }; + return new ClassProxyTargetContributor(Context, namingScope, targetType) { Logger = Logger }; } protected override ProxyTargetAccessorContributor GetProxyTargetAccessorContributor() diff --git a/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs index c7f6fe6ad5..f1d0551fb8 100644 --- a/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs @@ -31,9 +31,10 @@ internal sealed class ClassProxyWithTargetGenerator : BaseClassProxyGenerator { private FieldReference targetField; - public ClassProxyWithTargetGenerator(ModuleScope scope, Type targetType, Type[] interfaces, + public ClassProxyWithTargetGenerator(ProxyGenerationContext context, ModuleScope scope, + Type targetType, Type[] interfaces, ProxyGenerationOptions options) - : base(scope, targetType, interfaces, options) + : base(context, scope, targetType, interfaces, options) { } @@ -59,7 +60,7 @@ protected override SerializableContributor GetSerializableContributor() protected override CompositeTypeContributor GetProxyTargetContributor(INamingScope namingScope) { - return new ClassProxyWithTargetTargetContributor(targetType, namingScope) { Logger = Logger }; + return new ClassProxyWithTargetTargetContributor(Context, namingScope, targetType) { Logger = Logger }; } protected override ProxyTargetAccessorContributor GetProxyTargetAccessorContributor() diff --git a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs index dc4ac91c41..6ac8b9a89f 100644 --- a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs @@ -23,9 +23,10 @@ namespace Castle.DynamicProxy.Generators internal sealed class InterfaceProxyWithTargetGenerator : BaseInterfaceProxyGenerator { - public InterfaceProxyWithTargetGenerator(ModuleScope scope, Type targetType, Type[] interfaces, - Type proxyTargetType, ProxyGenerationOptions options) - : base(scope, targetType, interfaces, proxyTargetType, options) + public InterfaceProxyWithTargetGenerator(ProxyGenerationContext context, ModuleScope scope, + Type targetType, Type[] interfaces, Type proxyTargetType, + ProxyGenerationOptions options) + : base(context, scope, targetType, interfaces, proxyTargetType, options) { } protected override bool AllowChangeTarget => false; @@ -34,7 +35,7 @@ public InterfaceProxyWithTargetGenerator(ModuleScope scope, Type targetType, Typ protected override CompositeTypeContributor GetProxyTargetContributor(Type proxyTargetType, INamingScope namingScope) { - return new InterfaceProxyTargetContributor(proxyTargetType, AllowChangeTarget, namingScope) { Logger = Logger }; + return new InterfaceProxyTargetContributor(Context, namingScope, proxyTargetType, AllowChangeTarget) { Logger = Logger }; } protected override ProxyTargetAccessorContributor GetProxyTargetAccessorContributor() diff --git a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetInterfaceGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetInterfaceGenerator.cs index 9ae7b102c9..070e0da747 100644 --- a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetInterfaceGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetInterfaceGenerator.cs @@ -26,9 +26,10 @@ namespace Castle.DynamicProxy.Generators internal sealed class InterfaceProxyWithTargetInterfaceGenerator : BaseInterfaceProxyGenerator { - public InterfaceProxyWithTargetInterfaceGenerator(ModuleScope scope, Type targetType, Type[] interfaces, - Type proxyTargetType, ProxyGenerationOptions options) - : base(scope, targetType, interfaces, proxyTargetType, options) + public InterfaceProxyWithTargetInterfaceGenerator(ProxyGenerationContext context, ModuleScope scope, + Type targetType, Type[] interfaces, Type proxyTargetType, + ProxyGenerationOptions options) + : base(context, scope, targetType, interfaces, proxyTargetType, options) { } @@ -38,7 +39,7 @@ public InterfaceProxyWithTargetInterfaceGenerator(ModuleScope scope, Type target protected override CompositeTypeContributor GetProxyTargetContributor(Type proxyTargetType, INamingScope namingScope) { - return new InterfaceProxyWithTargetInterfaceTargetContributor(proxyTargetType, AllowChangeTarget, namingScope) { Logger = Logger }; + return new InterfaceProxyWithTargetInterfaceTargetContributor(Context, namingScope, proxyTargetType, AllowChangeTarget) { Logger = Logger }; } protected override ProxyTargetAccessorContributor GetProxyTargetAccessorContributor() @@ -57,7 +58,7 @@ protected override void AddMappingForAdditionalInterfaces(CompositeTypeContribut protected override InterfaceProxyWithoutTargetContributor GetContributorForAdditionalInterfaces( INamingScope namingScope) { - return new InterfaceProxyWithOptionalTargetContributor(namingScope, GetTargetExpression, GetTarget) + return new InterfaceProxyWithOptionalTargetContributor(Context, namingScope, GetTargetExpression, GetTarget) { Logger = Logger }; } diff --git a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithoutTargetGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithoutTargetGenerator.cs index d916e4dc62..5f41890bfd 100644 --- a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithoutTargetGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithoutTargetGenerator.cs @@ -23,9 +23,10 @@ namespace Castle.DynamicProxy.Generators internal sealed class InterfaceProxyWithoutTargetGenerator : BaseInterfaceProxyGenerator { - public InterfaceProxyWithoutTargetGenerator(ModuleScope scope, Type targetType, Type[] interfaces, - Type proxyTargetType, ProxyGenerationOptions options) - : base(scope, targetType, interfaces, proxyTargetType, options) + public InterfaceProxyWithoutTargetGenerator(ProxyGenerationContext context, ModuleScope scope, + Type targetType, Type[] interfaces, Type proxyTargetType, + ProxyGenerationOptions options) + : base(context, scope, targetType, interfaces, proxyTargetType, options) { } @@ -35,7 +36,7 @@ public InterfaceProxyWithoutTargetGenerator(ModuleScope scope, Type targetType, protected override CompositeTypeContributor GetProxyTargetContributor(Type proxyTargetType, INamingScope namingScope) { - return new InterfaceProxyWithoutTargetContributor(namingScope, (c, m) => NullExpression.Instance) { Logger = Logger }; + return new InterfaceProxyWithoutTargetContributor(Context, namingScope, (c, m) => NullExpression.Instance) { Logger = Logger }; } protected override ProxyTargetAccessorContributor GetProxyTargetAccessorContributor() diff --git a/src/Castle.Core/DynamicProxy/Serialization/ProxyObjectReference.cs b/src/Castle.Core/DynamicProxy/Serialization/ProxyObjectReference.cs index 461715ee97..60cc7fa009 100644 --- a/src/Castle.Core/DynamicProxy/Serialization/ProxyObjectReference.cs +++ b/src/Castle.Core/DynamicProxy/Serialization/ProxyObjectReference.cs @@ -131,7 +131,8 @@ protected virtual object RecreateProxy() private object RecreateClassProxyWithTarget() { - var generator = new ClassProxyWithTargetGenerator(scope, baseType, interfaces, proxyGenerationOptions); + var context = new ProxyGenerationContext(); + var generator = new ClassProxyWithTargetGenerator(context, scope, baseType, interfaces, proxyGenerationOptions); var proxyType = generator.GetProxyType(); return InstantiateClassProxy(proxyType); } @@ -141,18 +142,20 @@ public object RecreateInterfaceProxy(string generatorType) var @interface = DeserializeTypeFromString("__theInterface"); var targetType = DeserializeTypeFromString("__targetFieldType"); + var context = new ProxyGenerationContext(); + BaseInterfaceProxyGenerator generator; if (generatorType == ProxyTypeConstants.InterfaceWithTarget) { - generator = new InterfaceProxyWithTargetGenerator(scope, @interface, interfaces, targetType, proxyGenerationOptions); + generator = new InterfaceProxyWithTargetGenerator(context, scope, @interface, interfaces, targetType, proxyGenerationOptions); } else if (generatorType == ProxyTypeConstants.InterfaceWithoutTarget) { - generator = new InterfaceProxyWithoutTargetGenerator(scope, @interface, interfaces, targetType, proxyGenerationOptions); + generator = new InterfaceProxyWithoutTargetGenerator(context, scope, @interface, interfaces, targetType, proxyGenerationOptions); } else if (generatorType == ProxyTypeConstants.InterfaceWithTargetInterface) { - generator = new InterfaceProxyWithTargetInterfaceGenerator(scope, @interface, interfaces, targetType, proxyGenerationOptions); + generator = new InterfaceProxyWithTargetInterfaceGenerator(context, scope, @interface, interfaces, targetType, proxyGenerationOptions); } else { @@ -168,7 +171,8 @@ public object RecreateInterfaceProxy(string generatorType) public object RecreateClassProxy() { - var generator = new ClassProxyGenerator(scope, baseType, interfaces, proxyGenerationOptions); + var context = new ProxyGenerationContext(); + var generator = new ClassProxyGenerator(context, scope, baseType, interfaces, proxyGenerationOptions); var proxyType = generator.GetProxyType(); return InstantiateClassProxy(proxyType); } From 35e3bb71d8bd45485051366ac7264ce5b0c28118 Mon Sep 17 00:00:00 2001 From: Dominique Schuppli Date: Fri, 12 Feb 2021 00:53:37 +0100 Subject: [PATCH 03/13] Add `Logger` to context --- .../ClassProxyTargetContributor.cs | 5 ++--- .../ClassProxyWithTargetTargetContributor.cs | 5 ++--- .../Contributors/CompositeTypeContributor.cs | 7 ------- .../InterfaceProxyTargetContributor.cs | 4 +--- .../Contributors/MembersCollector.cs | 15 ++++--------- .../DynamicProxy/DefaultProxyBuilder.cs | 21 +++++++++---------- .../Generators/BaseClassProxyGenerator.cs | 4 ++-- .../Generators/BaseInterfaceProxyGenerator.cs | 4 ++-- .../Generators/BaseProxyGenerator.cs | 19 ++++++----------- .../Generators/ClassProxyGenerator.cs | 2 +- .../ClassProxyWithTargetGenerator.cs | 2 +- .../InterfaceProxyWithTargetGenerator.cs | 2 +- ...erfaceProxyWithTargetInterfaceGenerator.cs | 5 ++--- .../InterfaceProxyWithoutTargetGenerator.cs | 2 +- .../DynamicProxy/ProxyGenerationContext.cs | 8 +++++++ 15 files changed, 43 insertions(+), 62 deletions(-) diff --git a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyTargetContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyTargetContributor.cs index 068cc2074f..5db8019bfb 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyTargetContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyTargetContributor.cs @@ -40,13 +40,12 @@ public ClassProxyTargetContributor(ProxyGenerationContext context, INamingScope protected override IEnumerable GetCollectors() { - var targetItem = new ClassMembersCollector(Context, targetType) { Logger = Logger }; + var targetItem = new ClassMembersCollector(Context, targetType); yield return targetItem; foreach (var @interface in interfaces) { - var item = new InterfaceMembersOnClassCollector(Context, @interface, true, - targetType.GetInterfaceMap(@interface)) { Logger = Logger }; + var item = new InterfaceMembersOnClassCollector(Context, @interface, true, targetType.GetInterfaceMap(@interface)); yield return item; } } diff --git a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyWithTargetTargetContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyWithTargetTargetContributor.cs index 6b32416573..e653ce3bd3 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyWithTargetTargetContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyWithTargetTargetContributor.cs @@ -38,13 +38,12 @@ public ClassProxyWithTargetTargetContributor(ProxyGenerationContext context, INa protected override IEnumerable GetCollectors() { - var targetItem = new WrappedClassMembersCollector(Context, targetType) { Logger = Logger }; + var targetItem = new WrappedClassMembersCollector(Context, targetType); yield return targetItem; foreach (var @interface in interfaces) { - var item = new InterfaceMembersOnClassCollector(Context, @interface, true, - targetType.GetInterfaceMap(@interface)) { Logger = Logger }; + var item = new InterfaceMembersOnClassCollector(Context, @interface, true, targetType.GetInterfaceMap(@interface)); yield return item; } } diff --git a/src/Castle.Core/DynamicProxy/Contributors/CompositeTypeContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/CompositeTypeContributor.cs index f448e95397..73a841bd3f 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/CompositeTypeContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/CompositeTypeContributor.cs @@ -31,7 +31,6 @@ internal abstract class CompositeTypeContributor : ITypeContributor protected readonly ICollection interfaces = new HashSet(); - private ILogger logger = NullLogger.Instance; private readonly List properties = new List(); private readonly List events = new List(); private readonly List methods = new List(); @@ -42,12 +41,6 @@ protected CompositeTypeContributor(ProxyGenerationContext context, INamingScope this.namingScope = namingScope; } - public ILogger Logger - { - get { return logger; } - set { logger = value; } - } - protected ProxyGenerationContext Context { get { return context; } diff --git a/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyTargetContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyTargetContributor.cs index b767a48b4f..6e0a24446e 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyTargetContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyTargetContributor.cs @@ -40,15 +40,13 @@ protected override IEnumerable GetCollectors() foreach (var @interface in interfaces) { var item = GetCollectorForInterface(@interface); - item.Logger = Logger; yield return item; } } protected virtual MembersCollector GetCollectorForInterface(Type @interface) { - return new InterfaceMembersOnClassCollector(Context, @interface, false, - proxyTargetType.GetInterfaceMap(@interface)); + return new InterfaceMembersOnClassCollector(Context, @interface, false, proxyTargetType.GetInterfaceMap(@interface)); } protected override MethodGenerator GetMethodGenerator(MetaMethod method, ClassEmitter @class, diff --git a/src/Castle.Core/DynamicProxy/Contributors/MembersCollector.cs b/src/Castle.Core/DynamicProxy/Contributors/MembersCollector.cs index a3581ebd17..de4e5108a0 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/MembersCollector.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/MembersCollector.cs @@ -26,7 +26,6 @@ namespace Castle.DynamicProxy.Contributors internal abstract class MembersCollector { private const BindingFlags Flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; - private ILogger logger = NullLogger.Instance; private readonly ProxyGenerationContext context; protected readonly Type type; @@ -42,12 +41,6 @@ protected ProxyGenerationContext Context get { return context; } } - public ILogger Logger - { - get { return logger; } - set { logger = value; } - } - public virtual void CollectMembersToProxy(IProxyGenerationHook hook, IMembersCollectorSink sink) { var checkedMethods = new HashSet(); @@ -195,8 +188,8 @@ protected bool AcceptMethodPreScreen(MethodInfo method, bool onlyVirtuals, IProx method.IsGetType() == false && method.IsMemberwiseClone() == false) { - Logger.DebugFormat("Excluded non-overridable method {0} on {1} because it cannot be intercepted.", method.Name, - method.DeclaringType.FullName); + Context.Logger.DebugFormat("Excluded non-overridable method {0} on {1} because it cannot be intercepted.", method.Name, + method.DeclaringType.FullName); hook.NonProxyableMemberNotification(type, method); } return false; @@ -205,8 +198,8 @@ protected bool AcceptMethodPreScreen(MethodInfo method, bool onlyVirtuals, IProx // we can never intercept a sealed (final) method if (method.IsFinal) { - Logger.DebugFormat("Excluded sealed method {0} on {1} because it cannot be intercepted.", method.Name, - method.DeclaringType.FullName); + Context.Logger.DebugFormat("Excluded sealed method {0} on {1} because it cannot be intercepted.", method.Name, + method.DeclaringType.FullName); return false; } diff --git a/src/Castle.Core/DynamicProxy/DefaultProxyBuilder.cs b/src/Castle.Core/DynamicProxy/DefaultProxyBuilder.cs index 72af39242c..5831977670 100644 --- a/src/Castle.Core/DynamicProxy/DefaultProxyBuilder.cs +++ b/src/Castle.Core/DynamicProxy/DefaultProxyBuilder.cs @@ -65,9 +65,9 @@ public Type CreateClassProxyType(Type classToProxy, Type[] additionalInterfacesT AssertValidTypes(additionalInterfacesToProxy, nameof(additionalInterfacesToProxy)); AssertValidMixins(options, nameof(options)); - var context = new ProxyGenerationContext(); + var context = new ProxyGenerationContext(Logger); - var generator = new ClassProxyGenerator(context, scope, classToProxy, additionalInterfacesToProxy, options) { Logger = logger }; + var generator = new ClassProxyGenerator(context, scope, classToProxy, additionalInterfacesToProxy, options); return generator.GetProxyType(); } @@ -78,10 +78,9 @@ public Type CreateClassProxyTypeWithTarget(Type classToProxy, Type[] additionalI AssertValidTypes(additionalInterfacesToProxy, nameof(additionalInterfacesToProxy)); AssertValidMixins(options, nameof(options)); - var context = new ProxyGenerationContext(); + var context = new ProxyGenerationContext(Logger); - var generator = new ClassProxyWithTargetGenerator(context, scope, classToProxy, additionalInterfacesToProxy, options) - { Logger = logger }; + var generator = new ClassProxyWithTargetGenerator(context, scope, classToProxy, additionalInterfacesToProxy, options); return generator.GetProxyType(); } @@ -93,9 +92,9 @@ public Type CreateInterfaceProxyTypeWithTarget(Type interfaceToProxy, Type[] add AssertValidTypes(additionalInterfacesToProxy, nameof(additionalInterfacesToProxy)); AssertValidMixins(options, nameof(options)); - var context = new ProxyGenerationContext(); + var context = new ProxyGenerationContext(Logger); - var generator = new InterfaceProxyWithTargetGenerator(context, scope, interfaceToProxy, additionalInterfacesToProxy, targetType, options) { Logger = logger }; + var generator = new InterfaceProxyWithTargetGenerator(context, scope, interfaceToProxy, additionalInterfacesToProxy, targetType, options); return generator.GetProxyType(); } @@ -106,9 +105,9 @@ public Type CreateInterfaceProxyTypeWithTargetInterface(Type interfaceToProxy, T AssertValidTypes(additionalInterfacesToProxy, nameof(additionalInterfacesToProxy)); AssertValidMixins(options, nameof(options)); - var context = new ProxyGenerationContext(); + var context = new ProxyGenerationContext(Logger); - var generator = new InterfaceProxyWithTargetInterfaceGenerator(context, scope, interfaceToProxy, additionalInterfacesToProxy, interfaceToProxy, options) { Logger = logger }; + var generator = new InterfaceProxyWithTargetInterfaceGenerator(context, scope, interfaceToProxy, additionalInterfacesToProxy, interfaceToProxy, options); return generator.GetProxyType(); } @@ -119,9 +118,9 @@ public Type CreateInterfaceProxyTypeWithoutTarget(Type interfaceToProxy, Type[] AssertValidTypes(additionalInterfacesToProxy, nameof(additionalInterfacesToProxy)); AssertValidMixins(options, nameof(options)); - var context = new ProxyGenerationContext(); + var context = new ProxyGenerationContext(Logger); - var generator = new InterfaceProxyWithoutTargetGenerator(context, scope, interfaceToProxy, additionalInterfacesToProxy, typeof(object), options) { Logger = logger }; + var generator = new InterfaceProxyWithoutTargetGenerator(context, scope, interfaceToProxy, additionalInterfacesToProxy, typeof(object), options); return generator.GetProxyType(); } diff --git a/src/Castle.Core/DynamicProxy/Generators/BaseClassProxyGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/BaseClassProxyGenerator.cs index aae5a5b71d..72f814cc1e 100644 --- a/src/Castle.Core/DynamicProxy/Generators/BaseClassProxyGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/BaseClassProxyGenerator.cs @@ -124,7 +124,7 @@ private IEnumerable GetTypeImplementerMapping(out IEnumerable GetTypeImplementerMapping(out IEnumerable 0) { - var additionalInterfacesContributor = new InterfaceProxyWithoutTargetContributor(Context, namingScope, (c, m) => NullExpression.Instance) { Logger = Logger }; + var additionalInterfacesContributor = new InterfaceProxyWithoutTargetContributor(Context, namingScope, (c, m) => NullExpression.Instance); contributorsList.Add(additionalInterfacesContributor); foreach (var @interface in interfaces) diff --git a/src/Castle.Core/DynamicProxy/Generators/BaseInterfaceProxyGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/BaseInterfaceProxyGenerator.cs index c1fc79f99c..39631d84e9 100644 --- a/src/Castle.Core/DynamicProxy/Generators/BaseInterfaceProxyGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/BaseInterfaceProxyGenerator.cs @@ -146,7 +146,7 @@ protected override Type GenerateType(string typeName, INamingScope namingScope) protected virtual InterfaceProxyWithoutTargetContributor GetContributorForAdditionalInterfaces( INamingScope namingScope) { - return new InterfaceProxyWithoutTargetContributor(Context, namingScope, (c, m) => NullExpression.Instance) { Logger = Logger }; + return new InterfaceProxyWithoutTargetContributor(Context, namingScope, (c, m) => NullExpression.Instance); } protected virtual IEnumerable GetTypeImplementerMapping(Type proxyTargetType, @@ -165,7 +165,7 @@ protected virtual IEnumerable GetTypeImplementerMapping(Type proxyTargetTy // 2. then mixins if (ProxyGenerationOptions.HasMixins) { - var mixinContributor = new MixinContributor(Context, namingScope, AllowChangeTarget) { Logger = Logger }; + var mixinContributor = new MixinContributor(Context, namingScope, AllowChangeTarget); contributorsList.Add(mixinContributor); foreach (var mixinInterface in ProxyGenerationOptions.MixinData.MixinInterfaces) diff --git a/src/Castle.Core/DynamicProxy/Generators/BaseProxyGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/BaseProxyGenerator.cs index c62e9a62a8..22b3bcd47d 100644 --- a/src/Castle.Core/DynamicProxy/Generators/BaseProxyGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/BaseProxyGenerator.cs @@ -40,7 +40,6 @@ internal abstract class BaseProxyGenerator protected readonly Type[] interfaces; private readonly ProxyGenerationContext context; private readonly ModuleScope scope; - private ILogger logger = NullLogger.Instance; private ProxyGenerationOptions proxyGenerationOptions; protected BaseProxyGenerator(ProxyGenerationContext context, ModuleScope scope, @@ -58,12 +57,6 @@ protected BaseProxyGenerator(ProxyGenerationContext context, ModuleScope scope, this.proxyGenerationOptions.Initialize(); } - public ILogger Logger - { - get { return logger; } - set { logger = value; } - } - protected ProxyGenerationContext Context { get { return context; } @@ -86,7 +79,7 @@ public Type GetProxyType() var proxyType = Scope.TypeCache.GetOrAdd(GetCacheKey(), cacheKey => { notFoundInTypeCache = true; - Logger.DebugFormat("No cached proxy type was found for target type {0}.", targetType.FullName); + Context.Logger.DebugFormat("No cached proxy type was found for target type {0}.", targetType.FullName); EnsureOptionsOverrideEqualsAndGetHashCode(); @@ -96,7 +89,7 @@ public Type GetProxyType() if (!notFoundInTypeCache) { - Logger.DebugFormat("Found cached proxy type {0} for target type {1}.", proxyType.FullName, targetType.FullName); + Context.Logger.DebugFormat("Found cached proxy type {0} for target type {1}.", proxyType.FullName, targetType.FullName); } return proxyType; @@ -209,14 +202,14 @@ protected virtual void CreateTypeAttributes(ClassEmitter emitter) protected void EnsureOptionsOverrideEqualsAndGetHashCode() { - if (Logger.IsWarnEnabled) + if (Context.Logger.IsWarnEnabled) { // Check the proxy generation hook if (!OverridesEqualsAndGetHashCode(ProxyGenerationOptions.Hook.GetType())) { - Logger.WarnFormat("The IProxyGenerationHook type {0} does not override both Equals and GetHashCode. " + - "If these are not correctly overridden caching will fail to work causing performance problems.", - ProxyGenerationOptions.Hook.GetType().FullName); + Context.Logger.WarnFormat("The IProxyGenerationHook type {0} does not override both Equals and GetHashCode. " + + "If these are not correctly overridden caching will fail to work causing performance problems.", + ProxyGenerationOptions.Hook.GetType().FullName); } // Interceptor selectors no longer need to override Equals and GetHashCode diff --git a/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs index c099e2c01f..e93dc020f3 100644 --- a/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs @@ -47,7 +47,7 @@ protected override SerializableContributor GetSerializableContributor() protected override CompositeTypeContributor GetProxyTargetContributor(INamingScope namingScope) { - return new ClassProxyTargetContributor(Context, namingScope, targetType) { Logger = Logger }; + return new ClassProxyTargetContributor(Context, namingScope, targetType); } protected override ProxyTargetAccessorContributor GetProxyTargetAccessorContributor() diff --git a/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs index f1d0551fb8..b26dd3b21e 100644 --- a/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs @@ -60,7 +60,7 @@ protected override SerializableContributor GetSerializableContributor() protected override CompositeTypeContributor GetProxyTargetContributor(INamingScope namingScope) { - return new ClassProxyWithTargetTargetContributor(Context, namingScope, targetType) { Logger = Logger }; + return new ClassProxyWithTargetTargetContributor(Context, namingScope, targetType); } protected override ProxyTargetAccessorContributor GetProxyTargetAccessorContributor() diff --git a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs index 6ac8b9a89f..deac0635b1 100644 --- a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs @@ -35,7 +35,7 @@ public InterfaceProxyWithTargetGenerator(ProxyGenerationContext context, ModuleS protected override CompositeTypeContributor GetProxyTargetContributor(Type proxyTargetType, INamingScope namingScope) { - return new InterfaceProxyTargetContributor(Context, namingScope, proxyTargetType, AllowChangeTarget) { Logger = Logger }; + return new InterfaceProxyTargetContributor(Context, namingScope, proxyTargetType, AllowChangeTarget); } protected override ProxyTargetAccessorContributor GetProxyTargetAccessorContributor() diff --git a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetInterfaceGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetInterfaceGenerator.cs index 070e0da747..82a58b08bc 100644 --- a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetInterfaceGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetInterfaceGenerator.cs @@ -39,7 +39,7 @@ public InterfaceProxyWithTargetInterfaceGenerator(ProxyGenerationContext context protected override CompositeTypeContributor GetProxyTargetContributor(Type proxyTargetType, INamingScope namingScope) { - return new InterfaceProxyWithTargetInterfaceTargetContributor(Context, namingScope, proxyTargetType, AllowChangeTarget) { Logger = Logger }; + return new InterfaceProxyWithTargetInterfaceTargetContributor(Context, namingScope, proxyTargetType, AllowChangeTarget); } protected override ProxyTargetAccessorContributor GetProxyTargetAccessorContributor() @@ -58,8 +58,7 @@ protected override void AddMappingForAdditionalInterfaces(CompositeTypeContribut protected override InterfaceProxyWithoutTargetContributor GetContributorForAdditionalInterfaces( INamingScope namingScope) { - return new InterfaceProxyWithOptionalTargetContributor(Context, namingScope, GetTargetExpression, GetTarget) - { Logger = Logger }; + return new InterfaceProxyWithOptionalTargetContributor(Context, namingScope, GetTargetExpression, GetTarget); } private Reference GetTarget(ClassEmitter @class, MethodInfo method) diff --git a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithoutTargetGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithoutTargetGenerator.cs index 5f41890bfd..348d3975f3 100644 --- a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithoutTargetGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithoutTargetGenerator.cs @@ -36,7 +36,7 @@ public InterfaceProxyWithoutTargetGenerator(ProxyGenerationContext context, Modu protected override CompositeTypeContributor GetProxyTargetContributor(Type proxyTargetType, INamingScope namingScope) { - return new InterfaceProxyWithoutTargetContributor(Context, namingScope, (c, m) => NullExpression.Instance) { Logger = Logger }; + return new InterfaceProxyWithoutTargetContributor(Context, namingScope, (c, m) => NullExpression.Instance); } protected override ProxyTargetAccessorContributor GetProxyTargetAccessorContributor() diff --git a/src/Castle.Core/DynamicProxy/ProxyGenerationContext.cs b/src/Castle.Core/DynamicProxy/ProxyGenerationContext.cs index 92dae09528..3357f3b76a 100644 --- a/src/Castle.Core/DynamicProxy/ProxyGenerationContext.cs +++ b/src/Castle.Core/DynamicProxy/ProxyGenerationContext.cs @@ -14,7 +14,15 @@ namespace Castle.DynamicProxy { + using Castle.Core.Logging; + internal sealed class ProxyGenerationContext { + public ProxyGenerationContext(ILogger logger = null) + { + Logger = logger ?? NullLogger.Instance; + } + + public ILogger Logger { get; } } } From d8ba8d957a63b38e65d5188a38f52a7c9c11aa3c Mon Sep 17 00:00:00 2001 From: Dominique Schuppli Date: Fri, 12 Feb 2021 01:00:08 +0100 Subject: [PATCH 04/13] Add `Options` to context --- .../DynamicProxy/DefaultProxyBuilder.cs | 20 +++++++++---------- .../Generators/BaseClassProxyGenerator.cs | 5 ++--- .../Generators/BaseInterfaceProxyGenerator.cs | 5 ++--- .../Generators/BaseProxyGenerator.cs | 8 ++------ .../Generators/ClassProxyGenerator.cs | 5 ++--- .../ClassProxyWithTargetGenerator.cs | 5 ++--- .../InterfaceProxyWithTargetGenerator.cs | 5 ++--- ...erfaceProxyWithTargetInterfaceGenerator.cs | 5 ++--- .../InterfaceProxyWithoutTargetGenerator.cs | 5 ++--- .../DynamicProxy/ProxyGenerationContext.cs | 8 +++++++- .../Serialization/ProxyObjectReference.cs | 16 +++++++-------- 11 files changed, 41 insertions(+), 46 deletions(-) diff --git a/src/Castle.Core/DynamicProxy/DefaultProxyBuilder.cs b/src/Castle.Core/DynamicProxy/DefaultProxyBuilder.cs index 5831977670..8d1c665918 100644 --- a/src/Castle.Core/DynamicProxy/DefaultProxyBuilder.cs +++ b/src/Castle.Core/DynamicProxy/DefaultProxyBuilder.cs @@ -65,9 +65,9 @@ public Type CreateClassProxyType(Type classToProxy, Type[] additionalInterfacesT AssertValidTypes(additionalInterfacesToProxy, nameof(additionalInterfacesToProxy)); AssertValidMixins(options, nameof(options)); - var context = new ProxyGenerationContext(Logger); + var context = new ProxyGenerationContext(options, Logger); - var generator = new ClassProxyGenerator(context, scope, classToProxy, additionalInterfacesToProxy, options); + var generator = new ClassProxyGenerator(context, scope, classToProxy, additionalInterfacesToProxy); return generator.GetProxyType(); } @@ -78,9 +78,9 @@ public Type CreateClassProxyTypeWithTarget(Type classToProxy, Type[] additionalI AssertValidTypes(additionalInterfacesToProxy, nameof(additionalInterfacesToProxy)); AssertValidMixins(options, nameof(options)); - var context = new ProxyGenerationContext(Logger); + var context = new ProxyGenerationContext(options, Logger); - var generator = new ClassProxyWithTargetGenerator(context, scope, classToProxy, additionalInterfacesToProxy, options); + var generator = new ClassProxyWithTargetGenerator(context, scope, classToProxy, additionalInterfacesToProxy); return generator.GetProxyType(); } @@ -92,9 +92,9 @@ public Type CreateInterfaceProxyTypeWithTarget(Type interfaceToProxy, Type[] add AssertValidTypes(additionalInterfacesToProxy, nameof(additionalInterfacesToProxy)); AssertValidMixins(options, nameof(options)); - var context = new ProxyGenerationContext(Logger); + var context = new ProxyGenerationContext(options, Logger); - var generator = new InterfaceProxyWithTargetGenerator(context, scope, interfaceToProxy, additionalInterfacesToProxy, targetType, options); + var generator = new InterfaceProxyWithTargetGenerator(context, scope, interfaceToProxy, additionalInterfacesToProxy, targetType); return generator.GetProxyType(); } @@ -105,9 +105,9 @@ public Type CreateInterfaceProxyTypeWithTargetInterface(Type interfaceToProxy, T AssertValidTypes(additionalInterfacesToProxy, nameof(additionalInterfacesToProxy)); AssertValidMixins(options, nameof(options)); - var context = new ProxyGenerationContext(Logger); + var context = new ProxyGenerationContext(options, Logger); - var generator = new InterfaceProxyWithTargetInterfaceGenerator(context, scope, interfaceToProxy, additionalInterfacesToProxy, interfaceToProxy, options); + var generator = new InterfaceProxyWithTargetInterfaceGenerator(context, scope, interfaceToProxy, additionalInterfacesToProxy, interfaceToProxy); return generator.GetProxyType(); } @@ -118,9 +118,9 @@ public Type CreateInterfaceProxyTypeWithoutTarget(Type interfaceToProxy, Type[] AssertValidTypes(additionalInterfacesToProxy, nameof(additionalInterfacesToProxy)); AssertValidMixins(options, nameof(options)); - var context = new ProxyGenerationContext(Logger); + var context = new ProxyGenerationContext(options, Logger); - var generator = new InterfaceProxyWithoutTargetGenerator(context, scope, interfaceToProxy, additionalInterfacesToProxy, typeof(object), options); + var generator = new InterfaceProxyWithoutTargetGenerator(context, scope, interfaceToProxy, additionalInterfacesToProxy, typeof(object)); return generator.GetProxyType(); } diff --git a/src/Castle.Core/DynamicProxy/Generators/BaseClassProxyGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/BaseClassProxyGenerator.cs index 72f814cc1e..7c23e1fc8c 100644 --- a/src/Castle.Core/DynamicProxy/Generators/BaseClassProxyGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/BaseClassProxyGenerator.cs @@ -26,9 +26,8 @@ namespace Castle.DynamicProxy.Generators internal abstract class BaseClassProxyGenerator : BaseProxyGenerator { protected BaseClassProxyGenerator(ProxyGenerationContext context, ModuleScope scope, - Type targetType, Type[] interfaces, - ProxyGenerationOptions options) - : base(context, scope, targetType, interfaces, options) + Type targetType, Type[] interfaces) + : base(context, scope, targetType, interfaces) { EnsureDoesNotImplementIProxyTargetAccessor(targetType, nameof(targetType)); } diff --git a/src/Castle.Core/DynamicProxy/Generators/BaseInterfaceProxyGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/BaseInterfaceProxyGenerator.cs index 39631d84e9..3a3b834799 100644 --- a/src/Castle.Core/DynamicProxy/Generators/BaseInterfaceProxyGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/BaseInterfaceProxyGenerator.cs @@ -34,9 +34,8 @@ internal abstract class BaseInterfaceProxyGenerator : BaseProxyGenerator protected FieldReference targetField; protected BaseInterfaceProxyGenerator(ProxyGenerationContext context, ModuleScope scope, - Type targetType, Type[] interfaces, Type proxyTargetType, - ProxyGenerationOptions options) - : base(context, scope, targetType, interfaces, options) + Type targetType, Type[] interfaces, Type proxyTargetType) + : base(context, scope, targetType, interfaces) { CheckNotGenericTypeDefinition(proxyTargetType, nameof(proxyTargetType)); EnsureValidBaseType(ProxyGenerationOptions.BaseTypeForInterfaceProxy); diff --git a/src/Castle.Core/DynamicProxy/Generators/BaseProxyGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/BaseProxyGenerator.cs index 22b3bcd47d..cb8a8d6ff8 100644 --- a/src/Castle.Core/DynamicProxy/Generators/BaseProxyGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/BaseProxyGenerator.cs @@ -40,11 +40,9 @@ internal abstract class BaseProxyGenerator protected readonly Type[] interfaces; private readonly ProxyGenerationContext context; private readonly ModuleScope scope; - private ProxyGenerationOptions proxyGenerationOptions; protected BaseProxyGenerator(ProxyGenerationContext context, ModuleScope scope, - Type targetType, Type[] interfaces, - ProxyGenerationOptions proxyGenerationOptions) + Type targetType, Type[] interfaces) { CheckNotGenericTypeDefinition(targetType, nameof(targetType)); CheckNotGenericTypeDefinitions(interfaces, nameof(interfaces)); @@ -53,8 +51,6 @@ protected BaseProxyGenerator(ProxyGenerationContext context, ModuleScope scope, this.scope = scope; this.targetType = targetType; this.interfaces = TypeUtil.GetAllInterfaces(interfaces); - this.proxyGenerationOptions = proxyGenerationOptions; - this.proxyGenerationOptions.Initialize(); } protected ProxyGenerationContext Context @@ -64,7 +60,7 @@ protected ProxyGenerationContext Context protected ProxyGenerationOptions ProxyGenerationOptions { - get { return proxyGenerationOptions; } + get { return context.Options; } } protected ModuleScope Scope diff --git a/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs index e93dc020f3..a502bde7fb 100644 --- a/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs @@ -25,9 +25,8 @@ namespace Castle.DynamicProxy.Generators internal sealed class ClassProxyGenerator : BaseClassProxyGenerator { public ClassProxyGenerator(ProxyGenerationContext context, ModuleScope scope, - Type targetType, Type[] interfaces, - ProxyGenerationOptions options) - : base(context, scope, targetType, interfaces, options) + Type targetType, Type[] interfaces) + : base(context, scope, targetType, interfaces) { } diff --git a/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs index b26dd3b21e..4acd58f737 100644 --- a/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs @@ -32,9 +32,8 @@ internal sealed class ClassProxyWithTargetGenerator : BaseClassProxyGenerator private FieldReference targetField; public ClassProxyWithTargetGenerator(ProxyGenerationContext context, ModuleScope scope, - Type targetType, Type[] interfaces, - ProxyGenerationOptions options) - : base(context, scope, targetType, interfaces, options) + Type targetType, Type[] interfaces) + : base(context, scope, targetType, interfaces) { } diff --git a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs index deac0635b1..8b4fc4ae03 100644 --- a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs @@ -24,9 +24,8 @@ namespace Castle.DynamicProxy.Generators internal sealed class InterfaceProxyWithTargetGenerator : BaseInterfaceProxyGenerator { public InterfaceProxyWithTargetGenerator(ProxyGenerationContext context, ModuleScope scope, - Type targetType, Type[] interfaces, Type proxyTargetType, - ProxyGenerationOptions options) - : base(context, scope, targetType, interfaces, proxyTargetType, options) + Type targetType, Type[] interfaces, Type proxyTargetType) + : base(context, scope, targetType, interfaces, proxyTargetType) { } protected override bool AllowChangeTarget => false; diff --git a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetInterfaceGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetInterfaceGenerator.cs index 82a58b08bc..d7c8b27a21 100644 --- a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetInterfaceGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetInterfaceGenerator.cs @@ -27,9 +27,8 @@ namespace Castle.DynamicProxy.Generators internal sealed class InterfaceProxyWithTargetInterfaceGenerator : BaseInterfaceProxyGenerator { public InterfaceProxyWithTargetInterfaceGenerator(ProxyGenerationContext context, ModuleScope scope, - Type targetType, Type[] interfaces, Type proxyTargetType, - ProxyGenerationOptions options) - : base(context, scope, targetType, interfaces, proxyTargetType, options) + Type targetType, Type[] interfaces, Type proxyTargetType) + : base(context, scope, targetType, interfaces, proxyTargetType) { } diff --git a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithoutTargetGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithoutTargetGenerator.cs index 348d3975f3..40e598fc05 100644 --- a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithoutTargetGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithoutTargetGenerator.cs @@ -24,9 +24,8 @@ namespace Castle.DynamicProxy.Generators internal sealed class InterfaceProxyWithoutTargetGenerator : BaseInterfaceProxyGenerator { public InterfaceProxyWithoutTargetGenerator(ProxyGenerationContext context, ModuleScope scope, - Type targetType, Type[] interfaces, Type proxyTargetType, - ProxyGenerationOptions options) - : base(context, scope, targetType, interfaces, proxyTargetType, options) + Type targetType, Type[] interfaces, Type proxyTargetType) + : base(context, scope, targetType, interfaces, proxyTargetType) { } diff --git a/src/Castle.Core/DynamicProxy/ProxyGenerationContext.cs b/src/Castle.Core/DynamicProxy/ProxyGenerationContext.cs index 3357f3b76a..959a319237 100644 --- a/src/Castle.Core/DynamicProxy/ProxyGenerationContext.cs +++ b/src/Castle.Core/DynamicProxy/ProxyGenerationContext.cs @@ -18,11 +18,17 @@ namespace Castle.DynamicProxy internal sealed class ProxyGenerationContext { - public ProxyGenerationContext(ILogger logger = null) + public ProxyGenerationContext(ProxyGenerationOptions options, + ILogger logger = null) { Logger = logger ?? NullLogger.Instance; + + Options = options ?? ProxyGenerationOptions.Default; + Options.Initialize(); } + public ProxyGenerationOptions Options { get; } + public ILogger Logger { get; } } } diff --git a/src/Castle.Core/DynamicProxy/Serialization/ProxyObjectReference.cs b/src/Castle.Core/DynamicProxy/Serialization/ProxyObjectReference.cs index 60cc7fa009..8852051e60 100644 --- a/src/Castle.Core/DynamicProxy/Serialization/ProxyObjectReference.cs +++ b/src/Castle.Core/DynamicProxy/Serialization/ProxyObjectReference.cs @@ -131,8 +131,8 @@ protected virtual object RecreateProxy() private object RecreateClassProxyWithTarget() { - var context = new ProxyGenerationContext(); - var generator = new ClassProxyWithTargetGenerator(context, scope, baseType, interfaces, proxyGenerationOptions); + var context = new ProxyGenerationContext(proxyGenerationOptions); + var generator = new ClassProxyWithTargetGenerator(context, scope, baseType, interfaces); var proxyType = generator.GetProxyType(); return InstantiateClassProxy(proxyType); } @@ -142,20 +142,20 @@ public object RecreateInterfaceProxy(string generatorType) var @interface = DeserializeTypeFromString("__theInterface"); var targetType = DeserializeTypeFromString("__targetFieldType"); - var context = new ProxyGenerationContext(); + var context = new ProxyGenerationContext(proxyGenerationOptions); BaseInterfaceProxyGenerator generator; if (generatorType == ProxyTypeConstants.InterfaceWithTarget) { - generator = new InterfaceProxyWithTargetGenerator(context, scope, @interface, interfaces, targetType, proxyGenerationOptions); + generator = new InterfaceProxyWithTargetGenerator(context, scope, @interface, interfaces, targetType); } else if (generatorType == ProxyTypeConstants.InterfaceWithoutTarget) { - generator = new InterfaceProxyWithoutTargetGenerator(context, scope, @interface, interfaces, targetType, proxyGenerationOptions); + generator = new InterfaceProxyWithoutTargetGenerator(context, scope, @interface, interfaces, targetType); } else if (generatorType == ProxyTypeConstants.InterfaceWithTargetInterface) { - generator = new InterfaceProxyWithTargetInterfaceGenerator(context, scope, @interface, interfaces, targetType, proxyGenerationOptions); + generator = new InterfaceProxyWithTargetInterfaceGenerator(context, scope, @interface, interfaces, targetType); } else { @@ -171,8 +171,8 @@ public object RecreateInterfaceProxy(string generatorType) public object RecreateClassProxy() { - var context = new ProxyGenerationContext(); - var generator = new ClassProxyGenerator(context, scope, baseType, interfaces, proxyGenerationOptions); + var context = new ProxyGenerationContext(proxyGenerationOptions); + var generator = new ClassProxyGenerator(context, scope, baseType, interfaces); var proxyType = generator.GetProxyType(); return InstantiateClassProxy(proxyType); } From a5bd3d30829be058a843113026005e70e51d2cbf Mon Sep 17 00:00:00 2001 From: Dominique Schuppli Date: Fri, 12 Feb 2021 01:08:59 +0100 Subject: [PATCH 05/13] Cache `Hook` in context --- .../Contributors/ClassMembersCollector.cs | 4 ++-- .../ClassProxySerializableContributor.cs | 2 +- .../Contributors/CompositeTypeContributor.cs | 5 ++--- .../Contributors/DelegateTypeMembersCollector.cs | 2 +- .../Contributors/ITypeContributor.cs | 2 +- .../Contributors/InterfaceMembersCollector.cs | 4 ++-- .../InterfaceMembersOnClassCollector.cs | 4 ++-- .../Contributors/MembersCollector.cs | 16 ++++++++-------- .../NonInheritableAttributesContributor.cs | 2 +- .../ProxyTargetAccessorContributor.cs | 2 +- .../Contributors/SerializableContributor.cs | 2 +- .../Contributors/WrappedClassMembersCollector.cs | 16 ++++++++-------- .../Generators/BaseClassProxyGenerator.cs | 4 ++-- .../Generators/BaseInterfaceProxyGenerator.cs | 4 ++-- .../Generators/BaseProxyGenerator.cs | 4 ++-- .../DynamicProxy/ProxyGenerationContext.cs | 4 ++++ 16 files changed, 40 insertions(+), 37 deletions(-) diff --git a/src/Castle.Core/DynamicProxy/Contributors/ClassMembersCollector.cs b/src/Castle.Core/DynamicProxy/Contributors/ClassMembersCollector.cs index 47ef965b77..6047bab356 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/ClassMembersCollector.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/ClassMembersCollector.cs @@ -26,14 +26,14 @@ public ClassMembersCollector(ProxyGenerationContext context, Type targetType) { } - protected override MetaMethod GetMethodToGenerate(MethodInfo method, IProxyGenerationHook hook, bool isStandalone) + protected override MetaMethod GetMethodToGenerate(MethodInfo method, bool isStandalone) { if (ProxyUtil.IsAccessibleMethod(method) == false) { return null; } - var accepted = AcceptMethod(method, true, hook); + var accepted = AcceptMethod(method, true); if (!accepted && !method.IsAbstract) { //we don't need to do anything... diff --git a/src/Castle.Core/DynamicProxy/Contributors/ClassProxySerializableContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/ClassProxySerializableContributor.cs index c5c58f3931..e37a304582 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/ClassProxySerializableContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/ClassProxySerializableContributor.cs @@ -41,7 +41,7 @@ public ClassProxySerializableContributor(Type targetType, Type[] interfaces, str Debug.Assert(targetType.IsSerializable, "This contributor is intended for serializable types only."); } - public override void CollectElementsToProxy(IProxyGenerationHook hook, MetaType model) + public override void CollectElementsToProxy(MetaType model) { delegateToBaseGetObjectData = VerifyIfBaseImplementsGetObjectData(targetType, model, out var getObjectData); diff --git a/src/Castle.Core/DynamicProxy/Contributors/CompositeTypeContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/CompositeTypeContributor.cs index 73a841bd3f..ba81954a65 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/CompositeTypeContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/CompositeTypeContributor.cs @@ -46,16 +46,15 @@ protected ProxyGenerationContext Context get { return context; } } - public void CollectElementsToProxy(IProxyGenerationHook hook, MetaType model) + public void CollectElementsToProxy(MetaType model) { - Debug.Assert(hook != null); Debug.Assert(model != null); var sink = new MembersCollectorSink(model, this); foreach (var collector in GetCollectors()) { - collector.CollectMembersToProxy(hook, sink); + collector.CollectMembersToProxy(sink); } } diff --git a/src/Castle.Core/DynamicProxy/Contributors/DelegateTypeMembersCollector.cs b/src/Castle.Core/DynamicProxy/Contributors/DelegateTypeMembersCollector.cs index 44ea7b1426..eaaa25c279 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/DelegateTypeMembersCollector.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/DelegateTypeMembersCollector.cs @@ -27,7 +27,7 @@ public DelegateTypeMembersCollector(ProxyGenerationContext context, Type delegat { } - protected override MetaMethod GetMethodToGenerate(MethodInfo method, IProxyGenerationHook hook, bool isStandalone) + protected override MetaMethod GetMethodToGenerate(MethodInfo method, bool isStandalone) { if (method.Name == "Invoke" && method.DeclaringType.IsDelegateType()) { diff --git a/src/Castle.Core/DynamicProxy/Contributors/ITypeContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/ITypeContributor.cs index 64ab060dda..cf49a1ca86 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/ITypeContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/ITypeContributor.cs @@ -22,7 +22,7 @@ namespace Castle.DynamicProxy.Contributors /// internal interface ITypeContributor { - void CollectElementsToProxy(IProxyGenerationHook hook, MetaType model); + void CollectElementsToProxy(MetaType model); void Generate(ClassEmitter @class); } diff --git a/src/Castle.Core/DynamicProxy/Contributors/InterfaceMembersCollector.cs b/src/Castle.Core/DynamicProxy/Contributors/InterfaceMembersCollector.cs index f1e9109bd2..e099bb866e 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/InterfaceMembersCollector.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/InterfaceMembersCollector.cs @@ -26,14 +26,14 @@ public InterfaceMembersCollector(ProxyGenerationContext context, Type @interface { } - protected override MetaMethod GetMethodToGenerate(MethodInfo method, IProxyGenerationHook hook, bool isStandalone) + protected override MetaMethod GetMethodToGenerate(MethodInfo method, bool isStandalone) { if (ProxyUtil.IsAccessibleMethod(method) == false) { return null; } - var proxyable = AcceptMethod(method, false, hook); + var proxyable = AcceptMethod(method, false); return new MetaMethod(method, method, isStandalone, proxyable, false); } } diff --git a/src/Castle.Core/DynamicProxy/Contributors/InterfaceMembersOnClassCollector.cs b/src/Castle.Core/DynamicProxy/Contributors/InterfaceMembersOnClassCollector.cs index 34f08e0dbf..8ebe0045e4 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/InterfaceMembersOnClassCollector.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/InterfaceMembersOnClassCollector.cs @@ -32,7 +32,7 @@ public InterfaceMembersOnClassCollector(ProxyGenerationContext context, Type typ this.map = map; } - protected override MetaMethod GetMethodToGenerate(MethodInfo method, IProxyGenerationHook hook, bool isStandalone) + protected override MetaMethod GetMethodToGenerate(MethodInfo method, bool isStandalone) { if (ProxyUtil.IsAccessibleMethod(method) == false) { @@ -46,7 +46,7 @@ protected override MetaMethod GetMethodToGenerate(MethodInfo method, IProxyGener var methodOnTarget = GetMethodOnTarget(method); - var proxyable = AcceptMethod(method, onlyProxyVirtual, hook); + var proxyable = AcceptMethod(method, onlyProxyVirtual); return new MetaMethod(method, methodOnTarget, isStandalone, proxyable, methodOnTarget.IsPrivate == false); } diff --git a/src/Castle.Core/DynamicProxy/Contributors/MembersCollector.cs b/src/Castle.Core/DynamicProxy/Contributors/MembersCollector.cs index de4e5108a0..6fa99aee6d 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/MembersCollector.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/MembersCollector.cs @@ -41,7 +41,7 @@ protected ProxyGenerationContext Context get { return context; } } - public virtual void CollectMembersToProxy(IProxyGenerationHook hook, IMembersCollectorSink sink) + public virtual void CollectMembersToProxy(IMembersCollectorSink sink) { var checkedMethods = new HashSet(); @@ -146,7 +146,7 @@ MetaMethod AddMethod(MethodInfo method, bool isStandalone) return null; } - var methodToGenerate = GetMethodToGenerate(method, hook, isStandalone); + var methodToGenerate = GetMethodToGenerate(method, isStandalone); if (methodToGenerate != null) { sink.Add(methodToGenerate); @@ -156,25 +156,25 @@ MetaMethod AddMethod(MethodInfo method, bool isStandalone) } } - protected abstract MetaMethod GetMethodToGenerate(MethodInfo method, IProxyGenerationHook hook, bool isStandalone); + protected abstract MetaMethod GetMethodToGenerate(MethodInfo method, bool isStandalone); /// /// Performs some basic screening and invokes the /// to select methods. /// - protected bool AcceptMethod(MethodInfo method, bool onlyVirtuals, IProxyGenerationHook hook) + protected bool AcceptMethod(MethodInfo method, bool onlyVirtuals) { - return AcceptMethodPreScreen(method, onlyVirtuals, hook) && hook.ShouldInterceptMethod(type, method); + return AcceptMethodPreScreen(method, onlyVirtuals) && Context.Hook.ShouldInterceptMethod(type, method); } /// /// Performs some basic screening to filter out non-interceptable methods. /// /// - /// The will get invoked for non-interceptable method notification only; + /// The will get invoked for non-interceptable method notification only; /// it does not get asked whether or not to intercept the . /// - protected bool AcceptMethodPreScreen(MethodInfo method, bool onlyVirtuals, IProxyGenerationHook hook) + protected bool AcceptMethodPreScreen(MethodInfo method, bool onlyVirtuals) { if (IsInternalAndNotVisibleToDynamicProxy(method)) { @@ -190,7 +190,7 @@ protected bool AcceptMethodPreScreen(MethodInfo method, bool onlyVirtuals, IProx { Context.Logger.DebugFormat("Excluded non-overridable method {0} on {1} because it cannot be intercepted.", method.Name, method.DeclaringType.FullName); - hook.NonProxyableMemberNotification(type, method); + Context.Hook.NonProxyableMemberNotification(type, method); } return false; } diff --git a/src/Castle.Core/DynamicProxy/Contributors/NonInheritableAttributesContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/NonInheritableAttributesContributor.cs index 0f30bec9af..64b12320aa 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/NonInheritableAttributesContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/NonInheritableAttributesContributor.cs @@ -40,7 +40,7 @@ public void Generate(ClassEmitter emitter) } } - public void CollectElementsToProxy(IProxyGenerationHook hook, MetaType model) + public void CollectElementsToProxy(MetaType model) { } } diff --git a/src/Castle.Core/DynamicProxy/Contributors/ProxyTargetAccessorContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/ProxyTargetAccessorContributor.cs index 32cdec1492..8d92f9014a 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/ProxyTargetAccessorContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/ProxyTargetAccessorContributor.cs @@ -34,7 +34,7 @@ public ProxyTargetAccessorContributor(Func getTargetReference, Type t this.targetType = targetType; } - public void CollectElementsToProxy(IProxyGenerationHook hook, MetaType model) + public void CollectElementsToProxy(MetaType model) { } diff --git a/src/Castle.Core/DynamicProxy/Contributors/SerializableContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/SerializableContributor.cs index fc6be52924..08927db442 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/SerializableContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/SerializableContributor.cs @@ -144,7 +144,7 @@ protected virtual void AddAddValueInvocation(ArgumentReference serializationInfo protected abstract void CustomizeGetObjectData(CodeBuilder builder, ArgumentReference serializationInfo, ArgumentReference streamingContext, ClassEmitter emitter); - public virtual void CollectElementsToProxy(IProxyGenerationHook hook, MetaType model) + public virtual void CollectElementsToProxy(MetaType model) { } } diff --git a/src/Castle.Core/DynamicProxy/Contributors/WrappedClassMembersCollector.cs b/src/Castle.Core/DynamicProxy/Contributors/WrappedClassMembersCollector.cs index 55ad52c232..c79f8970cc 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/WrappedClassMembersCollector.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/WrappedClassMembersCollector.cs @@ -29,28 +29,28 @@ public WrappedClassMembersCollector(ProxyGenerationContext context, Type type) { } - public override void CollectMembersToProxy(IProxyGenerationHook hook, IMembersCollectorSink sink) + public override void CollectMembersToProxy(IMembersCollectorSink sink) { - base.CollectMembersToProxy(hook, sink); - CollectFields(hook); + base.CollectMembersToProxy(sink); + CollectFields(); // TODO: perhaps we should also look for nested classes... } - protected override MetaMethod GetMethodToGenerate(MethodInfo method, IProxyGenerationHook hook, bool isStandalone) + protected override MetaMethod GetMethodToGenerate(MethodInfo method, bool isStandalone) { if (ProxyUtil.IsAccessibleMethod(method) == false) { return null; } - var interceptable = AcceptMethodPreScreen(method, true, hook); + var interceptable = AcceptMethodPreScreen(method, true); if (!interceptable) { //we don't need to do anything... return null; } - var accepted = hook.ShouldInterceptMethod(type, method); + var accepted = Context.Hook.ShouldInterceptMethod(type, method); return new MetaMethod(method, method, isStandalone, accepted, hasTarget: true); } @@ -66,7 +66,7 @@ protected virtual bool IsOKToBeOnProxy(FieldInfo field) return IsGeneratedByTheCompiler(field); } - private void CollectFields(IProxyGenerationHook hook) + private void CollectFields() { var fields = type.GetAllFields(); foreach (var field in fields) @@ -76,7 +76,7 @@ private void CollectFields(IProxyGenerationHook hook) continue; } - hook.NonProxyableMemberNotification(type, field); + Context.Hook.NonProxyableMemberNotification(type, field); } } } diff --git a/src/Castle.Core/DynamicProxy/Generators/BaseClassProxyGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/BaseClassProxyGenerator.cs index 7c23e1fc8c..9572762fe0 100644 --- a/src/Castle.Core/DynamicProxy/Generators/BaseClassProxyGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/BaseClassProxyGenerator.cs @@ -51,9 +51,9 @@ protected sealed override Type GenerateType(string name, INamingScope namingScop // Collect methods foreach (var contributor in contributors) { - contributor.CollectElementsToProxy(ProxyGenerationOptions.Hook, model); + contributor.CollectElementsToProxy(model); } - ProxyGenerationOptions.Hook.MethodsInspected(); + Context.Hook.MethodsInspected(); var emitter = BuildClassEmitter(name, targetType, allInterfaces); diff --git a/src/Castle.Core/DynamicProxy/Generators/BaseInterfaceProxyGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/BaseInterfaceProxyGenerator.cs index 3a3b834799..8853cae120 100644 --- a/src/Castle.Core/DynamicProxy/Generators/BaseInterfaceProxyGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/BaseInterfaceProxyGenerator.cs @@ -93,10 +93,10 @@ protected override Type GenerateType(string typeName, INamingScope namingScope) // Collect methods foreach (var contributor in contributors) { - contributor.CollectElementsToProxy(ProxyGenerationOptions.Hook, model); + contributor.CollectElementsToProxy(model); } - ProxyGenerationOptions.Hook.MethodsInspected(); + Context.Hook.MethodsInspected(); ClassEmitter emitter; FieldReference interceptorsField; diff --git a/src/Castle.Core/DynamicProxy/Generators/BaseProxyGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/BaseProxyGenerator.cs index cb8a8d6ff8..8428320781 100644 --- a/src/Castle.Core/DynamicProxy/Generators/BaseProxyGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/BaseProxyGenerator.cs @@ -201,11 +201,11 @@ protected void EnsureOptionsOverrideEqualsAndGetHashCode() if (Context.Logger.IsWarnEnabled) { // Check the proxy generation hook - if (!OverridesEqualsAndGetHashCode(ProxyGenerationOptions.Hook.GetType())) + if (!OverridesEqualsAndGetHashCode(Context.Hook.GetType())) { Context.Logger.WarnFormat("The IProxyGenerationHook type {0} does not override both Equals and GetHashCode. " + "If these are not correctly overridden caching will fail to work causing performance problems.", - ProxyGenerationOptions.Hook.GetType().FullName); + Context.Hook.GetType().FullName); } // Interceptor selectors no longer need to override Equals and GetHashCode diff --git a/src/Castle.Core/DynamicProxy/ProxyGenerationContext.cs b/src/Castle.Core/DynamicProxy/ProxyGenerationContext.cs index 959a319237..9615623592 100644 --- a/src/Castle.Core/DynamicProxy/ProxyGenerationContext.cs +++ b/src/Castle.Core/DynamicProxy/ProxyGenerationContext.cs @@ -25,8 +25,12 @@ public ProxyGenerationContext(ProxyGenerationOptions options, Options = options ?? ProxyGenerationOptions.Default; Options.Initialize(); + + Hook = Options.Hook ?? new AllMethodsHook(); } + public IProxyGenerationHook Hook { get; } + public ProxyGenerationOptions Options { get; } public ILogger Logger { get; } From 5a15b94279c3432f3216f2d92f1b317a2e2e2ac1 Mon Sep 17 00:00:00 2001 From: Dominique Schuppli Date: Fri, 12 Feb 2021 01:26:00 +0100 Subject: [PATCH 06/13] Hard-code default `AttributesToAvoidReplicating` --- .../AttributesToAvoidReplicatingTestCase.cs | 8 -------- .../Generators/AttributesToAvoidReplicating.cs | 8 +------- .../DynamicProxy/Internal/AttributeUtil.cs | 14 ++++++++++++++ 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/AttributesToAvoidReplicatingTestCase.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/AttributesToAvoidReplicatingTestCase.cs index e2a084ef06..66c1e1b0bf 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/AttributesToAvoidReplicatingTestCase.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/AttributesToAvoidReplicatingTestCase.cs @@ -35,14 +35,6 @@ public void After_adding_attribute_must_be_listed_as_contained() Assert.IsTrue(contains); } - [Test] - public void After_adding_attribute_must_still_contain_original_attributes() - { - AttributesToAvoidReplicating.Add(); - bool contains = AttributesToAvoidReplicating.Contains(typeof(System.Runtime.InteropServices.ComImportAttribute)); - Assert.IsTrue(contains); - } - [Test] public void NonInheritableAttribute_should_be_replicated_as_it_is_not_inherited() { diff --git a/src/Castle.Core/DynamicProxy/Generators/AttributesToAvoidReplicating.cs b/src/Castle.Core/DynamicProxy/Generators/AttributesToAvoidReplicating.cs index e045a50bdf..e7be33a3e8 100644 --- a/src/Castle.Core/DynamicProxy/Generators/AttributesToAvoidReplicating.cs +++ b/src/Castle.Core/DynamicProxy/Generators/AttributesToAvoidReplicating.cs @@ -27,13 +27,7 @@ public static class AttributesToAvoidReplicating static AttributesToAvoidReplicating() { - attributes = new List() - { - typeof(System.Runtime.InteropServices.ComImportAttribute), - typeof(System.Runtime.InteropServices.MarshalAsAttribute), - typeof(System.Runtime.InteropServices.TypeIdentifierAttribute), - typeof(System.Security.Permissions.SecurityAttribute), - }; + attributes = new List(); } public static void Add(Type attribute) diff --git a/src/Castle.Core/DynamicProxy/Internal/AttributeUtil.cs b/src/Castle.Core/DynamicProxy/Internal/AttributeUtil.cs index e773080c6b..d8c5f6bd3f 100644 --- a/src/Castle.Core/DynamicProxy/Internal/AttributeUtil.cs +++ b/src/Castle.Core/DynamicProxy/Internal/AttributeUtil.cs @@ -19,6 +19,8 @@ namespace Castle.DynamicProxy.Internal using System.Diagnostics; using System.Linq; using System.Reflection; + using System.Runtime.InteropServices; + using System.Security.Permissions; using Castle.DynamicProxy.Generators; @@ -190,6 +192,18 @@ private static bool ShouldSkipAttributeReplication(Type attribute, bool ignoreIn return true; } + if (attribute == typeof(ComImportAttribute) || + attribute == typeof(MarshalAsAttribute) || + attribute == typeof(TypeIdentifierAttribute)) + { + return true; + } + + if (attribute.IsSubclassOf(typeof(SecurityAttribute))) + { + return true; + } + if (AttributesToAvoidReplicating.ShouldAvoid(attribute)) { return true; From 60e652f9c9e787f30b00b58ca89c2cd564f98704 Mon Sep 17 00:00:00 2001 From: Dominique Schuppli Date: Fri, 12 Feb 2021 01:41:13 +0100 Subject: [PATCH 07/13] Make `AttributesToAvoidReplicating` more list-like --- .../AttributesToAvoidReplicatingTestCase.cs | 2 +- .../Generators/AttributesToAvoidReplicating.cs | 10 ++-------- src/Castle.Core/DynamicProxy/Internal/AttributeUtil.cs | 2 +- 3 files changed, 4 insertions(+), 10 deletions(-) diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/AttributesToAvoidReplicatingTestCase.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/AttributesToAvoidReplicatingTestCase.cs index 66c1e1b0bf..2d908d6abf 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/AttributesToAvoidReplicatingTestCase.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/AttributesToAvoidReplicatingTestCase.cs @@ -31,7 +31,7 @@ public class AttributesToAvoidReplicatingTestCase : BasePEVerifyTestCase public void After_adding_attribute_must_be_listed_as_contained() { AttributesToAvoidReplicating.Add(); - bool contains = AttributesToAvoidReplicating.Contains(typeof(string)); + bool contains = AttributesToAvoidReplicating.AsList().Contains(typeof(string)); Assert.IsTrue(contains); } diff --git a/src/Castle.Core/DynamicProxy/Generators/AttributesToAvoidReplicating.cs b/src/Castle.Core/DynamicProxy/Generators/AttributesToAvoidReplicating.cs index e7be33a3e8..f93437862b 100644 --- a/src/Castle.Core/DynamicProxy/Generators/AttributesToAvoidReplicating.cs +++ b/src/Castle.Core/DynamicProxy/Generators/AttributesToAvoidReplicating.cs @@ -16,7 +16,6 @@ namespace Castle.DynamicProxy.Generators { using System; using System.Collections.Generic; - using System.Linq; using System.Reflection; public static class AttributesToAvoidReplicating @@ -44,14 +43,9 @@ public static void Add() Add(typeof(T)); } - public static bool Contains(Type attribute) + internal static IList AsList() { - return attributes.Contains(attribute); - } - - internal static bool ShouldAvoid(Type attribute) - { - return attributes.Any(attr => attr.IsAssignableFrom(attribute)); + return attributes; } } } \ No newline at end of file diff --git a/src/Castle.Core/DynamicProxy/Internal/AttributeUtil.cs b/src/Castle.Core/DynamicProxy/Internal/AttributeUtil.cs index d8c5f6bd3f..b21ce85b8b 100644 --- a/src/Castle.Core/DynamicProxy/Internal/AttributeUtil.cs +++ b/src/Castle.Core/DynamicProxy/Internal/AttributeUtil.cs @@ -204,7 +204,7 @@ private static bool ShouldSkipAttributeReplication(Type attribute, bool ignoreIn return true; } - if (AttributesToAvoidReplicating.ShouldAvoid(attribute)) + if (AttributesToAvoidReplicating.AsList().Any(a => a.IsAssignableFrom(attribute))) { return true; } From c0ece6eda6f06b610ea46baa0639ee3ed9c87837 Mon Sep 17 00:00:00 2001 From: Dominique Schuppli Date: Fri, 12 Feb 2021 02:04:18 +0100 Subject: [PATCH 08/13] Add `AttributesToAvoidReplicating` to context --- src/Castle.Core/DynamicProxy/ProxyGenerationContext.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Castle.Core/DynamicProxy/ProxyGenerationContext.cs b/src/Castle.Core/DynamicProxy/ProxyGenerationContext.cs index 9615623592..4d525e91a6 100644 --- a/src/Castle.Core/DynamicProxy/ProxyGenerationContext.cs +++ b/src/Castle.Core/DynamicProxy/ProxyGenerationContext.cs @@ -14,6 +14,9 @@ namespace Castle.DynamicProxy { + using System; + using System.Collections.Generic; + using Castle.Core.Logging; internal sealed class ProxyGenerationContext @@ -26,9 +29,13 @@ public ProxyGenerationContext(ProxyGenerationOptions options, Options = options ?? ProxyGenerationOptions.Default; Options.Initialize(); + AttributesToAvoidReplicating = Generators.AttributesToAvoidReplicating.AsList(); + Hook = Options.Hook ?? new AllMethodsHook(); } + public IList AttributesToAvoidReplicating { get; } + public IProxyGenerationHook Hook { get; } public ProxyGenerationOptions Options { get; } From d04dc907c07a44e8fd6b490ef15f7590f18078ee Mon Sep 17 00:00:00 2001 From: Dominique Schuppli Date: Fri, 12 Feb 2021 02:04:46 +0100 Subject: [PATCH 09/13] Access `AttributesToAvoidReplicating` via context --- .../ClassEmitterTestCase.cs | 41 ++++++++++++------- .../ClassProxyTargetContributor.cs | 12 ++---- .../ClassProxyWithTargetTargetContributor.cs | 11 +++-- .../Contributors/CompositeTypeContributor.cs | 2 +- .../InterfaceProxyTargetContributor.cs | 3 +- .../InterfaceProxyWithoutTargetContributor.cs | 3 +- .../Contributors/MembersCollector.cs | 2 +- .../Contributors/MixinContributor.cs | 3 +- .../NonInheritableAttributesContributor.cs | 6 ++- .../Generators/BaseClassProxyGenerator.cs | 2 +- .../Generators/BaseInterfaceProxyGenerator.cs | 2 +- .../Generators/BaseProxyGenerator.cs | 4 +- .../CompositionInvocationTypeGenerator.cs | 4 +- .../Generators/DelegateTypeGenerator.cs | 7 +++- .../Emitters/AbstractTypeEmitter.cs | 19 ++++++--- .../Generators/Emitters/ClassEmitter.cs | 12 +++--- .../Generators/Emitters/EventEmitter.cs | 8 ++-- .../Generators/Emitters/GenericUtil.cs | 16 +++++--- .../Generators/Emitters/MethodEmitter.cs | 20 +++++---- .../Generators/Emitters/NestedClassEmitter.cs | 11 ++--- .../Generators/Emitters/PropertyEmitter.cs | 8 ++-- .../InheritanceInvocationTypeGenerator.cs | 4 +- .../Generators/InvocationTypeGenerator.cs | 11 ++++- .../DynamicProxy/Internal/AttributeUtil.cs | 12 +++--- 24 files changed, 132 insertions(+), 91 deletions(-) diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/ClassEmitterTestCase.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/ClassEmitterTestCase.cs index 62d46e1697..d8a9ebe54e 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/ClassEmitterTestCase.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/ClassEmitterTestCase.cs @@ -27,7 +27,8 @@ public class ClassEmitterTestCase : BasePEVerifyTestCase [Test] public void AutomaticDefaultConstructorGeneration() { - ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "Foo", typeof (object), Type.EmptyTypes); + var context = new ProxyGenerationContext(ProxyGenerationOptions.Default); + ClassEmitter emitter = new ClassEmitter(context, generator.ProxyBuilder.ModuleScope, "Foo", typeof (object), Type.EmptyTypes); Type t = emitter.BuildType(); Activator.CreateInstance(t); } @@ -35,7 +36,8 @@ public void AutomaticDefaultConstructorGeneration() [Test] public void AutomaticDefaultConstructorGenerationWithClosedGenericType() { - ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "Foo", typeof (List), + var context = new ProxyGenerationContext(ProxyGenerationOptions.Default); + ClassEmitter emitter = new ClassEmitter(context, generator.ProxyBuilder.ModuleScope, "Foo", typeof (List), Type.EmptyTypes); Type t = emitter.BuildType(); Activator.CreateInstance(t); @@ -44,7 +46,8 @@ public void AutomaticDefaultConstructorGenerationWithClosedGenericType() [Test] public void StaticMethodArguments() { - ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "Foo", typeof (List), + var context = new ProxyGenerationContext(ProxyGenerationOptions.Default); + ClassEmitter emitter = new ClassEmitter(context, generator.ProxyBuilder.ModuleScope, "Foo", typeof (List), Type.EmptyTypes); MethodEmitter methodEmitter = emitter.CreateMethod("StaticMethod", MethodAttributes.Public | MethodAttributes.Static, typeof (string), typeof (string)); @@ -56,7 +59,8 @@ public void StaticMethodArguments() [Test] public void InstanceMethodArguments() { - ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "Foo", typeof (List), + var context = new ProxyGenerationContext(ProxyGenerationOptions.Default); + ClassEmitter emitter = new ClassEmitter(context, generator.ProxyBuilder.ModuleScope, "Foo", typeof (List), Type.EmptyTypes); MethodEmitter methodEmitter = emitter.CreateMethod("InstanceMethod", MethodAttributes.Public, typeof (string), typeof (string)); @@ -70,7 +74,8 @@ public void InstanceMethodArguments() public void ForceUnsignedFalseWithSignedTypes() { const bool shouldBeSigned = true; - ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "Foo", typeof (object), Type.EmptyTypes, + var context = new ProxyGenerationContext(ProxyGenerationOptions.Default); + ClassEmitter emitter = new ClassEmitter(context, generator.ProxyBuilder.ModuleScope, "Foo", typeof (object), Type.EmptyTypes, TypeAttributes.Public, false); Type t = emitter.BuildType(); Assert.AreEqual(shouldBeSigned, StrongNameUtil.IsAssemblySigned(t.Assembly)); @@ -79,7 +84,8 @@ public void ForceUnsignedFalseWithSignedTypes() [Test] public void ForceUnsignedTrueWithSignedTypes() { - ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "Foo", typeof (object), Type.EmptyTypes, + var context = new ProxyGenerationContext(ProxyGenerationOptions.Default); + ClassEmitter emitter = new ClassEmitter(context, generator.ProxyBuilder.ModuleScope, "Foo", typeof (object), Type.EmptyTypes, TypeAttributes.Public, true); Type t = emitter.BuildType(); Assert.IsFalse(StrongNameUtil.IsAssemblySigned(t.Assembly)); @@ -88,7 +94,8 @@ public void ForceUnsignedTrueWithSignedTypes() [Test] public void CreateFieldWithAttributes() { - ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "Foo", typeof (object), Type.EmptyTypes); + var context = new ProxyGenerationContext(ProxyGenerationOptions.Default); + ClassEmitter emitter = new ClassEmitter(context, generator.ProxyBuilder.ModuleScope, "Foo", typeof (object), Type.EmptyTypes); emitter.CreateField("myField", typeof (string), FieldAttributes.FamANDAssem | FieldAttributes.InitOnly); Type t = emitter.BuildType(); FieldInfo field = t.GetField("myField", BindingFlags.NonPublic | BindingFlags.Instance); @@ -99,7 +106,8 @@ public void CreateFieldWithAttributes() [Test] public void CreateStaticFieldWithAttributes() { - ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "Foo", typeof (object), Type.EmptyTypes); + var context = new ProxyGenerationContext(ProxyGenerationOptions.Default); + ClassEmitter emitter = new ClassEmitter(context, generator.ProxyBuilder.ModuleScope, "Foo", typeof (object), Type.EmptyTypes); emitter.CreateStaticField("myField", typeof (string), FieldAttributes.FamANDAssem | FieldAttributes.InitOnly); Type t = emitter.BuildType(); FieldInfo field = t.GetField("myField", BindingFlags.NonPublic | BindingFlags.Static); @@ -110,7 +118,8 @@ public void CreateStaticFieldWithAttributes() [Test] public void UsingClassEmitterForInterfaces() { - ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "IFoo", null, Type.EmptyTypes, + var context = new ProxyGenerationContext(ProxyGenerationOptions.Default); + ClassEmitter emitter = new ClassEmitter(context, generator.ProxyBuilder.ModuleScope, "IFoo", null, Type.EmptyTypes, TypeAttributes.Interface | TypeAttributes.Abstract | TypeAttributes.Public, false); emitter.CreateMethod("MyMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual, typeof(void), Type.EmptyTypes); @@ -124,7 +133,8 @@ public void UsingClassEmitterForInterfaces() public void NoBaseTypeForInterfaces() { DisableVerification(); - ClassEmitter emitter = new ClassEmitter (generator.ProxyBuilder.ModuleScope, "IFoo", null, Type.EmptyTypes, + var context = new ProxyGenerationContext(ProxyGenerationOptions.Default); + ClassEmitter emitter = new ClassEmitter(context, generator.ProxyBuilder.ModuleScope, "IFoo", null, Type.EmptyTypes, TypeAttributes.Interface | TypeAttributes.Abstract | TypeAttributes.Public, false); Assert.Throws(delegate { @@ -138,7 +148,8 @@ public void NoBaseTypeForInterfaces() public void NoDefaultCtorForInterfaces() { DisableVerification(); - ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "IFoo", null, Type.EmptyTypes, + var context = new ProxyGenerationContext(ProxyGenerationOptions.Default); + ClassEmitter emitter = new ClassEmitter(context, generator.ProxyBuilder.ModuleScope, "IFoo", null, Type.EmptyTypes, TypeAttributes.Interface | TypeAttributes.Abstract | TypeAttributes.Public, false); Assert.Throws(delegate { @@ -150,7 +161,8 @@ public void NoDefaultCtorForInterfaces() public void NoCustomCtorForInterfaces() { DisableVerification(); - ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "IFoo", null, Type.EmptyTypes, + var context = new ProxyGenerationContext(ProxyGenerationOptions.Default); + ClassEmitter emitter = new ClassEmitter(context, generator.ProxyBuilder.ModuleScope, "IFoo", null, Type.EmptyTypes, TypeAttributes.Interface | TypeAttributes.Abstract | TypeAttributes.Public, false); Assert.Throws(delegate { @@ -161,9 +173,10 @@ public void NoCustomCtorForInterfaces() [Test] public void NestedInterface() { - ClassEmitter outerEmitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "IOuter", null, Type.EmptyTypes, + var context = new ProxyGenerationContext(ProxyGenerationOptions.Default); + ClassEmitter outerEmitter = new ClassEmitter(context, generator.ProxyBuilder.ModuleScope, "IOuter", null, Type.EmptyTypes, TypeAttributes.Interface | TypeAttributes.Abstract | TypeAttributes.Public, false); - NestedClassEmitter innerEmitter = new NestedClassEmitter(outerEmitter, "IInner", + NestedClassEmitter innerEmitter = new NestedClassEmitter(context, outerEmitter, "IInner", TypeAttributes.Interface | TypeAttributes.Abstract | TypeAttributes.NestedPublic, null, Type.EmptyTypes); innerEmitter.CreateMethod("MyMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual, typeof(void), Type.EmptyTypes); diff --git a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyTargetContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyTargetContributor.cs index 5db8019bfb..f9ffd3fd3b 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyTargetContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyTargetContributor.cs @@ -87,16 +87,12 @@ private Type BuildInvocationType(MetaMethod method, ClassEmitter @class) var methodInfo = method.Method; if (!method.HasTarget) { - return new InheritanceInvocationTypeGenerator(targetType, - method, - null, null) + return new InheritanceInvocationTypeGenerator(Context, targetType, method, null, null) .Generate(@class, namingScope) .BuildType(); } var callback = CreateCallbackMethod(@class, methodInfo, method.MethodOnTarget); - return new InheritanceInvocationTypeGenerator(callback.DeclaringType, - method, - callback, null) + return new InheritanceInvocationTypeGenerator(Context, callback.DeclaringType, method, callback, null) .Generate(@class, namingScope) .BuildType(); } @@ -132,7 +128,7 @@ private MethodGenerator ExplicitlyImplementedInterfaceMethodGenerator(MetaMethod { var @delegate = GetDelegateType(method, @class); var contributor = GetContributor(@delegate, method); - var invocation = new InheritanceInvocationTypeGenerator(targetType, method, null, contributor) + var invocation = new InheritanceInvocationTypeGenerator(Context, targetType, method, null, contributor) .Generate(@class, namingScope) .BuildType(); return new MethodWithInvocationGenerator(method, @@ -166,7 +162,7 @@ private Type GetDelegateType(MetaMethod method, ClassEmitter @class) null); return scope.TypeCache.GetOrAddWithoutTakingLock(key, _ => - new DelegateTypeGenerator(method, targetType) + new DelegateTypeGenerator(Context, method, targetType) .Generate(@class, namingScope) .BuildType()); } diff --git a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyWithTargetTargetContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyWithTargetTargetContributor.cs index e653ce3bd3..ff9973f771 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyWithTargetTargetContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyWithTargetTargetContributor.cs @@ -89,13 +89,12 @@ private Type BuildInvocationType(MetaMethod method, ClassEmitter @class) { if (!method.HasTarget) { - return new InheritanceInvocationTypeGenerator(targetType, - method, - null, null) + return new InheritanceInvocationTypeGenerator(Context, targetType, method, null, null) .Generate(@class, namingScope) .BuildType(); } - return new CompositionInvocationTypeGenerator(method.Method.DeclaringType, + return new CompositionInvocationTypeGenerator(Context, + method.Method.DeclaringType, method, method.Method, false, @@ -127,7 +126,7 @@ private Type GetDelegateType(MetaMethod method, ClassEmitter @class) null); return scope.TypeCache.GetOrAddWithoutTakingLock(key, _ => - new DelegateTypeGenerator(method, targetType) + new DelegateTypeGenerator(Context, method, targetType) .Generate(@class, namingScope) .BuildType()); } @@ -150,7 +149,7 @@ private MethodGenerator IndirectlyCalledMethodGenerator(MetaMethod method, Class { var @delegate = GetDelegateType(method, proxy); var contributor = GetContributor(@delegate, method); - var invocation = new CompositionInvocationTypeGenerator(targetType, method, null, false, contributor) + var invocation = new CompositionInvocationTypeGenerator(Context, targetType, method, null, false, contributor) .Generate(proxy, namingScope) .BuildType(); return new MethodWithInvocationGenerator(method, diff --git a/src/Castle.Core/DynamicProxy/Contributors/CompositeTypeContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/CompositeTypeContributor.cs index ba81954a65..3ee37e924a 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/CompositeTypeContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/CompositeTypeContributor.cs @@ -129,7 +129,7 @@ private void ImplementMethod(MetaMethod method, ClassEmitter @class, return; } var proxyMethod = generator.Generate(@class, namingScope); - foreach (var attribute in method.Method.GetNonInheritableAttributes()) + foreach (var attribute in method.Method.GetNonInheritableAttributes(Context)) { proxyMethod.DefineCustomAttribute(attribute.Builder); } diff --git a/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyTargetContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyTargetContributor.cs index 6e0a24446e..bb03ff0d2b 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyTargetContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyTargetContributor.cs @@ -88,7 +88,8 @@ private Type GetInvocationType(MetaMethod method, ClassEmitter @class) // no locking required as we're already within a lock return scope.TypeCache.GetOrAddWithoutTakingLock(key, _ => - new CompositionInvocationTypeGenerator(method.Method.DeclaringType, + new CompositionInvocationTypeGenerator(Context, + method.Method.DeclaringType, method, method.Method, canChangeTarget, diff --git a/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyWithoutTargetContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyWithoutTargetContributor.cs index 95c99ef347..4f0bf3be9d 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyWithoutTargetContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyWithoutTargetContributor.cs @@ -86,7 +86,8 @@ private Type GetInvocationType(MetaMethod method, ClassEmitter emitter) // no locking required as we're already within a lock return scope.TypeCache.GetOrAddWithoutTakingLock(key, _ => - new CompositionInvocationTypeGenerator(methodInfo.DeclaringType, + new CompositionInvocationTypeGenerator(Context, + methodInfo.DeclaringType, method, methodInfo, canChangeTarget, diff --git a/src/Castle.Core/DynamicProxy/Contributors/MembersCollector.cs b/src/Castle.Core/DynamicProxy/Contributors/MembersCollector.cs index 6fa99aee6d..82bbd57eec 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/MembersCollector.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/MembersCollector.cs @@ -101,7 +101,7 @@ void AddProperty(PropertyInfo property) return; } - var nonInheritableAttributes = property.GetNonInheritableAttributes(); + var nonInheritableAttributes = property.GetNonInheritableAttributes(Context); var arguments = property.GetIndexParameters(); sink.Add(new MetaProperty(property.Name, diff --git a/src/Castle.Core/DynamicProxy/Contributors/MixinContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/MixinContributor.cs index a07e906fcd..649faa1290 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/MixinContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/MixinContributor.cs @@ -142,7 +142,8 @@ private Type GetInvocationType(MetaMethod method, ClassEmitter emitter) // no locking required as we're already within a lock return scope.TypeCache.GetOrAddWithoutTakingLock(key, _ => - new CompositionInvocationTypeGenerator(method.Method.DeclaringType, + new CompositionInvocationTypeGenerator(Context, + method.Method.DeclaringType, method, method.Method, canChangeTarget, diff --git a/src/Castle.Core/DynamicProxy/Contributors/NonInheritableAttributesContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/NonInheritableAttributesContributor.cs index 64b12320aa..ae6e92ddb6 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/NonInheritableAttributesContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/NonInheritableAttributesContributor.cs @@ -25,16 +25,18 @@ namespace Castle.DynamicProxy.Contributors /// internal sealed class NonInheritableAttributesContributor : ITypeContributor { + private readonly ProxyGenerationContext context; private readonly Type targetType; - public NonInheritableAttributesContributor(Type targetType) + public NonInheritableAttributesContributor(ProxyGenerationContext context, Type targetType) { + this.context = context; this.targetType = targetType; } public void Generate(ClassEmitter emitter) { - foreach (var attribute in targetType.GetNonInheritableAttributes()) + foreach (var attribute in targetType.GetNonInheritableAttributes(context)) { emitter.DefineCustomAttribute(attribute.Builder); } diff --git a/src/Castle.Core/DynamicProxy/Generators/BaseClassProxyGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/BaseClassProxyGenerator.cs index 9572762fe0..b2b6db572d 100644 --- a/src/Castle.Core/DynamicProxy/Generators/BaseClassProxyGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/BaseClassProxyGenerator.cs @@ -97,7 +97,7 @@ protected sealed override Type GenerateType(string name, INamingScope namingScop CompleteInitCacheMethod(cctor.CodeBuilder); // non-inheritable attributes from proxied type - var nonInheritableAttributesContributor = new NonInheritableAttributesContributor(targetType); + var nonInheritableAttributesContributor = new NonInheritableAttributesContributor(Context, targetType); nonInheritableAttributesContributor.Generate(emitter); // Crosses fingers and build type diff --git a/src/Castle.Core/DynamicProxy/Generators/BaseInterfaceProxyGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/BaseInterfaceProxyGenerator.cs index 8853cae120..f437e01fbc 100644 --- a/src/Castle.Core/DynamicProxy/Generators/BaseInterfaceProxyGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/BaseInterfaceProxyGenerator.cs @@ -132,7 +132,7 @@ protected override Type GenerateType(string typeName, INamingScope namingScope) CompleteInitCacheMethod(cctor.CodeBuilder); // non-inheritable attributes from proxied type - var nonInheritableAttributesContributor = new NonInheritableAttributesContributor(targetType); + var nonInheritableAttributesContributor = new NonInheritableAttributesContributor(Context, targetType); nonInheritableAttributesContributor.Generate(emitter); // Crosses fingers and build type diff --git a/src/Castle.Core/DynamicProxy/Generators/BaseProxyGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/BaseProxyGenerator.cs index 8428320781..d82aee5d35 100644 --- a/src/Castle.Core/DynamicProxy/Generators/BaseProxyGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/BaseProxyGenerator.cs @@ -129,7 +129,7 @@ protected virtual ClassEmitter BuildClassEmitter(string typeName, Type parentTyp CheckNotGenericTypeDefinition(parentType, nameof(parentType)); CheckNotGenericTypeDefinitions(interfaces, nameof(interfaces)); - return new ClassEmitter(Scope, typeName, parentType, interfaces); + return new ClassEmitter(Context, Scope, typeName, parentType, interfaces); } protected void CheckNotGenericTypeDefinition(Type type, string argumentName) @@ -251,7 +251,7 @@ protected void GenerateConstructor(ClassEmitter emitter, ConstructorInfo baseCon for (int i = 0, n = baseConstructorParams.Length; i < n; ++i) { var parameterBuilder = constructor.ConstructorBuilder.DefineParameter(offset + i, baseConstructorParams[i].Attributes, baseConstructorParams[i].Name); - foreach (var attribute in baseConstructorParams[i].GetNonInheritableAttributes()) + foreach (var attribute in baseConstructorParams[i].GetNonInheritableAttributes(Context)) { parameterBuilder.SetCustomAttribute(attribute.Builder); } diff --git a/src/Castle.Core/DynamicProxy/Generators/CompositionInvocationTypeGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/CompositionInvocationTypeGenerator.cs index 7222725f31..5ff64fbc0e 100644 --- a/src/Castle.Core/DynamicProxy/Generators/CompositionInvocationTypeGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/CompositionInvocationTypeGenerator.cs @@ -27,9 +27,9 @@ internal class CompositionInvocationTypeGenerator : InvocationTypeGenerator { public static readonly Type BaseType = typeof(CompositionInvocation); - public CompositionInvocationTypeGenerator(Type target, MetaMethod method, MethodInfo callback, bool canChangeTarget, + public CompositionInvocationTypeGenerator(ProxyGenerationContext context, Type target, MetaMethod method, MethodInfo callback, bool canChangeTarget, IInvocationCreationContributor contributor) - : base(target, method, callback, canChangeTarget, contributor) + : base(context, target, method, callback, canChangeTarget, contributor) { } diff --git a/src/Castle.Core/DynamicProxy/Generators/DelegateTypeGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/DelegateTypeGenerator.cs index 8efd6d40cf..d93cb5efab 100644 --- a/src/Castle.Core/DynamicProxy/Generators/DelegateTypeGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/DelegateTypeGenerator.cs @@ -29,11 +29,13 @@ internal class DelegateTypeGenerator : IGenerator TypeAttributes.AnsiClass | TypeAttributes.AutoClass; + private readonly ProxyGenerationContext context; private readonly MetaMethod method; private readonly Type targetType; - public DelegateTypeGenerator(MetaMethod method, Type targetType) + public DelegateTypeGenerator(ProxyGenerationContext context, MetaMethod method, Type targetType) { + this.context = context; this.method = method; this.targetType = targetType; } @@ -74,7 +76,8 @@ private AbstractTypeEmitter GetEmitter(ClassEmitter @class, INamingScope namingS method.Method.Name); var uniqueName = namingScope.ParentScope.GetUniqueName(suggestedName); - var @delegate = new ClassEmitter(@class.ModuleScope, + var @delegate = new ClassEmitter(context, + @class.ModuleScope, uniqueName, typeof(MulticastDelegate), Type.EmptyTypes, diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/AbstractTypeEmitter.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/AbstractTypeEmitter.cs index d2f41e39c2..0bf83a36db 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/AbstractTypeEmitter.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/AbstractTypeEmitter.cs @@ -36,6 +36,7 @@ internal abstract class AbstractTypeEmitter private readonly List methods; + private readonly ProxyGenerationContext context; private readonly Dictionary name2GenericType; private readonly List nested; private readonly List properties; @@ -43,8 +44,9 @@ internal abstract class AbstractTypeEmitter private GenericTypeParameterBuilder[] genericTypeParams; - protected AbstractTypeEmitter(TypeBuilder typeBuilder) + protected AbstractTypeEmitter(ProxyGenerationContext context, TypeBuilder typeBuilder) { + this.context = context; typebuilder = typeBuilder; nested = new List(); methods = new List(); @@ -78,6 +80,11 @@ public TypeBuilder TypeBuilder get { return typebuilder; } } + protected ProxyGenerationContext Context + { + get { return context; } + } + public void AddCustomAttributes(IEnumerable additionalAttributes) { foreach (var attribute in additionalAttributes) @@ -113,7 +120,7 @@ public void CopyGenericParametersFromMethod(MethodInfo methodToCopyGenericsFrom) throw new InvalidOperationException("Cannot invoke me twice"); } - SetGenericTypeParameters(GenericUtil.CopyGenericArguments(methodToCopyGenericsFrom, typebuilder, name2GenericType)); + SetGenericTypeParameters(GenericUtil.CopyGenericArguments(methodToCopyGenericsFrom, typebuilder, Context, name2GenericType)); } public ConstructorEmitter CreateConstructor(params ArgumentReference[] arguments) @@ -140,7 +147,7 @@ public void CreateDefaultConstructor() public EventEmitter CreateEvent(string name, EventAttributes atts, Type type) { - var eventEmitter = new EventEmitter(this, name, atts, type); + var eventEmitter = new EventEmitter(Context, this, name, atts, type); events.Add(eventEmitter); return eventEmitter; } @@ -172,7 +179,7 @@ public FieldReference CreateField(string name, Type fieldType, FieldAttributes a public MethodEmitter CreateMethod(string name, MethodAttributes attrs, Type returnType, params Type[] argumentTypes) { - var member = new MethodEmitter(this, name, attrs, returnType, argumentTypes ?? Type.EmptyTypes); + var member = new MethodEmitter(Context, this, name, attrs, returnType, argumentTypes ?? Type.EmptyTypes); methods.Add(member); return member; } @@ -189,14 +196,14 @@ public MethodEmitter CreateMethod(string name, MethodInfo methodToUseAsATemplate public MethodEmitter CreateMethod(string name, MethodAttributes attributes, MethodInfo methodToUseAsATemplate) { - var method = new MethodEmitter(this, name, attributes, methodToUseAsATemplate); + var method = new MethodEmitter(Context, this, name, attributes, methodToUseAsATemplate); methods.Add(method); return method; } public PropertyEmitter CreateProperty(string name, PropertyAttributes attributes, Type propertyType, Type[] arguments) { - var propEmitter = new PropertyEmitter(this, name, attributes, propertyType, arguments); + var propEmitter = new PropertyEmitter(Context, this, name, attributes, propertyType, arguments); properties.Add(propEmitter); return propEmitter; } diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/ClassEmitter.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/ClassEmitter.cs index cb2023bc87..f32924682b 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/ClassEmitter.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/ClassEmitter.cs @@ -29,15 +29,15 @@ internal class ClassEmitter : AbstractTypeEmitter private readonly ModuleScope moduleScope; - public ClassEmitter(ModuleScope modulescope, string name, Type baseType, IEnumerable interfaces) - : this(modulescope, name, baseType, interfaces, DefaultAttributes, forceUnsigned: false) + public ClassEmitter(ProxyGenerationContext context, ModuleScope modulescope, string name, Type baseType, IEnumerable interfaces) + : this(context, modulescope, name, baseType, interfaces, DefaultAttributes, forceUnsigned: false) { } - public ClassEmitter(ModuleScope modulescope, string name, Type baseType, IEnumerable interfaces, + public ClassEmitter(ProxyGenerationContext context, ModuleScope modulescope, string name, Type baseType, IEnumerable interfaces, TypeAttributes flags, bool forceUnsigned) - : this(CreateTypeBuilder(modulescope, name, baseType, interfaces, flags, forceUnsigned)) + : this(context, CreateTypeBuilder(modulescope, name, baseType, interfaces, flags, forceUnsigned)) { interfaces = InitializeGenericArgumentsFromBases(ref baseType, interfaces); @@ -60,8 +60,8 @@ public ClassEmitter(ModuleScope modulescope, string name, Type baseType, IEnumer moduleScope = modulescope; } - public ClassEmitter(TypeBuilder typeBuilder) - : base(typeBuilder) + public ClassEmitter(ProxyGenerationContext context, TypeBuilder typeBuilder) + : base(context, typeBuilder) { } diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/EventEmitter.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/EventEmitter.cs index 08ad72ea3e..a99ed332b8 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/EventEmitter.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/EventEmitter.cs @@ -20,13 +20,14 @@ namespace Castle.DynamicProxy.Generators.Emitters internal class EventEmitter : IMemberEmitter { + private readonly ProxyGenerationContext context; private readonly EventBuilder eventBuilder; private readonly Type type; private readonly AbstractTypeEmitter typeEmitter; private MethodEmitter addMethod; private MethodEmitter removeMethod; - public EventEmitter(AbstractTypeEmitter typeEmitter, string name, EventAttributes attributes, Type type) + public EventEmitter(ProxyGenerationContext context, AbstractTypeEmitter typeEmitter, string name, EventAttributes attributes, Type type) { if (name == null) { @@ -36,6 +37,7 @@ public EventEmitter(AbstractTypeEmitter typeEmitter, string name, EventAttribute { throw new ArgumentNullException(nameof(type)); } + this.context = context; this.typeEmitter = typeEmitter; this.type = type; eventBuilder = typeEmitter.TypeBuilder.DefineEvent(name, attributes, type); @@ -58,7 +60,7 @@ public MethodEmitter CreateAddMethod(string addMethodName, MethodAttributes attr throw new InvalidOperationException("An add method exists"); } - addMethod = new MethodEmitter(typeEmitter, addMethodName, attributes, methodToOverride); + addMethod = new MethodEmitter(context, typeEmitter, addMethodName, attributes, methodToOverride); return addMethod; } @@ -69,7 +71,7 @@ public MethodEmitter CreateRemoveMethod(string removeMethodName, MethodAttribute { throw new InvalidOperationException("A remove method exists"); } - removeMethod = new MethodEmitter(typeEmitter, removeMethodName, attributes, methodToOverride); + removeMethod = new MethodEmitter(context, typeEmitter, removeMethodName, attributes, methodToOverride); return removeMethod; } diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/GenericUtil.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/GenericUtil.cs index 0447dc7533..efb79c4acc 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/GenericUtil.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/GenericUtil.cs @@ -30,21 +30,23 @@ internal class GenericUtil public static GenericTypeParameterBuilder[] CopyGenericArguments( MethodInfo methodToCopyGenericsFrom, TypeBuilder builder, + ProxyGenerationContext context, Dictionary name2GenericType) { return CopyGenericArguments(methodToCopyGenericsFrom, name2GenericType, - builder.DefineGenericParameters); + builder.DefineGenericParameters, context); } public static GenericTypeParameterBuilder[] CopyGenericArguments( MethodInfo methodToCopyGenericsFrom, MethodBuilder builder, + ProxyGenerationContext context, Dictionary name2GenericType) { return CopyGenericArguments(methodToCopyGenericsFrom, name2GenericType, - builder.DefineGenericParameters); + builder.DefineGenericParameters, context); } public static Type ExtractCorrectType(Type paramType, Dictionary name2GenericType) @@ -185,7 +187,8 @@ private static Type[] AdjustGenericConstraints(MethodInfo methodToCopyGenericsFr private static GenericTypeParameterBuilder[] CopyGenericArguments( MethodInfo methodToCopyGenericsFrom, Dictionary name2GenericType, - ApplyGenArgs genericParameterGenerator) + ApplyGenArgs genericParameterGenerator, + ProxyGenerationContext context) { var originalGenericArguments = methodToCopyGenericsFrom.GetGenericArguments(); if (originalGenericArguments.Length == 0) @@ -205,7 +208,7 @@ private static GenericTypeParameterBuilder[] CopyGenericArguments( var constraints = AdjustGenericConstraints(methodToCopyGenericsFrom, newGenericParameters, originalGenericArguments, originalGenericArguments[i].GetGenericParameterConstraints()); newGenericParameters[i].SetInterfaceConstraints(constraints); - CopyNonInheritableAttributes(newGenericParameters[i], originalGenericArguments[i]); + CopyNonInheritableAttributes(newGenericParameters[i], originalGenericArguments[i], context); } catch (NotSupportedException) { @@ -221,9 +224,10 @@ private static GenericTypeParameterBuilder[] CopyGenericArguments( } private static void CopyNonInheritableAttributes(GenericTypeParameterBuilder newGenericParameter, - Type originalGenericArgument) + Type originalGenericArgument, + ProxyGenerationContext context) { - foreach (var attribute in originalGenericArgument.GetNonInheritableAttributes()) + foreach (var attribute in originalGenericArgument.GetNonInheritableAttributes(context)) { newGenericParameter.SetCustomAttribute(attribute.Builder); } diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/MethodEmitter.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/MethodEmitter.cs index b508e96f35..ac4a17c1a8 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/MethodEmitter.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/MethodEmitter.cs @@ -27,35 +27,37 @@ namespace Castle.DynamicProxy.Generators.Emitters [DebuggerDisplay("{builder.Name}")] internal class MethodEmitter : IMemberEmitter { + private readonly ProxyGenerationContext context; private readonly MethodBuilder builder; private readonly CodeBuilder codeBuilder; private readonly GenericTypeParameterBuilder[] genericTypeParams; private ArgumentReference[] arguments; - protected internal MethodEmitter(MethodBuilder builder) + protected internal MethodEmitter(ProxyGenerationContext context, MethodBuilder builder) { + this.context = context; this.builder = builder; codeBuilder = new CodeBuilder(); } - internal MethodEmitter(AbstractTypeEmitter owner, string name, MethodAttributes attributes) - : this(owner.TypeBuilder.DefineMethod(name, attributes)) + internal MethodEmitter(ProxyGenerationContext context, AbstractTypeEmitter owner, string name, MethodAttributes attributes) + : this(context, owner.TypeBuilder.DefineMethod(name, attributes)) { } - internal MethodEmitter(AbstractTypeEmitter owner, string name, + internal MethodEmitter(ProxyGenerationContext context, AbstractTypeEmitter owner, string name, MethodAttributes attributes, Type returnType, params Type[] argumentTypes) - : this(owner, name, attributes) + : this(context, owner, name, attributes) { SetParameters(argumentTypes); SetReturnType(returnType); } - internal MethodEmitter(AbstractTypeEmitter owner, string name, + internal MethodEmitter(ProxyGenerationContext context, AbstractTypeEmitter owner, string name, MethodAttributes attributes, MethodInfo methodToUseAsATemplate) - : this(owner, name, attributes) + : this(context, owner, name, attributes) { var name2GenericType = GenericUtil.GetGenericArgumentsMap(owner); @@ -63,7 +65,7 @@ internal MethodEmitter(AbstractTypeEmitter owner, string name, var baseMethodParameters = methodToUseAsATemplate.GetParameters(); var parameters = GenericUtil.ExtractParametersTypes(baseMethodParameters, name2GenericType); - genericTypeParams = GenericUtil.CopyGenericArguments(methodToUseAsATemplate, builder, name2GenericType); + genericTypeParams = GenericUtil.CopyGenericArguments(methodToUseAsATemplate, builder, context, name2GenericType); SetParameters(parameters); SetReturnType(returnType); SetSignature(returnType, methodToUseAsATemplate.ReturnParameter, parameters, baseMethodParameters); @@ -145,7 +147,7 @@ private void DefineParameters(ParameterInfo[] parameters) foreach (var parameter in parameters) { var parameterBuilder = builder.DefineParameter(parameter.Position + 1, parameter.Attributes, parameter.Name); - foreach (var attribute in parameter.GetNonInheritableAttributes()) + foreach (var attribute in parameter.GetNonInheritableAttributes(context)) { parameterBuilder.SetCustomAttribute(attribute.Builder); } diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/NestedClassEmitter.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/NestedClassEmitter.cs index 53809e46b2..173fc63854 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/NestedClassEmitter.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/NestedClassEmitter.cs @@ -20,22 +20,23 @@ namespace Castle.DynamicProxy.Generators.Emitters internal class NestedClassEmitter : AbstractTypeEmitter { - public NestedClassEmitter(AbstractTypeEmitter maintype, string name, Type baseType, Type[] interfaces) + public NestedClassEmitter(ProxyGenerationContext context, AbstractTypeEmitter maintype, string name, Type baseType, Type[] interfaces) : this( + context, maintype, CreateTypeBuilder(maintype, name, TypeAttributes.Sealed | TypeAttributes.NestedPublic | TypeAttributes.Class, baseType, interfaces)) { } - public NestedClassEmitter(AbstractTypeEmitter maintype, string name, TypeAttributes attributes, Type baseType, + public NestedClassEmitter(ProxyGenerationContext context, AbstractTypeEmitter maintype, string name, TypeAttributes attributes, Type baseType, Type[] interfaces) - : this(maintype, CreateTypeBuilder(maintype, name, attributes, baseType, interfaces)) + : this(context, maintype, CreateTypeBuilder(maintype, name, attributes, baseType, interfaces)) { } - public NestedClassEmitter(AbstractTypeEmitter maintype, TypeBuilder typeBuilder) - : base(typeBuilder) + public NestedClassEmitter(ProxyGenerationContext context, AbstractTypeEmitter maintype, TypeBuilder typeBuilder) + : base(context, typeBuilder) { maintype.AddNestedClass(this); } diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/PropertyEmitter.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/PropertyEmitter.cs index c2674b3dea..96f18bc593 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/PropertyEmitter.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/PropertyEmitter.cs @@ -20,14 +20,16 @@ namespace Castle.DynamicProxy.Generators.Emitters internal class PropertyEmitter : IMemberEmitter { + private readonly ProxyGenerationContext context; private readonly PropertyBuilder builder; private readonly AbstractTypeEmitter parentTypeEmitter; private MethodEmitter getMethod; private MethodEmitter setMethod; - public PropertyEmitter(AbstractTypeEmitter parentTypeEmitter, string name, PropertyAttributes attributes, + public PropertyEmitter(ProxyGenerationContext context, AbstractTypeEmitter parentTypeEmitter, string name, PropertyAttributes attributes, Type propertyType, Type[] arguments) { + this.context = context; this.parentTypeEmitter = parentTypeEmitter; builder = parentTypeEmitter.TypeBuilder.DefineProperty( @@ -53,7 +55,7 @@ public MethodEmitter CreateGetMethod(string name, MethodAttributes attrs, Method throw new InvalidOperationException("A get method exists"); } - getMethod = new MethodEmitter(parentTypeEmitter, name, attrs, methodToOverride); + getMethod = new MethodEmitter(context, parentTypeEmitter, name, attrs, methodToOverride); return getMethod; } @@ -70,7 +72,7 @@ public MethodEmitter CreateSetMethod(string name, MethodAttributes attrs, Method throw new InvalidOperationException("A set method exists"); } - setMethod = new MethodEmitter(parentTypeEmitter, name, attrs, methodToOverride); + setMethod = new MethodEmitter(context, parentTypeEmitter, name, attrs, methodToOverride); return setMethod; } diff --git a/src/Castle.Core/DynamicProxy/Generators/InheritanceInvocationTypeGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/InheritanceInvocationTypeGenerator.cs index b02d5876e9..309e42609a 100644 --- a/src/Castle.Core/DynamicProxy/Generators/InheritanceInvocationTypeGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/InheritanceInvocationTypeGenerator.cs @@ -26,9 +26,9 @@ internal class InheritanceInvocationTypeGenerator : InvocationTypeGenerator { public static readonly Type BaseType = typeof(InheritanceInvocation); - public InheritanceInvocationTypeGenerator(Type targetType, MetaMethod method, MethodInfo callback, + public InheritanceInvocationTypeGenerator(ProxyGenerationContext context, Type targetType, MetaMethod method, MethodInfo callback, IInvocationCreationContributor contributor) - : base(targetType, method, callback, false, contributor) + : base(context, targetType, method, callback, false, contributor) { } diff --git a/src/Castle.Core/DynamicProxy/Generators/InvocationTypeGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/InvocationTypeGenerator.cs index 133d9d77da..28c203f885 100644 --- a/src/Castle.Core/DynamicProxy/Generators/InvocationTypeGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/InvocationTypeGenerator.cs @@ -26,15 +26,17 @@ namespace Castle.DynamicProxy.Generators internal abstract class InvocationTypeGenerator : IGenerator { + private readonly ProxyGenerationContext context; protected readonly MetaMethod method; protected readonly Type targetType; private readonly MethodInfo callback; private readonly bool canChangeTarget; private readonly IInvocationCreationContributor contributor; - protected InvocationTypeGenerator(Type targetType, MetaMethod method, MethodInfo callback, bool canChangeTarget, + protected InvocationTypeGenerator(ProxyGenerationContext context, Type targetType, MetaMethod method, MethodInfo callback, bool canChangeTarget, IInvocationCreationContributor contributor) { + this.context = context; this.targetType = targetType; this.method = method; this.callback = callback; @@ -42,6 +44,11 @@ protected InvocationTypeGenerator(Type targetType, MetaMethod method, MethodInfo this.contributor = contributor; } + protected ProxyGenerationContext Context + { + get { return context; } + } + /// /// Generates the constructor for the class that extends /// @@ -260,7 +267,7 @@ private AbstractTypeEmitter GetEmitter(ClassEmitter @class, Type[] interfaces, I var suggestedName = string.Format("Castle.Proxies.Invocations.{0}_{1}", methodInfo.DeclaringType.Name, methodInfo.Name); var uniqueName = namingScope.ParentScope.GetUniqueName(suggestedName); - return new ClassEmitter(@class.ModuleScope, uniqueName, GetBaseType(), interfaces, ClassEmitter.DefaultAttributes, forceUnsigned: @class.InStrongNamedModule == false); + return new ClassEmitter(context, @class.ModuleScope, uniqueName, GetBaseType(), interfaces, ClassEmitter.DefaultAttributes, forceUnsigned: @class.InStrongNamedModule == false); } private void ImplemementInvokeMethodOnTarget(AbstractTypeEmitter invocation, ParameterInfo[] parameters, diff --git a/src/Castle.Core/DynamicProxy/Internal/AttributeUtil.cs b/src/Castle.Core/DynamicProxy/Internal/AttributeUtil.cs index b21ce85b8b..5f90fd3244 100644 --- a/src/Castle.Core/DynamicProxy/Internal/AttributeUtil.cs +++ b/src/Castle.Core/DynamicProxy/Internal/AttributeUtil.cs @@ -118,7 +118,7 @@ private static void GetSettersAndFields(Type attributeType, IEnumerable GetNonInheritableAttributes(this MemberInfo member) + public static IEnumerable GetNonInheritableAttributes(this MemberInfo member, ProxyGenerationContext context) { Debug.Assert(member != null, "member != null"); var attributes = member.CustomAttributes; @@ -126,7 +126,7 @@ public static IEnumerable GetNonInheritableAttributes(this foreach (var attribute in attributes) { var attributeType = attribute.AttributeType; - if (ShouldSkipAttributeReplication(attributeType, ignoreInheritance: false)) + if (ShouldSkipAttributeReplication(attributeType, ignoreInheritance: false, context)) { continue; } @@ -155,7 +155,7 @@ public static IEnumerable GetNonInheritableAttributes(this } } - public static IEnumerable GetNonInheritableAttributes(this ParameterInfo parameter) + public static IEnumerable GetNonInheritableAttributes(this ParameterInfo parameter, ProxyGenerationContext context) { Debug.Assert(parameter != null, "parameter != null"); @@ -167,7 +167,7 @@ public static IEnumerable GetNonInheritableAttributes(this { var attributeType = attribute.AttributeType; - if (ShouldSkipAttributeReplication(attributeType, ignoreInheritance)) + if (ShouldSkipAttributeReplication(attributeType, ignoreInheritance, context)) { continue; } @@ -185,7 +185,7 @@ public static IEnumerable GetNonInheritableAttributes(this /// but there are some special cases where the attributes means /// something to the CLR, where they should be skipped. /// - private static bool ShouldSkipAttributeReplication(Type attribute, bool ignoreInheritance) + private static bool ShouldSkipAttributeReplication(Type attribute, bool ignoreInheritance, ProxyGenerationContext context) { if (attribute.IsPublic == false) { @@ -204,7 +204,7 @@ private static bool ShouldSkipAttributeReplication(Type attribute, bool ignoreIn return true; } - if (AttributesToAvoidReplicating.AsList().Any(a => a.IsAssignableFrom(attribute))) + if (context.AttributesToAvoidReplicating.Any(a => a.IsAssignableFrom(attribute))) { return true; } From dd14444ef64e67b366c11f4997944aed37732456 Mon Sep 17 00:00:00 2001 From: Dominique Schuppli Date: Fri, 12 Feb 2021 02:10:10 +0100 Subject: [PATCH 10/13] Add `AttributesToAvoidReplicating` set in options --- .../DynamicProxy/ProxyGenerationOptions.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/Castle.Core/DynamicProxy/ProxyGenerationOptions.cs b/src/Castle.Core/DynamicProxy/ProxyGenerationOptions.cs index 515de4bef0..e338f60edd 100644 --- a/src/Castle.Core/DynamicProxy/ProxyGenerationOptions.cs +++ b/src/Castle.Core/DynamicProxy/ProxyGenerationOptions.cs @@ -45,6 +45,7 @@ public class ProxyGenerationOptions private List mixins; private readonly IList additionalAttributes = new List(); + private readonly HashSet attributesToAvoidReplicating = new HashSet(); #if FEATURE_SERIALIZATION [NonSerialized] @@ -76,6 +77,7 @@ private ProxyGenerationOptions(SerializationInfo info, StreamingContext context) Selector = (IInterceptorSelector)info.GetValue("selector", typeof(IInterceptorSelector)); mixins = (List)info.GetValue("mixins", typeof(List)); BaseTypeForInterfaceProxy = Type.GetType(info.GetString("baseTypeForInterfaceProxy.AssemblyQualifiedName")); + attributesToAvoidReplicating = (HashSet)info.GetValue("attributesToAvoidReplicating", typeof(HashSet)); } #endif @@ -102,6 +104,7 @@ public void GetObjectData(SerializationInfo info, StreamingContext context) info.AddValue("selector", Selector); info.AddValue("mixins", mixins); info.AddValue("baseTypeForInterfaceProxy.AssemblyQualifiedName", BaseTypeForInterfaceProxy.AssemblyQualifiedName); + info.AddValue("attributesToAvoidReplicating", attributesToAvoidReplicating); } #endif @@ -149,6 +152,14 @@ public IList AdditionalAttributes get { return additionalAttributes; } } + /// + /// Gets the set of types that should not be replicated on proxies. + /// + public ISet AttributesToAvoidReplicating + { + get { return attributesToAvoidReplicating; } + } + public MixinData MixinData { get @@ -281,6 +292,10 @@ public override bool Equals(object obj) { return false; } + if (!AttributesToAvoidReplicating.SetEquals(proxyGenerationOptions.AttributesToAvoidReplicating)) + { + return false; + } return true; } @@ -294,6 +309,7 @@ public override int GetHashCode() result = 29*result + MixinData.GetHashCode(); result = 29*result + (BaseTypeForInterfaceProxy != null ? BaseTypeForInterfaceProxy.GetHashCode() : 0); result = 29*result + GetAdditionalAttributesHashCode(); + result = 29*result + AttributesToAvoidReplicating.Count.GetHashCode(); return result; } From 7699715da7482e3e7b4828f0594e341097c9351d Mon Sep 17 00:00:00 2001 From: Dominique Schuppli Date: Fri, 12 Feb 2021 02:10:29 +0100 Subject: [PATCH 11/13] Reroute `AttributesToAvoidReplicating` in context to options --- src/Castle.Core/DynamicProxy/ProxyGenerationContext.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Castle.Core/DynamicProxy/ProxyGenerationContext.cs b/src/Castle.Core/DynamicProxy/ProxyGenerationContext.cs index 4d525e91a6..e69470ed65 100644 --- a/src/Castle.Core/DynamicProxy/ProxyGenerationContext.cs +++ b/src/Castle.Core/DynamicProxy/ProxyGenerationContext.cs @@ -29,12 +29,12 @@ public ProxyGenerationContext(ProxyGenerationOptions options, Options = options ?? ProxyGenerationOptions.Default; Options.Initialize(); - AttributesToAvoidReplicating = Generators.AttributesToAvoidReplicating.AsList(); + AttributesToAvoidReplicating = Options.AttributesToAvoidReplicating; Hook = Options.Hook ?? new AllMethodsHook(); } - public IList AttributesToAvoidReplicating { get; } + public ISet AttributesToAvoidReplicating { get; } public IProxyGenerationHook Hook { get; } From ab1647482a8cc5b1e7ad3546ff484aa35025df57 Mon Sep 17 00:00:00 2001 From: Dominique Schuppli Date: Fri, 12 Feb 2021 02:16:46 +0100 Subject: [PATCH 12/13] Remove static `AttributesToAvoidReplicating` class --- .../AttributesToAvoidReplicatingTestCase.cs | 16 +++--- .../AttributesToAvoidReplicating.cs | 51 ------------------- .../DynamicProxy/Internal/AttributeUtil.cs | 4 +- 3 files changed, 10 insertions(+), 61 deletions(-) delete mode 100644 src/Castle.Core/DynamicProxy/Generators/AttributesToAvoidReplicating.cs diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/AttributesToAvoidReplicatingTestCase.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/AttributesToAvoidReplicatingTestCase.cs index 2d908d6abf..61711bc9e9 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/AttributesToAvoidReplicatingTestCase.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/AttributesToAvoidReplicatingTestCase.cs @@ -19,7 +19,6 @@ namespace Castle.DynamicProxy.Tests using System.Reflection; using System.Security.Permissions; - using Castle.DynamicProxy.Generators; using Castle.DynamicProxy.Tests.Classes; using NUnit.Framework; @@ -28,18 +27,19 @@ namespace Castle.DynamicProxy.Tests public class AttributesToAvoidReplicatingTestCase : BasePEVerifyTestCase { [Test] - public void After_adding_attribute_must_be_listed_as_contained() + public void NonInheritableAttribute_should_be_replicated_as_it_is_not_inherited() { - AttributesToAvoidReplicating.Add(); - bool contains = AttributesToAvoidReplicating.AsList().Contains(typeof(string)); - Assert.IsTrue(contains); + var proxy = generator.CreateClassProxy(); + Assert.AreEqual(1, AttributeCount(proxy)); } [Test] - public void NonInheritableAttribute_should_be_replicated_as_it_is_not_inherited() + public void NonInheritableAttribute_can_be_suppressed_via_AttributesToAvoidReplicating() { - var proxy = generator.CreateClassProxy(); - Assert.AreEqual(1, AttributeCount(proxy)); + var options = new ProxyGenerationOptions(); + options.AttributesToAvoidReplicating.Add(typeof(NonInheritableAttribute)); + var proxy = generator.CreateClassProxy(options); + Assert.AreEqual(0, AttributeCount(proxy)); } [NonInheritable] diff --git a/src/Castle.Core/DynamicProxy/Generators/AttributesToAvoidReplicating.cs b/src/Castle.Core/DynamicProxy/Generators/AttributesToAvoidReplicating.cs deleted file mode 100644 index f93437862b..0000000000 --- a/src/Castle.Core/DynamicProxy/Generators/AttributesToAvoidReplicating.cs +++ /dev/null @@ -1,51 +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 -{ - using System; - using System.Collections.Generic; - using System.Reflection; - - public static class AttributesToAvoidReplicating - { - private static readonly object lockObject = new object(); - - private static IList attributes; - - static AttributesToAvoidReplicating() - { - attributes = new List(); - } - - public static void Add(Type attribute) - { - // note: this class is made thread-safe by replacing the backing list rather than adding to it - lock (lockObject) - { - attributes = new List(attributes) { attribute }; - } - } - - public static void Add() - { - Add(typeof(T)); - } - - internal static IList AsList() - { - return attributes; - } - } -} \ No newline at end of file diff --git a/src/Castle.Core/DynamicProxy/Internal/AttributeUtil.cs b/src/Castle.Core/DynamicProxy/Internal/AttributeUtil.cs index 5f90fd3244..4452e78dbe 100644 --- a/src/Castle.Core/DynamicProxy/Internal/AttributeUtil.cs +++ b/src/Castle.Core/DynamicProxy/Internal/AttributeUtil.cs @@ -141,11 +141,11 @@ public static IEnumerable GetNonInheritableAttributes(this var message = string.Format( "Due to limitations in CLR, DynamicProxy was unable to successfully replicate non-inheritable attribute {0} on {1}{2}. " + - "To avoid this error you can chose not to replicate this attribute type by calling '{3}.Add(typeof({0}))'.", + "To avoid this error you can chose not to replicate this attribute type by calling 'proxyGenerationOptions.{3}.Add(typeof({0}))'.", attributeType.FullName, member.DeclaringType.FullName, (member is TypeInfo) ? "" : ("." + member.Name), - typeof(AttributesToAvoidReplicating).FullName); + nameof(ProxyGenerationOptions.AttributesToAvoidReplicating)); throw new NotSupportedException(message, e); } if (info != null) From d684ef23332e791cecdce30b57dc17c5fb224e3d Mon Sep 17 00:00:00 2001 From: Dominique Schuppli Date: Fri, 12 Feb 2021 02:19:39 +0100 Subject: [PATCH 13/13] Update changelog and `ref/` contract files --- CHANGELOG.md | 1 + ref/Castle.Core-net45.cs | 10 +--------- ref/Castle.Core-netstandard2.0.cs | 10 +--------- ref/Castle.Core-netstandard2.1.cs | 10 +--------- 4 files changed, 4 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 68dfe004c6..41066c123a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Enhancements: - .NET Standard 2.0 and 2.1 support (@lg2de, #485) - Non-intercepted methods on a class proxy with target are now forwarded to the target (@stakx, #571) - Significant performance improvements with proxy type generation for interface proxies without target. (Up until now, DynamicProxy generated a separate `IInvocation` implementation type for every single proxied method – it is now able to reuse a single predefined type in many cases, thereby reducing the total amount of dynamic type generation.) (@stakx, #573) +- `AttributesToAvoidReplicating` has been converted from a static class to a collection property on `ProxyGenerationOptions` (@stakx, #575) Bugfixes: - Proxying certain `[Serializable]` classes produces proxy types that fail PEVerify test (@stakx, #367) diff --git a/ref/Castle.Core-net45.cs b/ref/Castle.Core-net45.cs index f4914b2c7b..ea14b816ac 100644 --- a/ref/Castle.Core-net45.cs +++ b/ref/Castle.Core-net45.cs @@ -2653,6 +2653,7 @@ public class ProxyGenerationOptions : System.Runtime.Serialization.ISerializable public ProxyGenerationOptions() { } public ProxyGenerationOptions(Castle.DynamicProxy.IProxyGenerationHook hook) { } public System.Collections.Generic.IList AdditionalAttributes { get; } + public System.Collections.Generic.ISet AttributesToAvoidReplicating { get; } public System.Type BaseTypeForInterfaceProxy { get; set; } public bool HasMixins { get; } public Castle.DynamicProxy.IProxyGenerationHook Hook { get; set; } @@ -2758,15 +2759,6 @@ protected virtual void PostProceed(Castle.DynamicProxy.IInvocation invocation) { protected virtual void PreProceed(Castle.DynamicProxy.IInvocation invocation) { } } } -namespace Castle.DynamicProxy.Generators -{ - public static class AttributesToAvoidReplicating - { - public static void Add(System.Type attribute) { } - public static void Add() { } - public static bool Contains(System.Type attribute) { } - } -} namespace Castle.DynamicProxy.Internal { public abstract class CompositionInvocation : Castle.DynamicProxy.AbstractInvocation diff --git a/ref/Castle.Core-netstandard2.0.cs b/ref/Castle.Core-netstandard2.0.cs index e2d8604e8f..c92c3c40bc 100644 --- a/ref/Castle.Core-netstandard2.0.cs +++ b/ref/Castle.Core-netstandard2.0.cs @@ -2608,6 +2608,7 @@ public class ProxyGenerationOptions public ProxyGenerationOptions() { } public ProxyGenerationOptions(Castle.DynamicProxy.IProxyGenerationHook hook) { } public System.Collections.Generic.IList AdditionalAttributes { get; } + public System.Collections.Generic.ISet AttributesToAvoidReplicating { get; } public System.Type BaseTypeForInterfaceProxy { get; set; } public bool HasMixins { get; } public Castle.DynamicProxy.IProxyGenerationHook Hook { get; set; } @@ -2711,15 +2712,6 @@ protected virtual void PostProceed(Castle.DynamicProxy.IInvocation invocation) { protected virtual void PreProceed(Castle.DynamicProxy.IInvocation invocation) { } } } -namespace Castle.DynamicProxy.Generators -{ - public static class AttributesToAvoidReplicating - { - public static void Add(System.Type attribute) { } - public static void Add() { } - public static bool Contains(System.Type attribute) { } - } -} namespace Castle.DynamicProxy.Internal { public abstract class CompositionInvocation : Castle.DynamicProxy.AbstractInvocation diff --git a/ref/Castle.Core-netstandard2.1.cs b/ref/Castle.Core-netstandard2.1.cs index 00e2896aa3..dfc8be04c4 100644 --- a/ref/Castle.Core-netstandard2.1.cs +++ b/ref/Castle.Core-netstandard2.1.cs @@ -2608,6 +2608,7 @@ public class ProxyGenerationOptions public ProxyGenerationOptions() { } public ProxyGenerationOptions(Castle.DynamicProxy.IProxyGenerationHook hook) { } public System.Collections.Generic.IList AdditionalAttributes { get; } + public System.Collections.Generic.ISet AttributesToAvoidReplicating { get; } public System.Type BaseTypeForInterfaceProxy { get; set; } public bool HasMixins { get; } public Castle.DynamicProxy.IProxyGenerationHook Hook { get; set; } @@ -2711,15 +2712,6 @@ protected virtual void PostProceed(Castle.DynamicProxy.IInvocation invocation) { protected virtual void PreProceed(Castle.DynamicProxy.IInvocation invocation) { } } } -namespace Castle.DynamicProxy.Generators -{ - public static class AttributesToAvoidReplicating - { - public static void Add(System.Type attribute) { } - public static void Add() { } - public static bool Contains(System.Type attribute) { } - } -} namespace Castle.DynamicProxy.Internal { public abstract class CompositionInvocation : Castle.DynamicProxy.AbstractInvocation