From 802b3d5c9011fdf38acbc9713b09b78aca0cc272 Mon Sep 17 00:00:00 2001 From: stakx Date: Fri, 12 Jun 2020 17:41:12 +0200 Subject: [PATCH 01/10] Stop generating serializable proxy/invocation types --- .../ClassProxyInstanceContributor.cs | 184 ------------------ .../InterfaceProxyInstanceContributor.cs | 21 -- .../Contributors/ProxyInstanceContributor.cs | 117 ----------- .../AttributesToAvoidReplicating.cs | 1 + .../Generators/BaseProxyGenerator.cs | 24 +-- .../Generators/ClassProxyGenerator.cs | 6 - .../ClassProxyWithTargetGenerator.cs | 15 +- .../Generators/Emitters/ClassEmitter.cs | 2 +- .../InterfaceProxyWithTargetGenerator.cs | 17 -- .../Generators/InvocationTypeGenerator.cs | 4 - .../MethodWithInvocationGenerator.cs | 9 +- 11 files changed, 7 insertions(+), 393 deletions(-) diff --git a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyInstanceContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyInstanceContributor.cs index 7560206478..af37cfd0fa 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyInstanceContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyInstanceContributor.cs @@ -17,9 +17,6 @@ namespace Castle.DynamicProxy.Contributors using System; using System.Collections.Generic; using System.Reflection; -#if FEATURE_SERIALIZATION - using System.Runtime.Serialization; -#endif using Castle.DynamicProxy.Generators.Emitters; using Castle.DynamicProxy.Generators.Emitters.CodeBuilders; @@ -29,24 +26,10 @@ namespace Castle.DynamicProxy.Contributors internal class ClassProxyInstanceContributor : ProxyInstanceContributor { -#if FEATURE_SERIALIZATION - private readonly bool delegateToBaseGetObjectData; - private readonly bool implementISerializable; - private ConstructorInfo serializationConstructor; - private readonly IList serializedFields = new List(); -#endif - public ClassProxyInstanceContributor(Type targetType, IList methodsToSkip, Type[] interfaces, string typeId) : base(targetType, interfaces, typeId) { -#if FEATURE_SERIALIZATION - if (targetType.IsSerializable) - { - implementISerializable = true; - delegateToBaseGetObjectData = VerifyIfBaseImplementsGetObjectData(targetType, methodsToSkip); - } -#endif } protected override Reference GetTargetReference(ClassEmitter emitter) @@ -57,178 +40,11 @@ protected override Reference GetTargetReference(ClassEmitter emitter) public override void Generate(ClassEmitter @class, ProxyGenerationOptions options) { var interceptors = @class.GetField("__interceptors"); -#if FEATURE_SERIALIZATION - if (implementISerializable) - { - ImplementGetObjectData(@class); - Constructor(@class); - } -#endif ImplementProxyTargetAccessor(@class, interceptors); foreach (var attribute in targetType.GetNonInheritableAttributes()) { @class.DefineCustomAttribute(attribute.Builder); } } - -#if FEATURE_SERIALIZATION - protected override void AddAddValueInvocation(ArgumentReference serializationInfo, MethodEmitter getObjectData, - FieldReference field) - { - serializedFields.Add(field); - base.AddAddValueInvocation(serializationInfo, getObjectData, field); - } - - protected override void CustomizeGetObjectData(AbstractCodeBuilder codebuilder, ArgumentReference serializationInfo, - ArgumentReference streamingContext, ClassEmitter emitter) - { - codebuilder.AddStatement(new ExpressionStatement( - new MethodInvocationExpression( - serializationInfo, - SerializationInfoMethods.AddValue_Bool, - new ConstReference("__delegateToBase").ToExpression(), - new ConstReference(delegateToBaseGetObjectData). - ToExpression()))); - - if (delegateToBaseGetObjectData == false) - { - EmitCustomGetObjectData(codebuilder, serializationInfo); - return; - } - - EmitCallToBaseGetObjectData(codebuilder, serializationInfo, streamingContext); - } - - private void EmitCustomGetObjectData(AbstractCodeBuilder codebuilder, ArgumentReference serializationInfo) - { - var members = codebuilder.DeclareLocal(typeof(MemberInfo[])); - var data = codebuilder.DeclareLocal(typeof(object[])); - - var getSerializableMembers = new MethodInvocationExpression( - null, - FormatterServicesMethods.GetSerializableMembers, - new TypeTokenExpression(targetType)); - codebuilder.AddStatement(new AssignStatement(members, getSerializableMembers)); - - // Sort to keep order on both serialize and deserialize side the same, c.f DYNPROXY-ISSUE-127 - var callSort = new MethodInvocationExpression( - null, - TypeUtilMethods.Sort, - members.ToExpression()); - codebuilder.AddStatement(new AssignStatement(members, callSort)); - - var getObjectData = new MethodInvocationExpression( - null, - FormatterServicesMethods.GetObjectData, - SelfReference.Self.ToExpression(), - members.ToExpression()); - codebuilder.AddStatement(new AssignStatement(data, getObjectData)); - - var addValue = new MethodInvocationExpression( - serializationInfo, - SerializationInfoMethods.AddValue_Object, - new ConstReference("__data").ToExpression(), - data.ToExpression()); - codebuilder.AddStatement(new ExpressionStatement(addValue)); - } - - private void EmitCallToBaseGetObjectData(AbstractCodeBuilder codebuilder, ArgumentReference serializationInfo, - ArgumentReference streamingContext) - { - var baseGetObjectData = targetType.GetMethod("GetObjectData", - new[] { typeof(SerializationInfo), typeof(StreamingContext) }); - - codebuilder.AddStatement(new ExpressionStatement( - new MethodInvocationExpression(baseGetObjectData, - serializationInfo.ToExpression(), - streamingContext.ToExpression()))); - } - - private void Constructor(ClassEmitter emitter) - { - if (!delegateToBaseGetObjectData) - { - return; - } - GenerateSerializationConstructor(emitter); - } - - private void GenerateSerializationConstructor(ClassEmitter emitter) - { - var serializationInfo = new ArgumentReference(typeof(SerializationInfo)); - var streamingContext = new ArgumentReference(typeof(StreamingContext)); - - var ctor = emitter.CreateConstructor(serializationInfo, streamingContext); - - ctor.CodeBuilder.AddStatement( - new ConstructorInvocationStatement(serializationConstructor, - serializationInfo.ToExpression(), - streamingContext.ToExpression())); - - foreach (var field in serializedFields) - { - var getValue = new MethodInvocationExpression(serializationInfo, - SerializationInfoMethods.GetValue, - new ConstReference(field.Reference.Name).ToExpression(), - new TypeTokenExpression(field.Reference.FieldType)); - ctor.CodeBuilder.AddStatement(new AssignStatement( - field, - new ConvertExpression(field.Reference.FieldType, - typeof(object), - getValue))); - } - ctor.CodeBuilder.AddStatement(new ReturnStatement()); - } - - private bool VerifyIfBaseImplementsGetObjectData(Type baseType, IList methodsToSkip) - { - if (!typeof(ISerializable).IsAssignableFrom(baseType)) - { - return false; - } - - if (baseType.IsDelegateType()) - { - //working around bug in CLR which returns true for "does this type implement ISerializable" for delegates - return false; - } - - // If base type implements ISerializable, we have to make sure - // the GetObjectData is marked as virtual - var getObjectDataMethod = baseType.GetInterfaceMap(typeof(ISerializable)).TargetMethods[0]; - if (getObjectDataMethod.IsPrivate) //explicit interface implementation - { - return false; - } - - if (!getObjectDataMethod.IsVirtual || getObjectDataMethod.IsFinal) - { - var message = String.Format("The type {0} implements ISerializable, but GetObjectData is not marked as virtual. " + - "Dynamic Proxy needs types implementing ISerializable to mark GetObjectData as virtual " + - "to ensure correct serialization process.", - baseType.FullName); - throw new ArgumentException(message); - } - - methodsToSkip.Add(getObjectDataMethod); - - serializationConstructor = baseType.GetConstructor( - BindingFlags.Instance | BindingFlags.Public | - BindingFlags.NonPublic, - null, - new[] { typeof(SerializationInfo), typeof(StreamingContext) }, - null); - - if (serializationConstructor == null) - { - var message = String.Format("The type {0} implements ISerializable, " + - "but failed to provide a deserialization constructor", - baseType.FullName); - throw new ArgumentException(message); - } - - return true; - } -#endif } } \ No newline at end of file diff --git a/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyInstanceContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyInstanceContributor.cs index 4907393b61..1866ef08be 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyInstanceContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyInstanceContributor.cs @@ -32,26 +32,5 @@ public InterfaceProxyInstanceContributor(Type targetType, string proxyGeneratorI : base(targetType, interfaces, proxyGeneratorId) { } - -#if FEATURE_SERIALIZATION - protected override void CustomizeGetObjectData(AbstractCodeBuilder codebuilder, ArgumentReference serializationInfo, - ArgumentReference streamingContext, ClassEmitter emitter) - { - var targetField = emitter.GetField("__target"); - - codebuilder.AddStatement(new ExpressionStatement( - new MethodInvocationExpression(serializationInfo, SerializationInfoMethods.AddValue_Object, - new ConstReference("__targetFieldType").ToExpression(), - new ConstReference( - targetField.Reference.FieldType.AssemblyQualifiedName). - ToExpression()))); - - codebuilder.AddStatement(new ExpressionStatement( - new MethodInvocationExpression(serializationInfo, SerializationInfoMethods.AddValue_Object, - new ConstReference("__theInterface").ToExpression(), - new ConstReference(targetType.AssemblyQualifiedName). - ToExpression()))); - } -#endif } } \ No newline at end of file diff --git a/src/Castle.Core/DynamicProxy/Contributors/ProxyInstanceContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/ProxyInstanceContributor.cs index 2ee8f777f9..8c79669d1d 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/ProxyInstanceContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/ProxyInstanceContributor.cs @@ -16,18 +16,12 @@ namespace Castle.DynamicProxy.Contributors { using System; using System.Reflection; -#if FEATURE_SERIALIZATION - using System.Runtime.Serialization; -#endif using Castle.DynamicProxy.Generators; using Castle.DynamicProxy.Generators.Emitters; using Castle.DynamicProxy.Generators.Emitters.CodeBuilders; using Castle.DynamicProxy.Generators.Emitters.SimpleAST; using Castle.DynamicProxy.Internal; -#if FEATURE_SERIALIZATION - using Castle.DynamicProxy.Serialization; -#endif using Castle.DynamicProxy.Tokens; internal abstract class ProxyInstanceContributor : ITypeContributor @@ -53,9 +47,6 @@ private Expression GetTargetReferenceExpression(ClassEmitter emitter) public virtual void Generate(ClassEmitter @class, ProxyGenerationOptions options) { var interceptors = @class.GetField("__interceptors"); -#if FEATURE_SERIALIZATION - ImplementGetObjectData(@class); -#endif ImplementProxyTargetAccessor(@class, interceptors); foreach (var attribute in targetType.GetNonInheritableAttributes()) { @@ -94,114 +85,6 @@ protected void ImplementProxyTargetAccessor(ClassEmitter emitter, FieldReference new ReturnStatement(interceptorsField)); } -#if FEATURE_SERIALIZATION - protected void ImplementGetObjectData(ClassEmitter emitter) - { - var getObjectData = emitter.CreateMethod("GetObjectData", typeof(void), - new[] { typeof(SerializationInfo), typeof(StreamingContext) }); - var info = getObjectData.Arguments[0]; - - var typeLocal = getObjectData.CodeBuilder.DeclareLocal(typeof(Type)); - - getObjectData.CodeBuilder.AddStatement( - new AssignStatement( - typeLocal, - new MethodInvocationExpression( - null, - TypeMethods.StaticGetType, - new ConstReference(typeof(ProxyObjectReference).AssemblyQualifiedName).ToExpression(), - new ConstReference(1).ToExpression(), - new ConstReference(0).ToExpression()))); - - getObjectData.CodeBuilder.AddStatement( - new ExpressionStatement( - new MethodInvocationExpression( - info, - SerializationInfoMethods.SetType, - typeLocal.ToExpression()))); - - foreach (var field in emitter.GetAllFields()) - { - if (field.Reference.IsStatic) - { - continue; - } - if (field.Reference.IsNotSerialized) - { - continue; - } - AddAddValueInvocation(info, getObjectData, field); - } - - var interfacesLocal = getObjectData.CodeBuilder.DeclareLocal(typeof(string[])); - - getObjectData.CodeBuilder.AddStatement( - new AssignStatement( - interfacesLocal, - new NewArrayExpression(interfaces.Length, typeof(string)))); - - for (var i = 0; i < interfaces.Length; i++) - { - getObjectData.CodeBuilder.AddStatement( - new AssignArrayStatement( - interfacesLocal, - i, - new ConstReference(interfaces[i].AssemblyQualifiedName).ToExpression())); - } - - getObjectData.CodeBuilder.AddStatement( - new ExpressionStatement( - new MethodInvocationExpression( - info, - SerializationInfoMethods.AddValue_Object, - new ConstReference("__interfaces").ToExpression(), - interfacesLocal.ToExpression()))); - - getObjectData.CodeBuilder.AddStatement( - new ExpressionStatement( - new MethodInvocationExpression( - info, - SerializationInfoMethods.AddValue_Object, - new ConstReference("__baseType").ToExpression(), - new ConstReference(emitter.BaseType.AssemblyQualifiedName).ToExpression()))); - - getObjectData.CodeBuilder.AddStatement( - new ExpressionStatement( - new MethodInvocationExpression( - info, - SerializationInfoMethods.AddValue_Object, - new ConstReference("__proxyGenerationOptions").ToExpression(), - emitter.GetField("proxyGenerationOptions").ToExpression()))); - - getObjectData.CodeBuilder.AddStatement( - new ExpressionStatement( - new MethodInvocationExpression(info, - SerializationInfoMethods.AddValue_Object, - new ConstReference("__proxyTypeId").ToExpression(), - new ConstReference(proxyTypeId).ToExpression()))); - - CustomizeGetObjectData(getObjectData.CodeBuilder, info, getObjectData.Arguments[1], emitter); - - getObjectData.CodeBuilder.AddStatement(new ReturnStatement()); - } - - protected virtual void AddAddValueInvocation(ArgumentReference serializationInfo, MethodEmitter getObjectData, - FieldReference field) - { - getObjectData.CodeBuilder.AddStatement( - new ExpressionStatement( - new MethodInvocationExpression( - serializationInfo, - SerializationInfoMethods.AddValue_Object, - new ConstReference(field.Reference.Name).ToExpression(), - field.ToExpression()))); - return; - } - - protected abstract void CustomizeGetObjectData(AbstractCodeBuilder builder, ArgumentReference serializationInfo, - ArgumentReference streamingContext, ClassEmitter emitter); -#endif - public void CollectElementsToProxy(IProxyGenerationHook hook, MetaType model) { } diff --git a/src/Castle.Core/DynamicProxy/Generators/AttributesToAvoidReplicating.cs b/src/Castle.Core/DynamicProxy/Generators/AttributesToAvoidReplicating.cs index c262b7b0ec..c0eeae1143 100644 --- a/src/Castle.Core/DynamicProxy/Generators/AttributesToAvoidReplicating.cs +++ b/src/Castle.Core/DynamicProxy/Generators/AttributesToAvoidReplicating.cs @@ -29,6 +29,7 @@ static AttributesToAvoidReplicating() { attributes = new List() { + typeof(SerializableAttribute), typeof(System.Runtime.InteropServices.ComImportAttribute), typeof(System.Runtime.InteropServices.MarshalAsAttribute), typeof(System.Runtime.InteropServices.TypeIdentifierAttribute), diff --git a/src/Castle.Core/DynamicProxy/Generators/BaseProxyGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/BaseProxyGenerator.cs index 826b75605c..974f7c836a 100644 --- a/src/Castle.Core/DynamicProxy/Generators/BaseProxyGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/BaseProxyGenerator.cs @@ -19,10 +19,6 @@ namespace Castle.DynamicProxy.Generators using System.Diagnostics; using System.Linq; using System.Reflection; -#if FEATURE_SERIALIZATION - using System.Runtime.Serialization; - using System.Xml.Serialization; -#endif using Castle.Core.Logging; using Castle.DynamicProxy.Contributors; @@ -91,14 +87,6 @@ protected void AddMapping(Type @interface, ITypeContributor implementer, IDictio } } -#if FEATURE_SERIALIZATION - protected void AddMappingForISerializable(IDictionary typeImplementerMapping, - ITypeContributor instance) - { - AddMapping(typeof(ISerializable), instance, typeImplementerMapping); - } -#endif - /// /// It is safe to add mapping (no mapping for the interface exists) /// @@ -150,11 +138,7 @@ protected virtual void CreateFields(ClassEmitter emitter) protected void CreateInterceptorsField(ClassEmitter emitter) { - var interceptorsField = emitter.CreateField("__interceptors", typeof(IInterceptor[])); - -#if FEATURE_SERIALIZATION - emitter.DefineCustomAttributeFor(interceptorsField); -#endif + emitter.CreateField("__interceptors", typeof(IInterceptor[])); } protected FieldReference CreateOptionsField(ClassEmitter emitter) @@ -175,9 +159,6 @@ protected void CreateSelectorField(ClassEmitter emitter) protected virtual void CreateTypeAttributes(ClassEmitter emitter) { emitter.AddCustomAttributes(ProxyGenerationOptions); -#if FEATURE_SERIALIZATION - emitter.DefineCustomAttribute(new object[] { targetType }); -#endif } protected void EnsureOptionsOverrideEqualsAndGetHashCode(ProxyGenerationOptions options) @@ -286,7 +267,8 @@ protected void GenerateConstructors(ClassEmitter emitter, Type baseType, params /// Generates a parameters constructor that initializes the proxy /// state with just to make it non-null. /// - /// This constructor is important to allow proxies to be XML serializable + /// This constructor used to be important to allow proxies to be XML serializable, + /// but now that DynamicProxy no longer supports serialization, it may be obsolete. /// /// protected void GenerateParameterlessConstructor(ClassEmitter emitter, Type baseClass, FieldReference interceptorField) diff --git a/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs index 53b39f6b01..8499a81793 100644 --- a/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs @@ -168,12 +168,6 @@ protected virtual Type GenerateType(string name, Type[] interfaces, INamingScope } } // 4. plus special interfaces -#if FEATURE_SERIALIZATION - if (targetType.IsSerializable) - { - AddMappingForISerializable(typeImplementerMapping, proxyInstance); - } -#endif try { AddMappingNoCheck(typeof(IProxyTargetAccessor), proxyInstance, typeImplementerMapping); diff --git a/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs index 210e0f7986..4eb61bb858 100644 --- a/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs @@ -18,9 +18,6 @@ namespace Castle.DynamicProxy.Generators using System.Collections.Generic; using System.Linq; using System.Reflection; -#if FEATURE_SERIALIZATION - using System.Xml.Serialization; -#endif using Castle.DynamicProxy.Contributors; using Castle.DynamicProxy.Generators.Emitters; @@ -119,12 +116,6 @@ public Type GetGeneratedType() } } // 4. plus special interfaces -#if FEATURE_SERIALIZATION - if (targetType.IsSerializable) - { - AddMappingForISerializable(typeImplementerMapping, proxyInstance); - } -#endif try { AddMappingNoCheck(typeof(IProxyTargetAccessor), proxyInstance, typeImplementerMapping); @@ -146,11 +137,7 @@ public Type GetGeneratedType() private FieldReference CreateTargetField(ClassEmitter emitter) { - var targetField = emitter.CreateField("__target", targetType); -#if FEATURE_SERIALIZATION - emitter.DefineCustomAttributeFor(targetField); -#endif - return targetField; + return emitter.CreateField("__target", targetType); } private void EnsureDoesNotImplementIProxyTargetAccessor(Type type, string name) diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/ClassEmitter.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/ClassEmitter.cs index 075b32e9a5..100646b04b 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/ClassEmitter.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/ClassEmitter.cs @@ -25,7 +25,7 @@ namespace Castle.DynamicProxy.Generators.Emitters internal class ClassEmitter : AbstractTypeEmitter { internal const TypeAttributes DefaultAttributes = - TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Serializable; + TypeAttributes.Public | TypeAttributes.Class; private readonly ModuleScope moduleScope; diff --git a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs index e903c0d527..5d15071a94 100644 --- a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs @@ -18,9 +18,6 @@ namespace Castle.DynamicProxy.Generators using System.Collections.Generic; using System.Linq; using System.Reflection; -#if FEATURE_SERIALIZATION - using System.Xml.Serialization; -#endif using Castle.DynamicProxy.Contributors; using Castle.DynamicProxy.Generators.Emitters; @@ -91,14 +88,6 @@ public Type GenerateCode(Type proxyTargetType, Type[] interfaces, ProxyGeneratio return contributor; } -#if FEATURE_SERIALIZATION - protected override void CreateTypeAttributes(ClassEmitter emitter) - { - base.CreateTypeAttributes(emitter); - emitter.DefineCustomAttribute(); - } -#endif - protected virtual Type GenerateType(string typeName, Type proxyTargetType, Type[] interfaces, INamingScope namingScope) { IEnumerable contributors; @@ -218,9 +207,6 @@ protected virtual Type GenerateType(string typeName, Type proxyTargetType, Type[ // 4. plus special interfaces var instance = new InterfaceProxyInstanceContributor(targetType, GeneratorType, interfaces); -#if FEATURE_SERIALIZATION - AddMappingForISerializable(typeImplementerMapping, instance); -#endif try { AddMappingNoCheck(typeof(IProxyTargetAccessor), instance, typeImplementerMapping); @@ -258,9 +244,6 @@ private void CreateFields(ClassEmitter emitter, Type proxyTargetType) { base.CreateFields(emitter); targetField = emitter.CreateField("__target", proxyTargetType); -#if FEATURE_SERIALIZATION - emitter.DefineCustomAttributeFor(targetField); -#endif } private void EnsureValidBaseType(Type type) diff --git a/src/Castle.Core/DynamicProxy/Generators/InvocationTypeGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/InvocationTypeGenerator.cs index 2653afbc79..85fd65c000 100644 --- a/src/Castle.Core/DynamicProxy/Generators/InvocationTypeGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/InvocationTypeGenerator.cs @@ -80,10 +80,6 @@ public AbstractTypeEmitter Generate(ClassEmitter @class, ProxyGenerationOptions ImplemementInvokeMethodOnTarget(invocation, methodInfo.GetParameters(), targetField, callback); -#if FEATURE_SERIALIZATION - invocation.DefineCustomAttribute(); -#endif - return invocation; } diff --git a/src/Castle.Core/DynamicProxy/Generators/MethodWithInvocationGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/MethodWithInvocationGenerator.cs index 66a53517f3..1e33b93348 100644 --- a/src/Castle.Core/DynamicProxy/Generators/MethodWithInvocationGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/MethodWithInvocationGenerator.cs @@ -18,9 +18,6 @@ namespace Castle.DynamicProxy.Generators using System.Diagnostics; using System.Reflection; using System.Reflection.Emit; -#if FEATURE_SERIALIZATION - using System.Xml.Serialization; -#endif using Castle.Core.Internal; using Castle.DynamicProxy.Contributors; @@ -59,14 +56,10 @@ internal class MethodWithInvocationGenerator : MethodGenerator protected FieldReference BuildMethodInterceptorsField(ClassEmitter @class, MethodInfo method, INamingScope namingScope) { - var methodInterceptors = @class.CreateField( + return @class.CreateField( namingScope.GetUniqueName(string.Format("interceptors_{0}", method.Name)), typeof(IInterceptor[]), false); -#if FEATURE_SERIALIZATION - @class.DefineCustomAttributeFor(methodInterceptors); -#endif - return methodInterceptors; } protected override MethodEmitter BuildProxiedMethodBody(MethodEmitter emitter, ClassEmitter @class, ProxyGenerationOptions options, INamingScope namingScope) From f6f6130c372a63508f647725f3f72a8082e80dc0 Mon Sep 17 00:00:00 2001 From: stakx Date: Fri, 12 Jun 2020 18:50:42 +0200 Subject: [PATCH 02/10] Remove tests related to proxy serialization Those tests will now fail. Change two of them to document that proxy types are now no longer made serializable. --- .../BugsReported/DynProxy159.cs | 87 --- .../ClassOverridingEqualsAndGetHashCode.cs | 3 - .../Classes/ClassWithExplicitInterface.cs | 34 -- .../Classes/MySerializableClass.cs | 63 -- .../{SimpleClass.cs => SerializableClass.cs} | 7 +- .../SerializableExplicitImpl.cs | 6 +- .../DictionarySerializationTestCase.cs | 117 ---- .../InterClasses/ClassWithMarkerInterface.cs | 29 - .../SerializableClassTestCase.cs | 576 +----------------- .../DynamicProxy.Tests/Serialization/C.cs | 35 -- ...ClassWithDirectAndIndirectSelfReference.cs | 36 -- .../ClassWithIndirectSelfReference.cs | 34 -- .../Serialization/ComplexHolder.cs | 29 - .../Serialization/DelegateHolder.cs | 34 -- .../Serialization/EventHandlerClass.cs | 30 - .../Serialization/IMixedInterface.cs | 21 - .../Serialization/IndirectDelegateHolder.cs | 32 - .../Serialization/MethodFilterHook.cs | 48 -- .../SerializableInterceptorSelector.cs | 32 - .../Serialization/SerializableMixin.cs | 31 - .../XmlSerializationTestCase.cs | 46 -- 21 files changed, 16 insertions(+), 1314 deletions(-) delete mode 100644 src/Castle.Core.Tests/DynamicProxy.Tests/BugsReported/DynProxy159.cs delete mode 100644 src/Castle.Core.Tests/DynamicProxy.Tests/Classes/ClassWithExplicitInterface.cs delete mode 100644 src/Castle.Core.Tests/DynamicProxy.Tests/Classes/MySerializableClass.cs rename src/Castle.Core.Tests/DynamicProxy.Tests/Classes/{SimpleClass.cs => SerializableClass.cs} (91%) rename src/Castle.Core.Tests/DynamicProxy.Tests/{Serialization => Classes}/SerializableExplicitImpl.cs (91%) delete mode 100644 src/Castle.Core.Tests/DynamicProxy.Tests/DictionarySerializationTestCase.cs delete mode 100644 src/Castle.Core.Tests/DynamicProxy.Tests/InterClasses/ClassWithMarkerInterface.cs delete mode 100644 src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/C.cs delete mode 100644 src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/ClassWithDirectAndIndirectSelfReference.cs delete mode 100644 src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/ClassWithIndirectSelfReference.cs delete mode 100644 src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/ComplexHolder.cs delete mode 100644 src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/DelegateHolder.cs delete mode 100644 src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/EventHandlerClass.cs delete mode 100644 src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/IMixedInterface.cs delete mode 100644 src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/IndirectDelegateHolder.cs delete mode 100644 src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/MethodFilterHook.cs delete mode 100644 src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/SerializableInterceptorSelector.cs delete mode 100644 src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/SerializableMixin.cs delete mode 100644 src/Castle.Core.Tests/DynamicProxy.Tests/XmlSerializationTestCase.cs diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/BugsReported/DynProxy159.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/BugsReported/DynProxy159.cs deleted file mode 100644 index 9ca24d1bab..0000000000 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/BugsReported/DynProxy159.cs +++ /dev/null @@ -1,87 +0,0 @@ -// Copyright 2004-2011 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. - -#if FEATURE_SERIALIZATION - -namespace Castle.DynamicProxy.Tests.BugsReported -{ - using System; - using System.IO; - using System.Reflection; - using System.Runtime.Serialization; - using System.Runtime.Serialization.Formatters.Binary; - - using Castle.DynamicProxy.Tests; - - using NUnit.Framework; - - [TestFixture] - public class DynProxy159 : BasePEVerifyTestCase - { - // this test will only fail the first time it is run in a given VM - - private void FakeSerialize(object o) - { - using (var ms = new MemoryStream()) - { - var formatter = new BinaryFormatter(); - formatter.Serialize(ms, o); - } - } - - [Test] - public void ShouldNotChangeOrderOfSerializeableMembers() - { - var fromSystem = FormatterServices.GetSerializableMembers(typeof(MySerialClass)); - var beforeProxySerialization = new MemberInfo[fromSystem.Length]; - Array.Copy(fromSystem, beforeProxySerialization, fromSystem.Length); - fromSystem = null; - - FakeSerialize(generator.CreateClassProxy(typeof(MySerialClass))); - - fromSystem = FormatterServices.GetSerializableMembers(typeof(MySerialClass)); - - CollectionAssert.AreEquivalent(beforeProxySerialization, fromSystem); - } - - [Test] - public void ShouldSerializedMixofProxiedAndUnproxiedInstances() - { - var o = new[] - { - new MySerialClass(), - (MySerialClass)generator.CreateClassProxy(typeof(MySerialClass)), - new MySerialClass(), - }; - - o[2].yyy = 3.1415; - o[2].zzz = 100; - - FakeSerialize(o); - - Assert.AreEqual(3.1415, o[2].yyy); - Assert.AreEqual(100, o[2].zzz); - } - } - - [Serializable] - public class MySerialClass - { - public string xxx { get; set; } - public double? yyy { get; set; } - public int? zzz { get; set; } - } -} - -#endif \ No newline at end of file diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/ClassOverridingEqualsAndGetHashCode.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/ClassOverridingEqualsAndGetHashCode.cs index 0e57bab5f8..3aea5e1402 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/ClassOverridingEqualsAndGetHashCode.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/ClassOverridingEqualsAndGetHashCode.cs @@ -16,9 +16,6 @@ namespace Castle.DynamicProxy.Tests.Classes { using System; -#if FEATURE_SERIALIZATION - [Serializable] -#endif public class ClassOverridingEqualsAndGetHashCode { private Guid _id; diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/ClassWithExplicitInterface.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/ClassWithExplicitInterface.cs deleted file mode 100644 index 5be83a19e1..0000000000 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/ClassWithExplicitInterface.cs +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright 2004-2010 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.Tests.Classes -{ - using System; - -#if FEATURE_SERIALIZATION - [Serializable] -#endif - public class ClassWithExplicitInterface : ISimpleInterface - { - int ISimpleInterface.Do() - { - return 5; - } - - public virtual int DoVirtual() - { - return 7; - } - } -} \ No newline at end of file diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/MySerializableClass.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/MySerializableClass.cs deleted file mode 100644 index 02e3ef7dfe..0000000000 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/MySerializableClass.cs +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright 2004-2010 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.Tests.Classes -{ - using System; -#if FEATURE_SERIALIZATION - using System.Runtime.Serialization; -#endif -#if FEATURE_SERIALIZATION - [Serializable] -#endif - public class MySerializableClass - { - protected DateTime current; - - public MySerializableClass() - { - current = DateTime.Now; - } - - public virtual DateTime Current - { - get { return current; } - } - - public virtual double CalculateSumDistanceNow() - { - return Math.PI; - } - } - -#if FEATURE_SERIALIZATION - [Serializable] - public class MySerializableClass2 : MySerializableClass, ISerializable - { - public MySerializableClass2() - { - } - - public MySerializableClass2(SerializationInfo info, StreamingContext context) - { - current = (DateTime) info.GetValue("dt", typeof (DateTime)); - } - - public virtual void GetObjectData(SerializationInfo info, StreamingContext context) - { - info.AddValue("dt", current); - } - } -#endif -} diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/SimpleClass.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/SerializableClass.cs similarity index 91% rename from src/Castle.Core.Tests/DynamicProxy.Tests/Classes/SimpleClass.cs rename to src/Castle.Core.Tests/DynamicProxy.Tests/Classes/SerializableClass.cs index d45e85ae06..ee11640447 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/SimpleClass.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/SerializableClass.cs @@ -14,7 +14,10 @@ namespace Castle.DynamicProxy.Tests.Classes { - public class SimpleClass + using System; + + [Serializable] + public class SerializableClass { } -} \ No newline at end of file +} diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/SerializableExplicitImpl.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/SerializableExplicitImpl.cs similarity index 91% rename from src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/SerializableExplicitImpl.cs rename to src/Castle.Core.Tests/DynamicProxy.Tests/Classes/SerializableExplicitImpl.cs index 26b001f971..e10973b4eb 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/SerializableExplicitImpl.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/SerializableExplicitImpl.cs @@ -12,9 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#if FEATURE_SERIALIZATION - -namespace Castle.DynamicProxy.Tests.Serialization +namespace Castle.DynamicProxy.Tests.Classes { using System; using System.Runtime.Serialization; @@ -27,5 +25,3 @@ void ISerializable.GetObjectData(SerializationInfo info, StreamingContext contex } } } - -#endif \ No newline at end of file diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/DictionarySerializationTestCase.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/DictionarySerializationTestCase.cs deleted file mode 100644 index bd2e6bdb19..0000000000 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/DictionarySerializationTestCase.cs +++ /dev/null @@ -1,117 +0,0 @@ -// Copyright 2004-2010 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. - -#if FEATURE_SERIALIZATION - -namespace Castle.DynamicProxy.Tests -{ - using System; - using System.Collections.Generic; - using System.IO; - using System.Runtime.Serialization.Formatters.Binary; - - using Castle.DynamicProxy.Tests.Classes; - - using NUnit.Framework; - - [TestFixture] - public class DictionarySerializationTestCase - { - [Test] - public void NullReferenceProxyDeserializationTest() - { - ProxyGenerator generator = new ProxyGenerator(); - Dictionary theInstances = - new Dictionary(); - ClassOverridingEqualsAndGetHashCode c = - (ClassOverridingEqualsAndGetHashCode)generator.CreateClassProxy(typeof(ClassOverridingEqualsAndGetHashCode)); - c.Id = Guid.NewGuid(); - c.Name = DateTime.Now.ToString("yyyyMMddHHmmss"); - theInstances.Add(c, c.Name); - Dictionary theInstancesBis = - SerializeAndDeserialize>(theInstances); - - Assert.IsNotNull(theInstancesBis); - Assert.AreEqual(theInstances.Count, theInstancesBis.Count); - } - - [Test] - public void DictionaryDeserializationWithoutProxyTest() - { - Dictionary theInstances = - new Dictionary(); - - for (int i = 0; i < 50; i++) - { - ClassOverridingEqualsAndGetHashCode c = new ClassOverridingEqualsAndGetHashCode(); - c.Id = Guid.NewGuid(); - c.Name = DateTime.Now.ToString("yyyyMMddHHmmss"); - theInstances.Add(c, c.Name); - } - -#pragma warning disable 219 - Dictionary theInstancesBis = - SerializeAndDeserialize>(theInstances); -#pragma warning restore 219 - } - - [Test] - public void DictionaryDeserializationWithProxyTest() - { - ProxyGenerator generator = new ProxyGenerator(); - Dictionary theInstances = - new Dictionary(); - - for (int i = 0; i < 50; i++) - { - ClassOverridingEqualsAndGetHashCode c = - (ClassOverridingEqualsAndGetHashCode)generator.CreateClassProxy(typeof(ClassOverridingEqualsAndGetHashCode)); - c.Id = Guid.NewGuid(); - c.Name = DateTime.Now.ToString("yyyyMMddHHmmss"); - theInstances.Add(c, c.Name); - } - -#pragma warning disable 219 - Dictionary theInstancesBis = - SerializeAndDeserialize>(theInstances); -#pragma warning restore 219 - } - - [Test] - public void BasicSerializationProxyTest() - { - ProxyGenerator generator = new ProxyGenerator(); - ClassOverridingEqualsAndGetHashCode c = - (ClassOverridingEqualsAndGetHashCode)generator.CreateClassProxy(typeof(ClassOverridingEqualsAndGetHashCode)); - c.Id = Guid.NewGuid(); - c.Name = DateTime.Now.ToString("yyyyMMddHHmmss"); - - ClassOverridingEqualsAndGetHashCode c2 = SerializeAndDeserialize(c); - Assert.IsNotNull(c2); - Assert.AreEqual(c.Id, c2.Id); - Assert.AreEqual(c.Name, c2.Name); - } - - public static T SerializeAndDeserialize(T proxy) - { - MemoryStream stream = new MemoryStream(); - BinaryFormatter formatter = new BinaryFormatter(); - formatter.Serialize(stream, proxy); - stream.Position = 0; - return (T) formatter.Deserialize(stream); - } - } -} - -#endif \ No newline at end of file diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/InterClasses/ClassWithMarkerInterface.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/InterClasses/ClassWithMarkerInterface.cs deleted file mode 100644 index 0ef3e40b35..0000000000 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/InterClasses/ClassWithMarkerInterface.cs +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright 2004-2010 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.Tests.Classes -{ - using System; - - public interface IMarkerInterface - { - } - -#if FEATURE_SERIALIZATION - [Serializable] -#endif - public class ClassWithMarkerInterface - { - } -} diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/SerializableClassTestCase.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/SerializableClassTestCase.cs index cdab0d9813..5e677ba231 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/SerializableClassTestCase.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/SerializableClassTestCase.cs @@ -12,40 +12,20 @@ // See the License for the specific language governing permissions and // limitations under the License. -#if FEATURE_SERIALIZATION - namespace Castle.DynamicProxy.Tests { using System; - using System.Collections; - using System.IO; using System.Reflection; using System.Runtime.Serialization; - using System.Runtime.Serialization.Formatters.Binary; - using Castle.DynamicProxy.Serialization; using Castle.DynamicProxy.Tests.Classes; - using Castle.DynamicProxy.Tests.BugsReported; using Castle.DynamicProxy.Tests.InterClasses; - using Castle.DynamicProxy.Tests.Serialization; using NUnit.Framework; [TestFixture] public class SerializableClassTestCase : BasePEVerifyTestCase { - [Test] - public void BaseTypeForInterfaceProxy_is_honored_after_deserialization() - { - var options = new ProxyGenerationOptions - { - BaseTypeForInterfaceProxy = typeof(SimpleClass) - }; - var proxy = generator.CreateInterfaceProxyWithoutTarget(typeof(IService), Type.EmptyTypes, options); - var newProxy = SerializeAndDeserialize(proxy); - Assert.AreEqual(typeof(SimpleClass), newProxy.GetType().BaseType); - } - [Test(Description = "DYNPROXY-133")] public void Can_proxy_class_with_explicit_GetObjectData() { @@ -53,561 +33,25 @@ public void Can_proxy_class_with_explicit_GetObjectData() } [Test] - public void ClassProxyWithTargetSerialization() - { - var proxy = generator.CreateClassProxyWithTarget(new MySerializableClass(), new StandardInterceptor()); - - var current = proxy.Current; - - var otherProxy = SerializeAndDeserialize(proxy); - - Assert.AreEqual(current, otherProxy.Current); - } - - [Test] - public void CreateSerializable() - { - var proxy = (MySerializableClass) - generator.CreateClassProxy(typeof(MySerializableClass), new StandardInterceptor()); - - Assert.IsTrue(proxy.GetType().IsSerializable); - } - - [Test] - public void CustomMarkerInterface() - { - var proxy = generator.CreateClassProxy(typeof(ClassWithMarkerInterface), - new[] { typeof(IMarkerInterface) }, - new StandardInterceptor()); - - Assert.IsNotNull(proxy); - Assert.IsTrue(proxy is IMarkerInterface); - - var otherProxy = SerializeAndDeserialize(proxy); - - Assert.IsTrue(otherProxy is IMarkerInterface); - } - - [Test] - public void DeserializationWithSpecificModuleScope() - { - ProxyObjectReference.SetScope(generator.ProxyBuilder.ModuleScope); - var first = generator.CreateClassProxy(new StandardInterceptor()); - var second = SerializeAndDeserialize(first); - Assert.AreSame(first.GetType(), second.GetType()); - } - - [Test] - public void HashtableSerialization() - { - var proxy = generator.CreateClassProxy( - typeof(Hashtable), new StandardInterceptor()); - - Assert.IsTrue(typeof(Hashtable).IsAssignableFrom(proxy.GetType())); - - (proxy as Hashtable).Add("key", "helloooo!"); - - var otherProxy = (Hashtable)SerializeAndDeserialize(proxy); - - Assert.IsTrue(otherProxy.ContainsKey("key")); - Assert.AreEqual("helloooo!", otherProxy["key"]); - } - - [Test] - public void ImplementsISerializable() - { - var proxy = (MySerializableClass) - generator.CreateClassProxy(typeof(MySerializableClass), new StandardInterceptor()); - - Assert.IsTrue(proxy is ISerializable); - } - - public override void Init() - { - base.Init(); - ProxyObjectReference.ResetScope(); - } - - [Test] - public void MixinFieldsSetOnDeserialization_ClassProxy() - { - var options = new ProxyGenerationOptions(); - options.AddMixinInstance(new SerializableMixin()); - - var proxy = (MySerializableClass)generator.CreateClassProxy( - typeof(MySerializableClass), - new Type[0], - options, - new StandardInterceptor()); - - Assert.IsTrue(proxy is IMixedInterface); - Assert.IsNotNull(((IMixedInterface)proxy).GetExecutingObject()); - Assert.IsTrue(((IMixedInterface)proxy).GetExecutingObject() is SerializableMixin); - - var otherProxy = SerializeAndDeserialize(proxy); - Assert.IsTrue(otherProxy is IMixedInterface); - Assert.IsNotNull(((IMixedInterface)otherProxy).GetExecutingObject()); - Assert.IsTrue(((IMixedInterface)otherProxy).GetExecutingObject() is SerializableMixin); - } - - [Test] - public void MixinFieldsSetOnDeserialization_InterfaceProxy_WithTarget() - { - var options = new ProxyGenerationOptions(); - options.AddMixinInstance(new SerializableMixin()); - - var proxy = (IService)generator.CreateInterfaceProxyWithTarget( - typeof(IService), - new ServiceImpl(), - options, - new StandardInterceptor()); - - Assert.IsTrue(proxy is IMixedInterface); - Assert.IsNotNull(((IMixedInterface)proxy).GetExecutingObject()); - Assert.IsTrue(((IMixedInterface)proxy).GetExecutingObject() is SerializableMixin); - - var otherProxy = SerializeAndDeserialize(proxy); - Assert.IsTrue(otherProxy is IMixedInterface); - Assert.IsNotNull(((IMixedInterface)otherProxy).GetExecutingObject()); - Assert.IsTrue(((IMixedInterface)otherProxy).GetExecutingObject() is SerializableMixin); - } - - [Test] - public void MixinFieldsSetOnDeserialization_InterfaceProxy_WithTargetInterface() - { - var options = new ProxyGenerationOptions(); - options.AddMixinInstance(new SerializableMixin()); - - var proxy = (IService)generator.CreateInterfaceProxyWithTargetInterface( - typeof(IService), - new ServiceImpl(), - options, - new StandardInterceptor()); - - Assert.IsTrue(proxy is IMixedInterface); - Assert.IsNotNull(((IMixedInterface)proxy).GetExecutingObject()); - Assert.IsTrue(((IMixedInterface)proxy).GetExecutingObject() is SerializableMixin); - - var otherProxy = SerializeAndDeserialize(proxy); - Assert.IsTrue(otherProxy is IMixedInterface); - Assert.IsNotNull(((IMixedInterface)otherProxy).GetExecutingObject()); - Assert.IsTrue(((IMixedInterface)otherProxy).GetExecutingObject() is SerializableMixin); - } - - [Test] - public void MixinFieldsSetOnDeserialization_InterfaceProxy_WithoutTarget() - { - var options = new ProxyGenerationOptions(); - options.AddMixinInstance(new SerializableMixin()); - - var proxy = (IService)generator.CreateInterfaceProxyWithoutTarget( - typeof(IService), - new Type[0], - options, - new StandardInterceptor()); - - Assert.IsTrue(proxy is IMixedInterface); - Assert.IsNotNull(((IMixedInterface)proxy).GetExecutingObject()); - Assert.IsTrue(((IMixedInterface)proxy).GetExecutingObject() is SerializableMixin); - - var otherProxy = SerializeAndDeserialize(proxy); - Assert.IsTrue(otherProxy is IMixedInterface); - Assert.IsNotNull(((IMixedInterface)otherProxy).GetExecutingObject()); - Assert.IsTrue(((IMixedInterface)otherProxy).GetExecutingObject() is SerializableMixin); - } - - [Test] - public void MixinsAppliedOnDeserialization() - { - var options = new ProxyGenerationOptions(); - options.AddMixinInstance(new SerializableMixin()); - - var proxy = (MySerializableClass)generator.CreateClassProxy( - typeof(MySerializableClass), - new Type[0], - options, - new StandardInterceptor()); - - Assert.IsTrue(proxy is IMixedInterface); - - var otherProxy = SerializeAndDeserialize(proxy); - Assert.IsTrue(otherProxy is IMixedInterface); - } - - [Test] - public void ProxyGenerationOptionsRespectedOnDeserialization() - { - var hook = new MethodFilterHook("(get_Current)|(GetExecutingObject)"); - var options = new ProxyGenerationOptions(hook); - options.AddMixinInstance(new SerializableMixin()); - options.Selector = new SerializableInterceptorSelector(); - - var proxy = (MySerializableClass)generator.CreateClassProxy( - typeof(MySerializableClass), - new Type[0], - options, - new StandardInterceptor()); - - Assert.AreEqual(proxy.GetType(), proxy.GetType().GetMethod("get_Current").DeclaringType); - Assert.AreNotEqual(proxy.GetType(), proxy.GetType().GetMethod("CalculateSumDistanceNow").DeclaringType); - Assert.AreEqual(proxy.GetType().BaseType, proxy.GetType().GetMethod("CalculateSumDistanceNow").DeclaringType); - var options2 = (ProxyGenerationOptions)proxy.GetType(). - GetField("proxyGenerationOptions", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null); - Assert.IsNotNull(Array.Find(options2.MixinsAsArray(), delegate(object o) { return o is SerializableMixin; })); - Assert.IsNotNull(options2.Selector); - - var otherProxy = SerializeAndDeserialize(proxy); - Assert.AreEqual(otherProxy.GetType(), otherProxy.GetType().GetMethod("get_Current").DeclaringType); - Assert.AreNotEqual(otherProxy.GetType(), otherProxy.GetType().GetMethod("CalculateSumDistanceNow").DeclaringType); - Assert.AreEqual(otherProxy.GetType().BaseType, - otherProxy.GetType().GetMethod("CalculateSumDistanceNow").DeclaringType); - options2 = (ProxyGenerationOptions)otherProxy.GetType(). - GetField("proxyGenerationOptions", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null); - Assert.IsNotNull(Array.Find(options2.MixinsAsArray(), delegate(object o) { return o is SerializableMixin; })); - Assert.IsNotNull(options2.Selector); - } - - [Test] - public void ProxyGenerationOptionsRespectedOnDeserializationComplex() - { - var hook = new MethodFilterHook("(get_Current)|(GetExecutingObject)"); - var options = new ProxyGenerationOptions(hook); - options.AddMixinInstance(new SerializableMixin()); - options.Selector = new SerializableInterceptorSelector(); - - var holder = new ComplexHolder(); - holder.Type = typeof(MySerializableClass); - holder.Element = generator.CreateClassProxy(typeof(MySerializableClass), new Type[0], options, - new StandardInterceptor()); - - // check holder elements - Assert.AreEqual(typeof(MySerializableClass), holder.Type); - Assert.IsNotNull(holder.Element); - Assert.IsTrue(holder.Element is MySerializableClass); - Assert.AreNotEqual(typeof(MySerializableClass), holder.Element.GetType()); - - // check whether options were applied correctly - Assert.AreEqual(holder.Element.GetType(), holder.Element.GetType().GetMethod("get_Current").DeclaringType); - Assert.AreNotEqual(holder.Element.GetType(), - holder.Element.GetType().GetMethod("CalculateSumDistanceNow").DeclaringType); - Assert.AreEqual(holder.Element.GetType().BaseType, - holder.Element.GetType().GetMethod("CalculateSumDistanceNow").DeclaringType); - var options2 = (ProxyGenerationOptions)holder.Element.GetType(). - GetField("proxyGenerationOptions", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null); - Assert.IsNotNull(Array.Find(options2.MixinsAsArray(), delegate(object o) { return o is SerializableMixin; })); - Assert.IsNotNull(options2.Selector); - - var otherHolder = SerializeAndDeserialize(holder); - - // check holder elements - Assert.AreEqual(typeof(MySerializableClass), otherHolder.Type); - Assert.IsNotNull(otherHolder.Element); - Assert.IsTrue(otherHolder.Element is MySerializableClass); - Assert.AreNotEqual(typeof(MySerializableClass), otherHolder.Element.GetType()); - - // check whether options were applied correctly - Assert.AreEqual(otherHolder.Element.GetType(), otherHolder.Element.GetType().GetMethod("get_Current").DeclaringType); - Assert.AreNotEqual(otherHolder.Element.GetType(), - otherHolder.Element.GetType().GetMethod("CalculateSumDistanceNow").DeclaringType); - Assert.AreEqual(otherHolder.Element.GetType().BaseType, - otherHolder.Element.GetType().GetMethod("CalculateSumDistanceNow").DeclaringType); - options2 = (ProxyGenerationOptions)otherHolder.Element.GetType(). - GetField("proxyGenerationOptions", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null); - Assert.IsNotNull(Array.Find(options2.MixinsAsArray(), delegate(object o) { return o is SerializableMixin; })); - Assert.IsNotNull(options2.Selector); - } - - [Test] - public void ProxyKnowsItsGenerationOptions() + public void Proxy_class_is_not_serializable_even_when_proxied_class_is() { - var hook = new MethodFilterHook(".*"); - var options = new ProxyGenerationOptions(hook); - options.AddMixinInstance(new SerializableMixin()); + // DynamicProxy 4 used to mark proxy classes as serializable. + // This test is here to explicitly document that this no longer happens. - var proxy = generator.CreateClassProxy( - typeof(MySerializableClass), - new Type[0], - options, - new StandardInterceptor()); + var proxy = generator.CreateClassProxy(new StandardInterceptor()); - var field = proxy.GetType().GetField("proxyGenerationOptions", BindingFlags.Static | BindingFlags.NonPublic); - Assert.IsNotNull(field); - Assert.AreSame(options, field.GetValue(proxy)); - - base.Init(); - - proxy = generator.CreateInterfaceProxyWithoutTarget(typeof(IService), new StandardInterceptor()); - field = proxy.GetType().GetField("proxyGenerationOptions", BindingFlags.Static | BindingFlags.NonPublic); - Assert.AreSame(ProxyGenerationOptions.Default, field.GetValue(proxy)); - - base.Init(); - - proxy = generator.CreateInterfaceProxyWithTarget(typeof(IService), new ServiceImpl(), options, - new StandardInterceptor()); - field = proxy.GetType().GetField("proxyGenerationOptions", BindingFlags.Static | BindingFlags.NonPublic); - Assert.AreSame(options, field.GetValue(proxy)); - - base.Init(); - - proxy = generator.CreateInterfaceProxyWithTargetInterface(typeof(IService), new ServiceImpl(), - new StandardInterceptor()); - field = proxy.GetType().GetField("proxyGenerationOptions", BindingFlags.Static | BindingFlags.NonPublic); - Assert.AreSame(ProxyGenerationOptions.Default, field.GetValue(proxy)); - } - - [Test] - public void ReusingModuleScopeFromProxyObjectReference() - { - var generatorWithSpecificModuleScope = - new ProxyGenerator(new DefaultProxyBuilder(ProxyObjectReference.ModuleScope)); - Assert.AreSame(generatorWithSpecificModuleScope.ProxyBuilder.ModuleScope, ProxyObjectReference.ModuleScope); - var first = - generatorWithSpecificModuleScope.CreateClassProxy(new StandardInterceptor()); - var second = SerializeAndDeserialize(first); - Assert.AreSame(first.GetType(), second.GetType()); - } - - [Test] - public void SerializatingObjectsWithoutDefaultConstructor() - { - var proxy = (C)generator.CreateClassProxy(typeof(C), new object[] { 1 }, new StandardInterceptor()); - var otherProxy = SerializeAndDeserialize(proxy); - - Assert.AreEqual(proxy.I, otherProxy.I); - Assert.AreSame(otherProxy, otherProxy.This); - } - - [Test] - public void SerializationDelegate() - { - var proxy = (MySerializableClass2) - generator.CreateClassProxy(typeof(MySerializableClass2), new StandardInterceptor()); - - var current = proxy.Current; - - var otherProxy = SerializeAndDeserialize(proxy); - - Assert.AreEqual(current, otherProxy.Current); - } - - [Test] - public void SerializeClassWithDirectAndIndirectSelfReference() - { - var proxy = - (ClassWithDirectAndIndirectSelfReference) - generator.CreateClassProxy(typeof(ClassWithDirectAndIndirectSelfReference), - new Type[0], new StandardInterceptor()); - Assert.AreSame(proxy, proxy.This); - - var otherProxy = - SerializeAndDeserialize(proxy); - Assert.AreSame(otherProxy, otherProxy.List[0]); - Assert.AreSame(otherProxy, otherProxy.This); - } - - [Test] - public void SerializeClassWithIndirectSelfReference() - { - var proxy = - (ClassWithIndirectSelfReference)generator.CreateClassProxy(typeof(ClassWithIndirectSelfReference), - new Type[0], new StandardInterceptor()); - Assert.AreSame(proxy, proxy.List[0]); - - var otherProxy = SerializeAndDeserialize(proxy); - Assert.AreSame(otherProxy, otherProxy.List[0]); - } - - [Test] - public void SerializeObjectsWithDelegateToOtherObject() - { - var eventHandlerInstance = new EventHandlerClass(); - var proxy = - (DelegateHolder)generator.CreateClassProxy(typeof(DelegateHolder), new IInterceptor[] { new StandardInterceptor() }); - - proxy.DelegateMember = new EventHandler(eventHandlerInstance.TestHandler); - proxy.ComplexTypeMember = new ArrayList(new[] { 1, 2, 3 }); - proxy.ComplexTypeMember.Add(eventHandlerInstance); - - Assert.IsNotNull(proxy.DelegateMember); - Assert.IsNotNull(proxy.DelegateMember.Target); - - Assert.IsNotNull(proxy.ComplexTypeMember); - Assert.AreEqual(4, proxy.ComplexTypeMember.Count); - Assert.AreEqual(1, proxy.ComplexTypeMember[0]); - Assert.AreEqual(2, proxy.ComplexTypeMember[1]); - Assert.AreEqual(3, proxy.ComplexTypeMember[2]); - Assert.AreSame(proxy.ComplexTypeMember[3], proxy.DelegateMember.Target); - - var otherProxy = (SerializeAndDeserialize(proxy)); - - Assert.IsNotNull(otherProxy.DelegateMember); - Assert.IsNotNull(otherProxy.DelegateMember.Target); - - Assert.IsNotNull(otherProxy.ComplexTypeMember); - Assert.AreEqual(4, otherProxy.ComplexTypeMember.Count); - Assert.AreEqual(1, otherProxy.ComplexTypeMember[0]); - Assert.AreEqual(2, otherProxy.ComplexTypeMember[1]); - Assert.AreEqual(3, otherProxy.ComplexTypeMember[2]); - Assert.AreSame(otherProxy.ComplexTypeMember[3], otherProxy.DelegateMember.Target); - } - - [Test] - public void SerializeObjectsWithDelegateToThisObject() - { - var proxy = - (DelegateHolder)generator.CreateClassProxy(typeof(DelegateHolder), new IInterceptor[] { new StandardInterceptor() }); - - proxy.DelegateMember = new EventHandler(proxy.TestHandler); - proxy.ComplexTypeMember = new ArrayList(new[] { 1, 2, 3 }); - - Assert.IsNotNull(proxy.DelegateMember); - Assert.AreSame(proxy, proxy.DelegateMember.Target); - - Assert.IsNotNull(proxy.ComplexTypeMember); - Assert.AreEqual(3, proxy.ComplexTypeMember.Count); - Assert.AreEqual(1, proxy.ComplexTypeMember[0]); - Assert.AreEqual(2, proxy.ComplexTypeMember[1]); - Assert.AreEqual(3, proxy.ComplexTypeMember[2]); - - var otherProxy = (SerializeAndDeserialize(proxy)); - - Assert.IsNotNull(otherProxy.DelegateMember); - Assert.AreSame(otherProxy, otherProxy.DelegateMember.Target); - - Assert.IsNotNull(otherProxy.ComplexTypeMember); - Assert.AreEqual(3, otherProxy.ComplexTypeMember.Count); - Assert.AreEqual(1, otherProxy.ComplexTypeMember[0]); - Assert.AreEqual(2, otherProxy.ComplexTypeMember[1]); - Assert.AreEqual(3, otherProxy.ComplexTypeMember[2]); - } - - [Test] - public void SerializeObjectsWithIndirectDelegateToMember() - { - var proxy = (IndirectDelegateHolder)generator.CreateClassProxy(typeof(IndirectDelegateHolder), - new IInterceptor[] { new StandardInterceptor() }); - - proxy.DelegateHolder.DelegateMember = new EventHandler(proxy.DelegateHolder.TestHandler); - proxy.DelegateHolder.ComplexTypeMember = new ArrayList(new[] { 1, 2, 3 }); - - Assert.IsNotNull(proxy.DelegateHolder.DelegateMember); - Assert.AreSame(proxy.DelegateHolder, proxy.DelegateHolder.DelegateMember.Target); - - Assert.IsNotNull(proxy.DelegateHolder.ComplexTypeMember); - Assert.AreEqual(3, proxy.DelegateHolder.ComplexTypeMember.Count); - Assert.AreEqual(1, proxy.DelegateHolder.ComplexTypeMember[0]); - Assert.AreEqual(2, proxy.DelegateHolder.ComplexTypeMember[1]); - Assert.AreEqual(3, proxy.DelegateHolder.ComplexTypeMember[2]); - - var otherProxy = (SerializeAndDeserialize(proxy)); - - Assert.IsNotNull(otherProxy.DelegateHolder.DelegateMember); - Assert.AreSame(otherProxy.DelegateHolder, otherProxy.DelegateHolder.DelegateMember.Target); - - Assert.IsNotNull(otherProxy.DelegateHolder.ComplexTypeMember); - Assert.AreEqual(3, otherProxy.DelegateHolder.ComplexTypeMember.Count); - Assert.AreEqual(1, otherProxy.DelegateHolder.ComplexTypeMember[0]); - Assert.AreEqual(2, otherProxy.DelegateHolder.ComplexTypeMember[1]); - Assert.AreEqual(3, otherProxy.DelegateHolder.ComplexTypeMember[2]); - } - - [Test] - public void SerializeObjectsWithIndirectDelegateToThisObject() - { - var proxy = (IndirectDelegateHolder)generator.CreateClassProxy(typeof(IndirectDelegateHolder), - new IInterceptor[] { new StandardInterceptor() }); - - proxy.DelegateHolder.DelegateMember = new EventHandler(proxy.TestHandler); - proxy.DelegateHolder.ComplexTypeMember = new ArrayList(new[] { 1, 2, 3 }); - - Assert.IsNotNull(proxy.DelegateHolder.DelegateMember); - Assert.AreSame(proxy, proxy.DelegateHolder.DelegateMember.Target); - - Assert.IsNotNull(proxy.DelegateHolder.ComplexTypeMember); - Assert.AreEqual(3, proxy.DelegateHolder.ComplexTypeMember.Count); - Assert.AreEqual(1, proxy.DelegateHolder.ComplexTypeMember[0]); - Assert.AreEqual(2, proxy.DelegateHolder.ComplexTypeMember[1]); - Assert.AreEqual(3, proxy.DelegateHolder.ComplexTypeMember[2]); - - var otherProxy = (SerializeAndDeserialize(proxy)); - - Assert.IsNotNull(otherProxy.DelegateHolder.DelegateMember); - Assert.AreSame(otherProxy, otherProxy.DelegateHolder.DelegateMember.Target); - - Assert.IsNotNull(otherProxy.DelegateHolder.ComplexTypeMember); - Assert.AreEqual(3, otherProxy.DelegateHolder.ComplexTypeMember.Count); - Assert.AreEqual(1, otherProxy.DelegateHolder.ComplexTypeMember[0]); - Assert.AreEqual(2, otherProxy.DelegateHolder.ComplexTypeMember[1]); - Assert.AreEqual(3, otherProxy.DelegateHolder.ComplexTypeMember[2]); + Assert.IsFalse(proxy.GetType().IsSerializable); } [Test] - public void SimpleInterfaceProxy() + public void Proxy_class_does_not_implement_ISerializable() { - var proxy = - generator.CreateInterfaceProxyWithTarget(typeof(IMyInterface2), new MyInterfaceImpl(), new StandardInterceptor()); + // DynamicProxy 4 used to auto-implement `ISerializable`. + // This test is here to explicitly document that this no longer happens. - Assert.IsTrue(proxy.GetType().IsSerializable); + var proxy = generator.CreateClassProxy(new StandardInterceptor()); - var inter = (IMyInterface2)proxy; - - inter.Name = "opa"; - Assert.AreEqual("opa", inter.Name); - inter.Started = true; - Assert.AreEqual(true, inter.Started); - - var otherProxy = (IMyInterface2)SerializeAndDeserialize(proxy); - - Assert.AreEqual(inter.Name, otherProxy.Name); - Assert.AreEqual(inter.Started, otherProxy.Started); - } - - [Test] - public void SimpleInterfaceProxy_WithoutTarget() - { - var proxy = - generator.CreateInterfaceProxyWithoutTarget(typeof(IMyInterface2), new[] { typeof(IMyInterface) }, - new StandardInterceptor()); - - Assert.IsTrue(proxy is IMyInterface2); - Assert.IsTrue(proxy is IMyInterface); - - var otherProxy = SerializeAndDeserialize(proxy); - - Assert.IsTrue(otherProxy is IMyInterface2); - Assert.IsTrue(otherProxy is IMyInterface); - } - - [Test] - public void SimpleProxySerialization() - { - var proxy = (MySerializableClass) - generator.CreateClassProxy(typeof(MySerializableClass), new StandardInterceptor()); - - var current = proxy.Current; - - var otherProxy = SerializeAndDeserialize(proxy); - - Assert.AreEqual(current, otherProxy.Current); - } - - public override void TearDown() - { - base.TearDown(); - ProxyObjectReference.ResetScope(); - } - - public static T SerializeAndDeserialize(T proxy) - { - using (var stream = new MemoryStream()) - { - var formatter = new BinaryFormatter(); - formatter.Serialize(stream, proxy); - stream.Position = 0; - return (T)formatter.Deserialize(stream); - } + Assert.IsFalse(proxy is ISerializable); } } } - -#endif \ No newline at end of file diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/C.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/C.cs deleted file mode 100644 index 8c5cef69b8..0000000000 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/C.cs +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright 2004-2010 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. - -#if FEATURE_SERIALIZATION - -namespace Castle.DynamicProxy.Tests.Serialization -{ - using System; - - [Serializable] - public class C - { - public int I; - public C This; - - public C(int i) - { - I = i; - This = this; - } - } -} - -#endif \ No newline at end of file diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/ClassWithDirectAndIndirectSelfReference.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/ClassWithDirectAndIndirectSelfReference.cs deleted file mode 100644 index 14f4d76386..0000000000 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/ClassWithDirectAndIndirectSelfReference.cs +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright 2004-2010 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. - -#if FEATURE_SERIALIZATION - -namespace Castle.DynamicProxy.Tests.Serialization -{ - using System; - using System.Collections; - - [Serializable] - public class ClassWithDirectAndIndirectSelfReference - { - public ArrayList List = new ArrayList(); - public ClassWithDirectAndIndirectSelfReference This; - - public ClassWithDirectAndIndirectSelfReference() - { - This = this; - List.Add(this); - } - } -} - -#endif \ No newline at end of file diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/ClassWithIndirectSelfReference.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/ClassWithIndirectSelfReference.cs deleted file mode 100644 index f9af56c6ba..0000000000 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/ClassWithIndirectSelfReference.cs +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright 2004-2010 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. - -#if FEATURE_SERIALIZATION - -namespace Castle.DynamicProxy.Tests.Serialization -{ - using System; - using System.Collections; - - [Serializable] - public class ClassWithIndirectSelfReference - { - public ArrayList List = new ArrayList(); - - public ClassWithIndirectSelfReference() - { - List.Add(this); - } - } -} - -#endif \ No newline at end of file diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/ComplexHolder.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/ComplexHolder.cs deleted file mode 100644 index 19c64b4861..0000000000 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/ComplexHolder.cs +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright 2004-2010 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. - -#if FEATURE_SERIALIZATION - -namespace Castle.DynamicProxy.Tests.Serialization -{ - using System; - - [Serializable] - public class ComplexHolder - { - public object Element; - public Type Type; - } -} - -#endif \ No newline at end of file diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/DelegateHolder.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/DelegateHolder.cs deleted file mode 100644 index 10985b9eee..0000000000 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/DelegateHolder.cs +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright 2004-2010 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. - -#if FEATURE_SERIALIZATION - -namespace Castle.DynamicProxy.Tests.Serialization -{ - using System; - using System.Collections; - - [Serializable] - public class DelegateHolder - { - public ArrayList ComplexTypeMember; - public EventHandler DelegateMember; - - public void TestHandler(object sender, EventArgs e) - { - } - } -} - -#endif \ No newline at end of file diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/EventHandlerClass.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/EventHandlerClass.cs deleted file mode 100644 index 0bbd1477ed..0000000000 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/EventHandlerClass.cs +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2004-2010 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. - -#if FEATURE_SERIALIZATION - -namespace Castle.DynamicProxy.Tests.Serialization -{ - using System; - - [Serializable] - public class EventHandlerClass - { - public void TestHandler(object sender, EventArgs e) - { - } - } -} - -#endif \ No newline at end of file diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/IMixedInterface.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/IMixedInterface.cs deleted file mode 100644 index 0eac97c9c9..0000000000 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/IMixedInterface.cs +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright 2004-2010 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.Tests.Serialization -{ - public interface IMixedInterface - { - object GetExecutingObject(); - } -} \ No newline at end of file diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/IndirectDelegateHolder.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/IndirectDelegateHolder.cs deleted file mode 100644 index b8fdbf51cf..0000000000 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/IndirectDelegateHolder.cs +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright 2004-2010 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. - -#if FEATURE_SERIALIZATION - -namespace Castle.DynamicProxy.Tests.Serialization -{ - using System; - - [Serializable] - public class IndirectDelegateHolder - { - public DelegateHolder DelegateHolder = new DelegateHolder(); - - public void TestHandler(object sender, EventArgs e) - { - } - } -} - -#endif \ No newline at end of file diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/MethodFilterHook.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/MethodFilterHook.cs deleted file mode 100644 index f983594214..0000000000 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/MethodFilterHook.cs +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright 2004-2010 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. - -#if FEATURE_SERIALIZATION - -namespace Castle.DynamicProxy.Tests.Serialization -{ - using System; - using System.Reflection; - using System.Text.RegularExpressions; - - [Serializable] - public class MethodFilterHook : IProxyGenerationHook - { - private string nameFilter; - - public MethodFilterHook(string nameFilter) - { - this.nameFilter = nameFilter; - } - - public void MethodsInspected() - { - } - - public void NonProxyableMemberNotification(Type type, MemberInfo memberInfo) - { - } - - public bool ShouldInterceptMethod(Type type, MethodInfo memberInfo) - { - return Regex.IsMatch(memberInfo.Name, nameFilter); - } - } -} - -#endif \ No newline at end of file diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/SerializableInterceptorSelector.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/SerializableInterceptorSelector.cs deleted file mode 100644 index 3f42df411b..0000000000 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/SerializableInterceptorSelector.cs +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright 2004-2010 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. - -#if FEATURE_SERIALIZATION - -namespace Castle.DynamicProxy.Tests.Serialization -{ - using System; - using System.Reflection; - - [Serializable] - public class SerializableInterceptorSelector : IInterceptorSelector - { - public IInterceptor[] SelectInterceptors(Type type, MethodInfo method, IInterceptor[] interceptors) - { - return interceptors; - } - } -} - -#endif \ No newline at end of file diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/SerializableMixin.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/SerializableMixin.cs deleted file mode 100644 index d0208a3117..0000000000 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/Serialization/SerializableMixin.cs +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright 2004-2010 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. - -#if FEATURE_SERIALIZATION - -namespace Castle.DynamicProxy.Tests.Serialization -{ - using System; - - [Serializable] - public class SerializableMixin : IMixedInterface - { - public object GetExecutingObject() - { - return this; - } - } -} - -#endif \ No newline at end of file diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/XmlSerializationTestCase.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/XmlSerializationTestCase.cs deleted file mode 100644 index de62d3bb71..0000000000 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/XmlSerializationTestCase.cs +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright 2004-2010 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.Tests -{ - using System.IO; - using System.Xml.Serialization; - - using Castle.DynamicProxy.Tests.Classes; - using NUnit.Framework; - - [TestFixture] - public class XmlSerializationTestCase : BasePEVerifyTestCase - { - [Test, Ignore("Could not come up with a solution for this")] - public void ProxyIsXmlSerializable() - { - ClassToSerialize proxy = (ClassToSerialize) - generator.CreateClassProxy(typeof (ClassToSerialize), new StandardInterceptor()); - - XmlSerializer serializer = new XmlSerializer(proxy.GetType()); - - StringWriter writer = new StringWriter(); - - serializer.Serialize(writer, proxy); - - StringReader reader = new StringReader(writer.GetStringBuilder().ToString()); - - object newObj = serializer.Deserialize(reader); - - Assert.IsNotNull(newObj); - Assert.IsInstanceOf(typeof (ClassToSerialize), newObj); - } - } -} From 50620fd0fdc4268b444591ecc98215585f980718 Mon Sep 17 00:00:00 2001 From: stakx Date: Fri, 12 Jun 2020 18:58:31 +0200 Subject: [PATCH 03/10] Remove library code that has become redundant --- .../DynamicProxy.Tests/ModuleScopeTestCase.cs | 2 + .../ClassProxyInstanceContributor.cs | 5 +- ...ClassProxyWithTargetInstanceContributor.cs | 4 +- .../InterfaceProxyInstanceContributor.cs | 4 +- .../Contributors/ProxyInstanceContributor.cs | 4 +- .../Generators/ClassProxyGenerator.cs | 3 +- .../ClassProxyWithTargetGenerator.cs | 4 +- .../InterfaceProxyWithTargetGenerator.cs | 8 +- ...erfaceProxyWithTargetInterfaceGenerator.cs | 6 - .../InterfaceProxyWithoutTargetGenerator.cs | 6 - .../DynamicProxy/Internal/TypeUtil.cs | 8 - .../Serialization/ProxyObjectReference.cs | 294 ------------------ .../Serialization/ProxyTypeConstants.cs | 25 -- .../Tokens/FormatterServicesMethods.cs | 33 -- .../Tokens/SerializationInfoMethods.cs | 60 ---- .../DynamicProxy/Tokens/TypeUtilMethods.cs | 1 - 16 files changed, 12 insertions(+), 455 deletions(-) delete mode 100644 src/Castle.Core/DynamicProxy/Serialization/ProxyObjectReference.cs delete mode 100644 src/Castle.Core/DynamicProxy/Serialization/ProxyTypeConstants.cs delete mode 100644 src/Castle.Core/DynamicProxy/Tokens/FormatterServicesMethods.cs delete mode 100644 src/Castle.Core/DynamicProxy/Tokens/SerializationInfoMethods.cs diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/ModuleScopeTestCase.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/ModuleScopeTestCase.cs index 28f63e554f..c72d07ff92 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/ModuleScopeTestCase.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/ModuleScopeTestCase.cs @@ -18,7 +18,9 @@ namespace Castle.DynamicProxy.Tests using System.IO; using System.Reflection; using Castle.DynamicProxy.Generators; +#if FEATURE_SERIALIZATION using Castle.DynamicProxy.Serialization; +#endif using Castle.DynamicProxy.Tests.InterClasses; using NUnit.Framework; diff --git a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyInstanceContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyInstanceContributor.cs index af37cfd0fa..b4efbeaffc 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyInstanceContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyInstanceContributor.cs @@ -26,9 +26,8 @@ namespace Castle.DynamicProxy.Contributors internal class ClassProxyInstanceContributor : ProxyInstanceContributor { - public ClassProxyInstanceContributor(Type targetType, IList methodsToSkip, Type[] interfaces, - string typeId) - : base(targetType, interfaces, typeId) + public ClassProxyInstanceContributor(Type targetType, IList methodsToSkip, Type[] interfaces) + : base(targetType, interfaces) { } diff --git a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyWithTargetInstanceContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyWithTargetInstanceContributor.cs index a616360bf8..458f297d43 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyWithTargetInstanceContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyWithTargetInstanceContributor.cs @@ -24,8 +24,8 @@ namespace Castle.DynamicProxy.Contributors class ClassProxyWithTargetInstanceContributor : ClassProxyInstanceContributor { public ClassProxyWithTargetInstanceContributor(Type targetType, IList methodsToSkip, - Type[] interfaces, string typeId) - : base(targetType, methodsToSkip, interfaces, typeId) + Type[] interfaces) + : base(targetType, methodsToSkip, interfaces) { } diff --git a/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyInstanceContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyInstanceContributor.cs index 1866ef08be..67ed6e5350 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyInstanceContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyInstanceContributor.cs @@ -28,8 +28,8 @@ protected override Reference GetTargetReference(ClassEmitter emitter) return emitter.GetField("__target"); } - public InterfaceProxyInstanceContributor(Type targetType, string proxyGeneratorId, Type[] interfaces) - : base(targetType, interfaces, proxyGeneratorId) + public InterfaceProxyInstanceContributor(Type targetType, Type[] interfaces) + : base(targetType, interfaces) { } } diff --git a/src/Castle.Core/DynamicProxy/Contributors/ProxyInstanceContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/ProxyInstanceContributor.cs index 8c79669d1d..e4e6a50289 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/ProxyInstanceContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/ProxyInstanceContributor.cs @@ -27,13 +27,11 @@ namespace Castle.DynamicProxy.Contributors internal abstract class ProxyInstanceContributor : ITypeContributor { protected readonly Type targetType; - private readonly string proxyTypeId; private readonly Type[] interfaces; - protected ProxyInstanceContributor(Type targetType, Type[] interfaces, string proxyTypeId) + protected ProxyInstanceContributor(Type targetType, Type[] interfaces) { this.targetType = targetType; - this.proxyTypeId = proxyTypeId; this.interfaces = interfaces ?? Type.EmptyTypes; } diff --git a/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs index 8499a81793..4c2b187783 100644 --- a/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs @@ -23,7 +23,6 @@ namespace Castle.DynamicProxy.Generators using Castle.DynamicProxy.Generators.Emitters; using Castle.DynamicProxy.Generators.Emitters.SimpleAST; using Castle.DynamicProxy.Internal; - using Castle.DynamicProxy.Serialization; internal class ClassProxyGenerator : BaseProxyGenerator { @@ -106,7 +105,7 @@ protected virtual Type GenerateType(string name, Type[] interfaces, INamingScope INamingScope namingScope) { var methodsToSkip = new List(); - var proxyInstance = new ClassProxyInstanceContributor(targetType, methodsToSkip, interfaces, ProxyTypeConstants.Class); + var proxyInstance = new ClassProxyInstanceContributor(targetType, methodsToSkip, interfaces); // TODO: the trick with methodsToSkip is not very nice... var proxyTarget = new ClassProxyTargetContributor(targetType, methodsToSkip, namingScope) { Logger = Logger }; IDictionary typeImplementerMapping = new Dictionary(); diff --git a/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs index 4eb61bb858..a25696b67a 100644 --- a/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs @@ -23,7 +23,6 @@ namespace Castle.DynamicProxy.Generators using Castle.DynamicProxy.Generators.Emitters; using Castle.DynamicProxy.Generators.Emitters.SimpleAST; using Castle.DynamicProxy.Internal; - using Castle.DynamicProxy.Serialization; internal class ClassProxyWithTargetGenerator : BaseProxyGenerator { @@ -52,8 +51,7 @@ public Type GetGeneratedType() INamingScope namingScope) { var methodsToSkip = new List(); - var proxyInstance = new ClassProxyWithTargetInstanceContributor(targetType, methodsToSkip, additionalInterfacesToProxy, - ProxyTypeConstants.ClassWithTarget); + var proxyInstance = new ClassProxyWithTargetInstanceContributor(targetType, methodsToSkip, additionalInterfacesToProxy); // TODO: the trick with methodsToSkip is not very nice... var proxyTarget = new ClassProxyWithTargetTargetContributor(targetType, methodsToSkip, namingScope) { Logger = Logger }; diff --git a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs index 5d15071a94..ca8334ce43 100644 --- a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs @@ -23,7 +23,6 @@ namespace Castle.DynamicProxy.Generators using Castle.DynamicProxy.Generators.Emitters; using Castle.DynamicProxy.Generators.Emitters.SimpleAST; using Castle.DynamicProxy.Internal; - using Castle.DynamicProxy.Serialization; internal class InterfaceProxyWithTargetGenerator : BaseProxyGenerator { @@ -40,11 +39,6 @@ protected virtual bool AllowChangeTarget get { return false; } } - protected virtual string GeneratorType - { - get { return ProxyTypeConstants.InterfaceWithTarget; } - } - public Type GenerateCode(Type proxyTargetType, Type[] interfaces, ProxyGenerationOptions options) { // make sure ProxyGenerationOptions is initialized @@ -206,7 +200,7 @@ protected virtual Type GenerateType(string typeName, Type proxyTargetType, Type[ } // 4. plus special interfaces - var instance = new InterfaceProxyInstanceContributor(targetType, GeneratorType, interfaces); + var instance = new InterfaceProxyInstanceContributor(targetType, interfaces); try { AddMappingNoCheck(typeof(IProxyTargetAccessor), instance, typeImplementerMapping); diff --git a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetInterfaceGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetInterfaceGenerator.cs index ac7448342e..511e809c8c 100644 --- a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetInterfaceGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetInterfaceGenerator.cs @@ -22,7 +22,6 @@ namespace Castle.DynamicProxy.Generators using Castle.DynamicProxy.Generators.Emitters; using Castle.DynamicProxy.Generators.Emitters.SimpleAST; using Castle.DynamicProxy.Internal; - using Castle.DynamicProxy.Serialization; internal class InterfaceProxyWithTargetInterfaceGenerator : InterfaceProxyWithTargetGenerator { @@ -36,11 +35,6 @@ protected override bool AllowChangeTarget get { return true; } } - protected override string GeneratorType - { - get { return ProxyTypeConstants.InterfaceWithTargetInterface; } - } - protected override ITypeContributor AddMappingForTargetType( IDictionary typeImplementerMapping, Type proxyTargetType, ICollection targetInterfaces, ICollection additionalInterfaces, INamingScope namingScope) diff --git a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithoutTargetGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithoutTargetGenerator.cs index 8654e5ec6e..d9c3e85a39 100644 --- a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithoutTargetGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithoutTargetGenerator.cs @@ -21,7 +21,6 @@ namespace Castle.DynamicProxy.Generators using Castle.DynamicProxy.Generators.Emitters; using Castle.DynamicProxy.Generators.Emitters.SimpleAST; using Castle.DynamicProxy.Internal; - using Castle.DynamicProxy.Serialization; internal class InterfaceProxyWithoutTargetGenerator : InterfaceProxyWithTargetGenerator { @@ -29,11 +28,6 @@ public InterfaceProxyWithoutTargetGenerator(ModuleScope scope, Type @interface) { } - protected override string GeneratorType - { - get { return ProxyTypeConstants.InterfaceWithoutTarget; } - } - protected override ITypeContributor AddMappingForTargetType( IDictionary interfaceTypeImplementerMapping, Type proxyTargetType, ICollection targetInterfaces, ICollection additionalInterfaces, INamingScope namingScope) diff --git a/src/Castle.Core/DynamicProxy/Internal/TypeUtil.cs b/src/Castle.Core/DynamicProxy/Internal/TypeUtil.cs index 9bf7df268d..1e55749c1d 100644 --- a/src/Castle.Core/DynamicProxy/Internal/TypeUtil.cs +++ b/src/Castle.Core/DynamicProxy/Internal/TypeUtil.cs @@ -216,14 +216,6 @@ internal static void SetStaticField(this Type type, string fieldName, BindingFla } } - public static MemberInfo[] Sort(MemberInfo[] members) - { - var sortedMembers = new MemberInfo[members.Length]; - Array.Copy(members, sortedMembers, members.Length); - Array.Sort(sortedMembers, (l, r) => string.Compare(l.Name, r.Name, StringComparison.OrdinalIgnoreCase)); - return sortedMembers; - } - /// /// Checks whether the specified is a delegate type (i.e. a direct subclass of ). /// diff --git a/src/Castle.Core/DynamicProxy/Serialization/ProxyObjectReference.cs b/src/Castle.Core/DynamicProxy/Serialization/ProxyObjectReference.cs deleted file mode 100644 index c36f97eff4..0000000000 --- a/src/Castle.Core/DynamicProxy/Serialization/ProxyObjectReference.cs +++ /dev/null @@ -1,294 +0,0 @@ -// Copyright 2004-2012 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. - -#if FEATURE_SERIALIZATION - -namespace Castle.DynamicProxy.Serialization -{ - using System; - using System.Collections.Generic; - using System.Diagnostics; - using System.Reflection; - using System.Runtime.Serialization; - - using Castle.DynamicProxy.Generators; - using Castle.DynamicProxy.Internal; - - /// - /// Handles the deserialization of proxies. - /// - [Serializable] - public class ProxyObjectReference : IObjectReference, ISerializable, IDeserializationCallback - { - private static ModuleScope scope = new ModuleScope(); - - private readonly SerializationInfo info; - private readonly StreamingContext context; - - private readonly Type baseType; - private readonly Type[] interfaces; - private readonly object proxy; - private readonly ProxyGenerationOptions proxyGenerationOptions; - - private bool isInterfaceProxy; - private bool delegateToBase; - - /// - /// Resets the used for deserialization to a new scope. - /// - /// - /// This is useful for test cases. - /// - public static void ResetScope() - { - SetScope(new ModuleScope()); - } - - /// - /// Resets the used for deserialization to a given . - /// - /// The scope to be used for deserialization. - /// - /// By default, the deserialization process uses a different scope than the rest of the application, which can lead to multiple proxies - /// being generated for the same type. By explicitly setting the deserialization scope to the application's scope, this can be avoided. - /// - public static void SetScope(ModuleScope scope) - { - if (scope == null) - { - throw new ArgumentNullException("scope"); - } - ProxyObjectReference.scope = scope; - } - - /// - /// Gets the used for deserialization. - /// - /// As has no way of automatically determining the scope used by the application (and the application might use more than one scope at the same time), uses a dedicated scope instance for deserializing proxy types. This instance can be reset and set to a specific value via and . - public static ModuleScope ModuleScope - { - get { return scope; } - } - - protected ProxyObjectReference(SerializationInfo info, StreamingContext context) - { - this.info = info; - this.context = context; - - baseType = DeserializeTypeFromString("__baseType"); - - var _interfaceNames = (String[])info.GetValue("__interfaces", typeof(String[])); - interfaces = new Type[_interfaceNames.Length]; - - for (var i = 0; i < _interfaceNames.Length; i++) - { - interfaces[i] = Type.GetType(_interfaceNames[i]); - } - - proxyGenerationOptions = - (ProxyGenerationOptions)info.GetValue("__proxyGenerationOptions", typeof(ProxyGenerationOptions)); - proxy = RecreateProxy(); - - // We'll try to deserialize as much of the proxy state as possible here. This is just best effort; due to deserialization dependency reasons, - // we need to repeat this in OnDeserialization to guarantee correct state deserialization. - DeserializeProxyState(); - } - - private Type DeserializeTypeFromString(string key) - { - return Type.GetType(info.GetString(key), true, false); - } - - protected virtual object RecreateProxy() - { - var generatorType = GetValue("__proxyTypeId"); - if (generatorType.Equals(ProxyTypeConstants.Class)) - { - isInterfaceProxy = false; - return RecreateClassProxy(); - } - if (generatorType.Equals(ProxyTypeConstants.ClassWithTarget)) - { - isInterfaceProxy = false; - return RecreateClassProxyWithTarget(); - } - isInterfaceProxy = true; - return RecreateInterfaceProxy(generatorType); - } - - private object RecreateClassProxyWithTarget() - { - var generator = new ClassProxyWithTargetGenerator(scope, baseType, interfaces, proxyGenerationOptions); - var proxyType = generator.GetGeneratedType(); - return InstantiateClassProxy(proxyType); - } - - public object RecreateInterfaceProxy(string generatorType) - { - var @interface = DeserializeTypeFromString("__theInterface"); - var targetType = DeserializeTypeFromString("__targetFieldType"); - - InterfaceProxyWithTargetGenerator generator; - if (generatorType == ProxyTypeConstants.InterfaceWithTarget) - { - generator = new InterfaceProxyWithTargetGenerator(scope, @interface); - } - else if (generatorType == ProxyTypeConstants.InterfaceWithoutTarget) - { - generator = new InterfaceProxyWithoutTargetGenerator(scope, @interface); - } - else if (generatorType == ProxyTypeConstants.InterfaceWithTargetInterface) - { - generator = new InterfaceProxyWithTargetInterfaceGenerator(scope, @interface); - } - else - { - throw new InvalidOperationException( - string.Format( - "Got value {0} for the interface generator type, which is not known for the purpose of serialization.", - generatorType)); - } - - var proxyType = generator.GenerateCode(targetType, interfaces, proxyGenerationOptions); - return FormatterServices.GetSafeUninitializedObject(proxyType); - } - - public object RecreateClassProxy() - { - var generator = new ClassProxyGenerator(scope, baseType); - var proxyType = generator.GenerateCode(interfaces, proxyGenerationOptions); - return InstantiateClassProxy(proxyType); - } - - private object InstantiateClassProxy(Type proxy_type) - { - delegateToBase = GetValue("__delegateToBase"); - if (delegateToBase) - { - return Activator.CreateInstance(proxy_type, new object[] { info, context }); - } - else - { - return FormatterServices.GetSafeUninitializedObject(proxy_type); - } - } - - protected void InvokeCallback(object target) - { - if (target is IDeserializationCallback) - { - (target as IDeserializationCallback).OnDeserialization(this); - } - } - - public object GetRealObject(StreamingContext context) - { - return proxy; - } - - public void GetObjectData(SerializationInfo info, StreamingContext context) - { - // There is no need to implement this method as - // this class would never be serialized. - } - - public void OnDeserialization(object sender) - { - var interceptors = GetValue("__interceptors"); - SetInterceptors(interceptors); - - DeserializeProxyMembers(); - - // Get the proxy state again, to get all those members we couldn't get in the constructor due to deserialization ordering. - DeserializeProxyState(); - InvokeCallback(proxy); - } - - private void DeserializeProxyMembers() - { - var proxyType = proxy.GetType(); - var members = FormatterServices.GetSerializableMembers(proxyType); - - var deserializedMembers = new List(); - var deserializedValues = new List(); - for (var i = 0; i < members.Length; i++) - { - var member = members[i] as FieldInfo; - // we get some inherited members... - if (member.DeclaringType != proxyType) - { - continue; - } - - Debug.Assert(member != null); - var value = info.GetValue(member.Name, member.FieldType); - deserializedMembers.Add(member); - deserializedValues.Add(value); - } - FormatterServices.PopulateObjectMembers(proxy, deserializedMembers.ToArray(), deserializedValues.ToArray()); - } - - private void DeserializeProxyState() - { - if (isInterfaceProxy) - { - var target = GetValue("__target"); - SetTarget(target); - } - else if (!delegateToBase) - { - var baseMemberData = GetValue("__data"); - var members = FormatterServices.GetSerializableMembers(baseType); - - // Sort to keep order on both serialize and deserialize side the same, c.f DYNPROXY-ISSUE-127 - members = TypeUtil.Sort(members); - - FormatterServices.PopulateObjectMembers(proxy, members, baseMemberData); - } - } - - private void SetTarget(object target) - { - var targetField = proxy.GetType().GetField("__target", BindingFlags.Instance | BindingFlags.NonPublic); - if (targetField == null) - { - throw new SerializationException( - "The SerializationInfo specifies an invalid interface proxy type, which has no __target field."); - } - - targetField.SetValue(proxy, target); - } - - private void SetInterceptors(IInterceptor[] interceptors) - { - var interceptorField = proxy.GetType().GetField("__interceptors", BindingFlags.Instance | BindingFlags.NonPublic); - if (interceptorField == null) - { - throw new SerializationException( - "The SerializationInfo specifies an invalid proxy type, which has no __interceptors field."); - } - - interceptorField.SetValue(proxy, interceptors); - } - - private T GetValue(string name) - { - return (T)info.GetValue(name, typeof(T)); - } - } -} - -#endif \ No newline at end of file diff --git a/src/Castle.Core/DynamicProxy/Serialization/ProxyTypeConstants.cs b/src/Castle.Core/DynamicProxy/Serialization/ProxyTypeConstants.cs deleted file mode 100644 index 2b02e846bf..0000000000 --- a/src/Castle.Core/DynamicProxy/Serialization/ProxyTypeConstants.cs +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2004-2011 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.Serialization -{ - internal static class ProxyTypeConstants - { - public static readonly string Class = "class"; - public static readonly string ClassWithTarget = "class.with.target"; - public static readonly string InterfaceWithTarget = "interface.with.target"; - public static readonly string InterfaceWithTargetInterface = "interface.with.target.interface"; - public static readonly string InterfaceWithoutTarget = "interface.without.target"; - } -} \ No newline at end of file diff --git a/src/Castle.Core/DynamicProxy/Tokens/FormatterServicesMethods.cs b/src/Castle.Core/DynamicProxy/Tokens/FormatterServicesMethods.cs deleted file mode 100644 index a4145f40cf..0000000000 --- a/src/Castle.Core/DynamicProxy/Tokens/FormatterServicesMethods.cs +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2004-2011 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. - -#if FEATURE_SERIALIZATION - -namespace Castle.DynamicProxy.Tokens -{ - using System; - using System.Reflection; - using System.Runtime.Serialization; - - internal static class FormatterServicesMethods - { - public static readonly MethodInfo GetObjectData = - typeof(FormatterServices).GetMethod("GetObjectData", new[] { typeof(object), typeof(MemberInfo[]) }); - - public static readonly MethodInfo GetSerializableMembers = - typeof(FormatterServices).GetMethod("GetSerializableMembers", new[] { typeof(Type) }); - } -} - -#endif \ No newline at end of file diff --git a/src/Castle.Core/DynamicProxy/Tokens/SerializationInfoMethods.cs b/src/Castle.Core/DynamicProxy/Tokens/SerializationInfoMethods.cs deleted file mode 100644 index bf72ac85de..0000000000 --- a/src/Castle.Core/DynamicProxy/Tokens/SerializationInfoMethods.cs +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright 2004-2011 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. - -#if FEATURE_SERIALIZATION - -namespace Castle.DynamicProxy.Tokens -{ - using System; - using System.Reflection; - using System.Runtime.Serialization; - - /// - /// Holds objects representing methods of class. - /// - internal static class SerializationInfoMethods - { - /// - /// - /// - public static readonly MethodInfo AddValue_Bool = - typeof(SerializationInfo).GetMethod("AddValue", new[] { typeof(String), typeof(bool) }); - - /// - /// - /// - public static readonly MethodInfo AddValue_Int32 = - typeof(SerializationInfo).GetMethod("AddValue", new[] { typeof(String), typeof(int) }); - - /// - /// - /// - public static readonly MethodInfo AddValue_Object = - typeof(SerializationInfo).GetMethod("AddValue", new[] { typeof(String), typeof(Object) }); - - /// - /// - /// - public static readonly MethodInfo GetValue = - typeof(SerializationInfo).GetMethod("GetValue", new[] { typeof(String), typeof(Type) }); - - /// - /// - /// - public static readonly MethodInfo SetType = - typeof(SerializationInfo).GetMethod("SetType"); - } -} - -#endif \ No newline at end of file diff --git a/src/Castle.Core/DynamicProxy/Tokens/TypeUtilMethods.cs b/src/Castle.Core/DynamicProxy/Tokens/TypeUtilMethods.cs index 90f0801589..53c3714af5 100644 --- a/src/Castle.Core/DynamicProxy/Tokens/TypeUtilMethods.cs +++ b/src/Castle.Core/DynamicProxy/Tokens/TypeUtilMethods.cs @@ -20,7 +20,6 @@ namespace Castle.DynamicProxy.Tokens internal static class TypeUtilMethods { - public static readonly MethodInfo Sort = typeof(TypeUtil).GetMethod("Sort", BindingFlags.Public | BindingFlags.Static); public static readonly MethodInfo GetTypeOrNull = typeof(TypeUtil).GetMethod("GetTypeOrNull", BindingFlags.Public | BindingFlags.Static); } } \ No newline at end of file From ad3c3c4be8777c6711e713d2914930eb781e9e58 Mon Sep 17 00:00:00 2001 From: stakx Date: Fri, 12 Jun 2020 19:04:16 +0200 Subject: [PATCH 04/10] Refactor: remove superfluous ctor args --- .../Contributors/ClassProxyInstanceContributor.cs | 8 ++------ .../ClassProxyWithTargetInstanceContributor.cs | 7 ++----- .../Contributors/InterfaceProxyInstanceContributor.cs | 10 ++++------ .../Contributors/ProxyInstanceContributor.cs | 7 +------ .../DynamicProxy/Generators/ClassProxyGenerator.cs | 2 +- .../Generators/ClassProxyWithTargetGenerator.cs | 2 +- .../Generators/InterfaceProxyWithTargetGenerator.cs | 2 +- 7 files changed, 12 insertions(+), 26 deletions(-) diff --git a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyInstanceContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyInstanceContributor.cs index b4efbeaffc..9671e3d710 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyInstanceContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyInstanceContributor.cs @@ -15,19 +15,15 @@ namespace Castle.DynamicProxy.Contributors { using System; - using System.Collections.Generic; - using System.Reflection; using Castle.DynamicProxy.Generators.Emitters; - using Castle.DynamicProxy.Generators.Emitters.CodeBuilders; using Castle.DynamicProxy.Generators.Emitters.SimpleAST; using Castle.DynamicProxy.Internal; - using Castle.DynamicProxy.Tokens; internal class ClassProxyInstanceContributor : ProxyInstanceContributor { - public ClassProxyInstanceContributor(Type targetType, IList methodsToSkip, Type[] interfaces) - : base(targetType, interfaces) + public ClassProxyInstanceContributor(Type targetType) + : base(targetType) { } diff --git a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyWithTargetInstanceContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyWithTargetInstanceContributor.cs index 458f297d43..c31620287e 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyWithTargetInstanceContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyWithTargetInstanceContributor.cs @@ -15,17 +15,14 @@ namespace Castle.DynamicProxy.Contributors { using System; - using System.Collections.Generic; - using System.Reflection; using Castle.DynamicProxy.Generators.Emitters; using Castle.DynamicProxy.Generators.Emitters.SimpleAST; class ClassProxyWithTargetInstanceContributor : ClassProxyInstanceContributor { - public ClassProxyWithTargetInstanceContributor(Type targetType, IList methodsToSkip, - Type[] interfaces) - : base(targetType, methodsToSkip, interfaces) + public ClassProxyWithTargetInstanceContributor(Type targetType) + : base(targetType) { } diff --git a/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyInstanceContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyInstanceContributor.cs index 67ed6e5350..095e221acd 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyInstanceContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyInstanceContributor.cs @@ -17,20 +17,18 @@ namespace Castle.DynamicProxy.Contributors using System; using Castle.DynamicProxy.Generators.Emitters; - using Castle.DynamicProxy.Generators.Emitters.CodeBuilders; using Castle.DynamicProxy.Generators.Emitters.SimpleAST; - using Castle.DynamicProxy.Tokens; internal class InterfaceProxyInstanceContributor : ProxyInstanceContributor { - protected override Reference GetTargetReference(ClassEmitter emitter) + public InterfaceProxyInstanceContributor(Type targetType) + : base(targetType) { - return emitter.GetField("__target"); } - public InterfaceProxyInstanceContributor(Type targetType, Type[] interfaces) - : base(targetType, interfaces) + protected override Reference GetTargetReference(ClassEmitter emitter) { + return emitter.GetField("__target"); } } } \ No newline at end of file diff --git a/src/Castle.Core/DynamicProxy/Contributors/ProxyInstanceContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/ProxyInstanceContributor.cs index e4e6a50289..e877a5f69b 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/ProxyInstanceContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/ProxyInstanceContributor.cs @@ -15,24 +15,19 @@ namespace Castle.DynamicProxy.Contributors { using System; - using System.Reflection; using Castle.DynamicProxy.Generators; using Castle.DynamicProxy.Generators.Emitters; - using Castle.DynamicProxy.Generators.Emitters.CodeBuilders; using Castle.DynamicProxy.Generators.Emitters.SimpleAST; using Castle.DynamicProxy.Internal; - using Castle.DynamicProxy.Tokens; internal abstract class ProxyInstanceContributor : ITypeContributor { protected readonly Type targetType; - private readonly Type[] interfaces; - protected ProxyInstanceContributor(Type targetType, Type[] interfaces) + protected ProxyInstanceContributor(Type targetType) { this.targetType = targetType; - this.interfaces = interfaces ?? Type.EmptyTypes; } protected abstract Reference GetTargetReference(ClassEmitter emitter); diff --git a/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs index 4c2b187783..5c7ebe0281 100644 --- a/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs @@ -105,7 +105,7 @@ protected virtual Type GenerateType(string name, Type[] interfaces, INamingScope INamingScope namingScope) { var methodsToSkip = new List(); - var proxyInstance = new ClassProxyInstanceContributor(targetType, methodsToSkip, interfaces); + var proxyInstance = new ClassProxyInstanceContributor(targetType); // TODO: the trick with methodsToSkip is not very nice... var proxyTarget = new ClassProxyTargetContributor(targetType, methodsToSkip, namingScope) { Logger = Logger }; IDictionary typeImplementerMapping = new Dictionary(); diff --git a/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs index a25696b67a..521c9979d1 100644 --- a/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs @@ -51,7 +51,7 @@ public Type GetGeneratedType() INamingScope namingScope) { var methodsToSkip = new List(); - var proxyInstance = new ClassProxyWithTargetInstanceContributor(targetType, methodsToSkip, additionalInterfacesToProxy); + var proxyInstance = new ClassProxyWithTargetInstanceContributor(targetType); // TODO: the trick with methodsToSkip is not very nice... var proxyTarget = new ClassProxyWithTargetTargetContributor(targetType, methodsToSkip, namingScope) { Logger = Logger }; diff --git a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs index ca8334ce43..c1ada04caa 100644 --- a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs @@ -200,7 +200,7 @@ protected virtual Type GenerateType(string typeName, Type proxyTargetType, Type[ } // 4. plus special interfaces - var instance = new InterfaceProxyInstanceContributor(targetType, interfaces); + var instance = new InterfaceProxyInstanceContributor(targetType); try { AddMappingNoCheck(typeof(IProxyTargetAccessor), instance, typeImplementerMapping); From 1d34cd1b2ed6e08117ad835aa167f04cf1fe0cd9 Mon Sep 17 00:00:00 2001 From: stakx Date: Fri, 12 Jun 2020 19:21:22 +0200 Subject: [PATCH 05/10] Remove `LoadAssemblyIntoCache` API --- src/Castle.Core/DynamicProxy/ModuleScope.cs | 68 --------------------- 1 file changed, 68 deletions(-) diff --git a/src/Castle.Core/DynamicProxy/ModuleScope.cs b/src/Castle.Core/DynamicProxy/ModuleScope.cs index 01b45b5df1..da5a2f910d 100644 --- a/src/Castle.Core/DynamicProxy/ModuleScope.cs +++ b/src/Castle.Core/DynamicProxy/ModuleScope.cs @@ -15,19 +15,13 @@ namespace Castle.DynamicProxy { using System; - using System.Collections.Generic; - using System.Diagnostics; using System.IO; using System.Reflection; using System.Reflection.Emit; using System.Resources; - using System.Threading; using Castle.Core.Internal; using Castle.DynamicProxy.Generators; -#if FEATURE_SERIALIZATION - using Castle.DynamicProxy.Serialization; -#endif public class ModuleScope { @@ -463,73 +457,11 @@ public string SaveAssembly(bool strongNamed) File.Delete(assemblyFilePath); } -#if FEATURE_SERIALIZATION - AddCacheMappings(assemblyBuilder); -#endif - assemblyBuilder.Save(assemblyFileName); return assemblyFilePath; } #endif -#if FEATURE_SERIALIZATION - private void AddCacheMappings(AssemblyBuilder builder) - { - var mappings = new Dictionary(); - - typeCache.ForEach((key, value) => - { - // NOTE: using == returns invalid results. - // we need to use Equals here for it to work properly - if (builder.Equals(value.Assembly)) - { - mappings.Add(key, value.FullName); - } - }); - - CacheMappingsAttribute.ApplyTo(builder, mappings); - } - - /// - /// Loads the generated types from the given assembly into this 's cache. - /// - /// The assembly to load types from. This assembly must have been saved via or - /// , or it must have the manually applied. - /// - /// This method can be used to load previously generated and persisted proxy types from disk into this scope's type cache, e.g. in order - /// to avoid the performance hit associated with proxy generation. - /// - public void LoadAssemblyIntoCache(Assembly assembly) - { - if (assembly == null) - { - throw new ArgumentNullException("assembly"); - } - - var cacheMappings = - (CacheMappingsAttribute[])assembly.GetCustomAttributes(typeof(CacheMappingsAttribute), false); - - if (cacheMappings.Length == 0) - { - var message = string.Format( - "The given assembly '{0}' does not contain any cache information for generated types.", - assembly.FullName); - throw new ArgumentException(message, "assembly"); - } - - foreach (var mapping in cacheMappings[0].GetDeserializedMappings()) - { - var loadedType = assembly.GetType(mapping.Value); - - if (loadedType != null) - { - typeCache.AddOrUpdateWithoutTakingLock(mapping.Key, loadedType); - } - } - } -#endif - internal TypeBuilder DefineType(bool inSignedModulePreferably, string name, TypeAttributes flags) { var module = ObtainDynamicModule(disableSignedModule == false && inSignedModulePreferably); From 5b1ac480f0ac5633f6105b7f4111150ea940cef3 Mon Sep 17 00:00:00 2001 From: stakx Date: Fri, 12 Jun 2020 19:21:48 +0200 Subject: [PATCH 06/10] Remove tests related to `LoadAssemblyIntoCache` --- .../CrossAppDomainCaller.cs | 57 ----- .../DynamicProxy.Tests/ModuleScopeTestCase.cs | 177 +-------------- .../MultipleSavedAssembliesTestCase.cs | 204 ------------------ 3 files changed, 2 insertions(+), 436 deletions(-) delete mode 100644 src/Castle.Core.Tests/DynamicProxy.Tests/CrossAppDomainCaller.cs delete mode 100644 src/Castle.Core.Tests/DynamicProxy.Tests/MultipleSavedAssembliesTestCase.cs diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/CrossAppDomainCaller.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/CrossAppDomainCaller.cs deleted file mode 100644 index 5b470d4c87..0000000000 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/CrossAppDomainCaller.cs +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright 2004-2010 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. - -#if FEATURE_APPDOMAIN - -namespace Castle.DynamicProxy.Tests -{ - using System; - -#if FEATURE_SERIALIZATION - [Serializable] -#endif - public class CrossAppDomainCaller - { - public static void RunInOtherAppDomain(Action callback, params object[] args) - { - CrossAppDomainCaller callbackObject = new CrossAppDomainCaller(callback, args); - AppDomain newDomain = AppDomain.CreateDomain("otherDomain", AppDomain.CurrentDomain.Evidence, - AppDomain.CurrentDomain.SetupInformation); - try - { - newDomain.DoCallBack(callbackObject.Run); - } - finally - { - AppDomain.Unload(newDomain); - } - } - - private readonly Action callback; - private readonly object[] args; - - public CrossAppDomainCaller(Action callback, object[] args) - { - this.callback = callback; - this.args = args; - } - - private void Run() - { - callback(args); - } - } -} - -#endif \ No newline at end of file diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/ModuleScopeTestCase.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/ModuleScopeTestCase.cs index c72d07ff92..01e321531e 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/ModuleScopeTestCase.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/ModuleScopeTestCase.cs @@ -17,11 +17,10 @@ namespace Castle.DynamicProxy.Tests using System; using System.IO; using System.Reflection; + using Castle.DynamicProxy.Generators; -#if FEATURE_SERIALIZATION - using Castle.DynamicProxy.Serialization; -#endif using Castle.DynamicProxy.Tests.InterClasses; + using NUnit.Framework; [TestFixture] @@ -274,54 +273,6 @@ public void ExplicitSaveThrowsWhenSpecifiedAssemblyNotGeneratedStrongName() } #endif -#if FEATURE_SERIALIZATION - [Test] - public void SavedAssemblyHasCacheMappings() - { - var scope = new ModuleScope(true); - scope.ObtainDynamicModuleWithWeakName(); - - var savedPath = scope.SaveAssembly(); - - CrossAppDomainCaller.RunInOtherAppDomain(delegate(object[] args) - { - var assembly = Assembly.LoadFrom((string) args[0]); - Assert.IsTrue(assembly.IsDefined(typeof (CacheMappingsAttribute), false)); - }, - savedPath); - - File.Delete(savedPath); - } - - [Test] - public void CacheMappingsHoldTypes() - { - var scope = new ModuleScope(true); - var builder = new DefaultProxyBuilder(scope); - var cp = builder.CreateClassProxyType(typeof (object), Type.EmptyTypes, ProxyGenerationOptions.Default); - - var savedPath = scope.SaveAssembly(); - - CrossAppDomainCaller.RunInOtherAppDomain(delegate(object[] args) - { - var assembly = Assembly.LoadFrom((string) args[0]); - var attribute = - (CacheMappingsAttribute) - assembly.GetCustomAttributes(typeof (CacheMappingsAttribute), false)[0]; - var entries = attribute.GetDeserializedMappings(); - Assert.AreEqual(1, entries.Count); - - var key = new CacheKey(typeof (object), new Type[0], - ProxyGenerationOptions.Default); - Assert.IsTrue(entries.ContainsKey(key)); - Assert.AreEqual(args[1], entries[key]); - }, - savedPath, cp.FullName); - - File.Delete(savedPath); - } -#endif - [Test] public void GeneratedAssembliesDefaultName() { @@ -384,129 +335,5 @@ public void DefaultProxyBuilderWithSpecificScope() var builder = new DefaultProxyBuilder(scope); Assert.AreSame(scope, builder.ModuleScope); } - -#if FEATURE_SERIALIZATION - [Test] - public void LoadAssemblyIntoCache_InvalidAssembly() - { - var newScope = new ModuleScope(false); - - Assert.Throws(() => - newScope.LoadAssemblyIntoCache(Assembly.GetExecutingAssembly()) - ); - } - - [Test] - public void LoadAssemblyIntoCache_CreateClassProxy() - { - CheckLoadAssemblyIntoCache( - builder => builder.CreateClassProxyType(typeof (object), null, ProxyGenerationOptions.Default)); - } - - [Test] - public void LoadAssemblyIntoCache_CreateInterfaceProxyTypeWithoutTarget() - { - CheckLoadAssemblyIntoCache( - delegate(IProxyBuilder builder) - { - return builder.CreateInterfaceProxyTypeWithoutTarget(typeof (IServiceProvider), new Type[0], - ProxyGenerationOptions.Default); - }); - } - - [Test] - public void LoadAssemblyIntoCache_CreateInterfaceProxyTypeWithTarget() - { - CheckLoadAssemblyIntoCache( - delegate(IProxyBuilder builder) - { - return builder.CreateInterfaceProxyTypeWithTarget(typeof (IMyInterface2), new Type[0], typeof (MyInterfaceImpl), - ProxyGenerationOptions.Default); - }); - } - - [Test] - public void LoadAssemblyIntoCache_CreateInterfaceProxyTypeWithTargetInterface() - { - CheckLoadAssemblyIntoCache( - delegate(IProxyBuilder builder) - { - return builder.CreateInterfaceProxyTypeWithTargetInterface(typeof (IMyInterface2), null, - ProxyGenerationOptions.Default); - }); - } - - [Test] - public void LoadAssemblyIntoCache_DifferentGenerationOptions() - { - var savedScope = new ModuleScope(true); - var builder = new DefaultProxyBuilder(savedScope); - - var options1 = new ProxyGenerationOptions(); - options1.AddMixinInstance(new DateTime()); - var options2 = ProxyGenerationOptions.Default; - - var cp1 = builder.CreateClassProxyType(typeof (object), Type.EmptyTypes, options1); - var cp2 = builder.CreateClassProxyType(typeof (object), Type.EmptyTypes, options2); - Assert.AreNotSame(cp1, cp2); - Assert.AreSame(cp1, builder.CreateClassProxyType(typeof (object), Type.EmptyTypes, options1)); - Assert.AreSame(cp2, builder.CreateClassProxyType(typeof (object), Type.EmptyTypes, options2)); - - var path = savedScope.SaveAssembly(); - - CrossAppDomainCaller.RunInOtherAppDomain(delegate(object[] args) - { - var newScope = new ModuleScope(false); - var newBuilder = new DefaultProxyBuilder(newScope); - - var assembly = Assembly.LoadFrom((string) args[0]); - newScope.LoadAssemblyIntoCache(assembly); - - var newOptions1 = new ProxyGenerationOptions(); - newOptions1.AddMixinInstance(new DateTime()); - var newOptions2 = ProxyGenerationOptions.Default; - - var loadedCP1 = newBuilder.CreateClassProxyType(typeof (object), - Type.EmptyTypes, - newOptions1); - var loadedCP2 = newBuilder.CreateClassProxyType(typeof (object), - Type.EmptyTypes, - newOptions2); - Assert.AreNotSame(loadedCP1, loadedCP2); - Assert.AreEqual(assembly, loadedCP1.Assembly); - Assert.AreEqual(assembly, loadedCP2.Assembly); - }, path); - - File.Delete(path); - } - - private delegate Type ProxyCreator(IProxyBuilder proxyBuilder); - - private void CheckLoadAssemblyIntoCache(ProxyCreator creator) - { - var savedScope = new ModuleScope(true); - var builder = new DefaultProxyBuilder(savedScope); - - var cp = creator(builder); - Assert.AreSame(cp, creator(builder)); - - var path = savedScope.SaveAssembly(); - - CrossAppDomainCaller.RunInOtherAppDomain(delegate(object[] args) - { - var newScope = new ModuleScope(false); - var newBuilder = new DefaultProxyBuilder(newScope); - - var assembly = Assembly.LoadFrom((string) args[0]); - newScope.LoadAssemblyIntoCache(assembly); - - var loadedCP = assembly.GetType((string) args[1]); - Assert.AreSame(loadedCP, ((ProxyCreator) args[2])(newBuilder)); - Assert.AreEqual(assembly, ((ProxyCreator) args[2])(newBuilder).Assembly); - }, path, cp.FullName, creator); - - File.Delete(path); - } -#endif } } \ No newline at end of file diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/MultipleSavedAssembliesTestCase.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/MultipleSavedAssembliesTestCase.cs deleted file mode 100644 index 61d50f3a1b..0000000000 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/MultipleSavedAssembliesTestCase.cs +++ /dev/null @@ -1,204 +0,0 @@ -// Copyright 2004-2012 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. - -#if FEATURE_SERIALIZATION - -namespace Castle.DynamicProxy.Tests -{ - using System.Reflection; - - using Castle.DynamicProxy.Tests.Classes; - using Castle.DynamicProxy.Tests.Interceptors; - - using NUnit.Framework; - - public interface IGenericInterface - { - void GenericMethod(T toto); - } - - public class Class1 : IGenericInterface - { - public void GenericMethod(IInterface1 toto) - { - } - } - - public class Class2 : IGenericInterface - { - public void GenericMethod(IInterface2 toto) - { - } - } - - public class Class3 : IInterface3, IInterface2 - { - } - - public class Class4 : IInterface3, IInterface1 - { - } - - public interface IInterface1 - { - } - - public interface IInterface2 - { - } - - public interface IInterface3 - { - } - - [TestFixture] - public class MultipleSavedAssembliesTestCase - { - [Test] - [Bug("DYNPROXY-179")] - public void LoadAssemblyIntoCache_InvalidCacheAfterTwoLoadAssemblyIntoCacheThatContainsSameClass() - { - // - // Step 1 - Save an assembly with 1 class proxy - // - var proxyGeneratorModuleScope = new ModuleScope(true, true, ModuleScope.DEFAULT_ASSEMBLY_NAME + "5", "ProxyCache5.dll", ModuleScope.DEFAULT_ASSEMBLY_NAME + "5", "ProxyCache5.dll"); - var proxyBuilder = new DefaultProxyBuilder(proxyGeneratorModuleScope); - var generator = new ProxyGenerator(proxyBuilder); - generator.CreateClassProxy(typeof(EmptyClass), new[] { typeof(IInterface1) }, new DoNothingInterceptor()); - proxyGeneratorModuleScope.SaveAssembly(); - - // - // Step 2 - Save another assembly with 1 class proxy - // note : to reproduce the problem, must load previously saved assembly (in cache) before saving this assembly. - // - proxyGeneratorModuleScope = new ModuleScope(true, true, ModuleScope.DEFAULT_ASSEMBLY_NAME + "6", "ProxyCache6.dll", ModuleScope.DEFAULT_ASSEMBLY_NAME + "6", "ProxyCache6.dll"); - proxyBuilder = new DefaultProxyBuilder(proxyGeneratorModuleScope); - generator = new ProxyGenerator(proxyBuilder); - - var proxyAssembly = Assembly.LoadFrom("ProxyCache5.dll"); - proxyGeneratorModuleScope.LoadAssemblyIntoCache(proxyAssembly); - - generator.CreateClassProxy(typeof(EmptyClass), new[] { typeof(IInterface2) }, new DoNothingInterceptor()); - proxyGeneratorModuleScope.SaveAssembly(); - - // - // Step 3 - Load the last proxy assembly and try to create the first class proxy (created in step 1) - // note : Creating proxy from step 2 works. - // issue : returns the wrong proxy (the one from step 2) - // - proxyGeneratorModuleScope = new ModuleScope(true); - proxyBuilder = new DefaultProxyBuilder(proxyGeneratorModuleScope); - generator = new ProxyGenerator(proxyBuilder); - - proxyAssembly = Assembly.LoadFrom("ProxyCache6.dll"); - proxyGeneratorModuleScope.LoadAssemblyIntoCache(proxyAssembly); - - var invalidProxy = generator.CreateClassProxy(typeof(EmptyClass), new[] { typeof(IInterface1) }, new DoNothingInterceptor()); - if (invalidProxy as IInterface1 == null) - { - Assert.Fail(); - } - } - - [Test] - [Bug("DYNPROXY-179")] - public void LoadAssemblyIntoCache_InvalidCacheAfterTwoLoadAssemblyIntoCacheThatContainsSameGeneric() - { - // - // Step 1 - Save an assembly with 1 generic proxy - // - var proxyGeneratorModuleScope = new ModuleScope(true, true, ModuleScope.DEFAULT_ASSEMBLY_NAME + "1", "ProxyCache1.dll", ModuleScope.DEFAULT_ASSEMBLY_NAME + "1", "ProxyCache1.dll"); - var proxyBuilder = new DefaultProxyBuilder(proxyGeneratorModuleScope); - var generator = new ProxyGenerator(proxyBuilder); - generator.CreateInterfaceProxyWithTargetInterface(typeof(IGenericInterface), new Class1(), new DoNothingInterceptor()); - proxyGeneratorModuleScope.SaveAssembly(); - - // - // Step 2 - Save another assembly with 1 generic proxy - // note : to reproduce the problem, must load previously saved assembly (in cache) before saving this assembly. - // - proxyGeneratorModuleScope = new ModuleScope(true, true, ModuleScope.DEFAULT_ASSEMBLY_NAME + "2", "ProxyCache2.dll", ModuleScope.DEFAULT_ASSEMBLY_NAME + "2", "ProxyCache2.dll"); - proxyBuilder = new DefaultProxyBuilder(proxyGeneratorModuleScope); - generator = new ProxyGenerator(proxyBuilder); - - var proxyAssembly = Assembly.LoadFrom("ProxyCache1.dll"); - proxyGeneratorModuleScope.LoadAssemblyIntoCache(proxyAssembly); - - generator.CreateInterfaceProxyWithTargetInterface(typeof(IGenericInterface), new Class2(), new DoNothingInterceptor()); - proxyGeneratorModuleScope.SaveAssembly(); - - // - // Step 3 - Load the last proxy assembly and try to create the first generic proxy (created in step 1) - // note : Creating proxy from step 2 works. - // exception : Missing method exception, it returns the wrong proxy and the constructor used doesn't match the arguments passed. - // - proxyGeneratorModuleScope = new ModuleScope(true); - proxyBuilder = new DefaultProxyBuilder(proxyGeneratorModuleScope); - generator = new ProxyGenerator(proxyBuilder); - - proxyAssembly = Assembly.LoadFrom("ProxyCache2.dll"); - proxyGeneratorModuleScope.LoadAssemblyIntoCache(proxyAssembly); - - generator.CreateInterfaceProxyWithTargetInterface(typeof(IGenericInterface), new Class1(), new DoNothingInterceptor()); - } - - [Test] - [Bug("DYNPROXY-179")] - public void LoadAssemblyIntoCache_InvalidCacheAfterTwoLoadAssemblyIntoCacheThatContainsSameInterface() - { - // - // Step 1 - Save an assembly with 1 interface proxy - // - var proxyGeneratorModuleScope = new ModuleScope(true, true, ModuleScope.DEFAULT_ASSEMBLY_NAME + "3", "ProxyCache3.dll", ModuleScope.DEFAULT_ASSEMBLY_NAME + "3", "ProxyCache3.dll"); - var proxyBuilder = new DefaultProxyBuilder(proxyGeneratorModuleScope); - var generator = new ProxyGenerator(proxyBuilder); - generator.CreateInterfaceProxyWithTargetInterface(typeof(IInterface3), new[] { typeof(IInterface2) }, new Class3(), new DoNothingInterceptor()); - proxyGeneratorModuleScope.SaveAssembly(); - - // - // Step 2 - Save another assembly with 1 interface proxy - // note : to reproduce the problem, must load previously saved assembly (in cache) before saving this assembly. - // - proxyGeneratorModuleScope = new ModuleScope(true, true, ModuleScope.DEFAULT_ASSEMBLY_NAME + "4", "ProxyCache4.dll", ModuleScope.DEFAULT_ASSEMBLY_NAME + "4", "ProxyCache4.dll"); - proxyBuilder = new DefaultProxyBuilder(proxyGeneratorModuleScope); - generator = new ProxyGenerator(proxyBuilder); - - var proxyAssembly = Assembly.LoadFrom("ProxyCache3.dll"); - proxyGeneratorModuleScope.LoadAssemblyIntoCache(proxyAssembly); - - generator.CreateInterfaceProxyWithTargetInterface(typeof(IInterface3), new[] { typeof(IInterface1) }, new Class4(), new DoNothingInterceptor()); - proxyGeneratorModuleScope.SaveAssembly(); - - // - // Step 3 - Load the last proxy assembly and try to create the first interface proxy (created in step 1) - // note : Creating proxy from step 2 works. - // issue : returns the wrong proxy (the one from step 2) - // - proxyGeneratorModuleScope = new ModuleScope(true); - proxyBuilder = new DefaultProxyBuilder(proxyGeneratorModuleScope); - generator = new ProxyGenerator(proxyBuilder); - - proxyAssembly = Assembly.LoadFrom("ProxyCache4.dll"); - proxyGeneratorModuleScope.LoadAssemblyIntoCache(proxyAssembly); - - var invalidProxy = generator.CreateInterfaceProxyWithTargetInterface(typeof(IInterface3), new[] { typeof(IInterface2) }, new Class3(), new DoNothingInterceptor()); - if (invalidProxy as IInterface2 == null) - { - Assert.Fail(); - } - } - } -} - -#endif \ No newline at end of file From 01061a8f590498144f635a2cccd22e5f3df0f1ba Mon Sep 17 00:00:00 2001 From: stakx Date: Fri, 12 Jun 2020 19:23:48 +0200 Subject: [PATCH 07/10] Remove library code that has become redundant --- .../Serialization/CacheMappingsAttribute.cs | 73 ------------------- 1 file changed, 73 deletions(-) delete mode 100644 src/Castle.Core/DynamicProxy/Serialization/CacheMappingsAttribute.cs diff --git a/src/Castle.Core/DynamicProxy/Serialization/CacheMappingsAttribute.cs b/src/Castle.Core/DynamicProxy/Serialization/CacheMappingsAttribute.cs deleted file mode 100644 index bb8cb380a0..0000000000 --- a/src/Castle.Core/DynamicProxy/Serialization/CacheMappingsAttribute.cs +++ /dev/null @@ -1,73 +0,0 @@ -// Copyright 2004-2012 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. - -#if FEATURE_SERIALIZATION - -namespace Castle.DynamicProxy.Serialization -{ - using System; - using System.Collections.Generic; - using System.IO; - using System.Reflection; - using System.Reflection.Emit; - using System.Runtime.Serialization.Formatters.Binary; - - using Castle.DynamicProxy.Generators; - - /// - /// Applied to the assemblies saved by in order to persist the cache data included in the persisted assembly. - /// - [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = false)] - [CLSCompliant(false)] - public class CacheMappingsAttribute : Attribute - { - private static readonly ConstructorInfo constructor = - typeof(CacheMappingsAttribute).GetConstructor(new[] { typeof(byte[]) }); - - private readonly byte[] serializedCacheMappings; - - public CacheMappingsAttribute(byte[] serializedCacheMappings) - { - this.serializedCacheMappings = serializedCacheMappings; - } - - public byte[] SerializedCacheMappings - { - get { return serializedCacheMappings; } - } - - internal Dictionary GetDeserializedMappings() - { - using (var stream = new MemoryStream(SerializedCacheMappings)) - { - var formatter = new BinaryFormatter(); - return (Dictionary)formatter.Deserialize(stream); - } - } - - internal static void ApplyTo(AssemblyBuilder assemblyBuilder, Dictionary mappings) - { - using (var stream = new MemoryStream()) - { - var formatter = new BinaryFormatter(); - formatter.Serialize(stream, mappings); - var bytes = stream.ToArray(); - var attributeBuilder = new CustomAttributeBuilder(constructor, new object[] { bytes }); - assemblyBuilder.SetCustomAttribute(attributeBuilder); - } - } - } -} - -#endif \ No newline at end of file From 5880b0f32b70c4b6a11ff3f14b2deac22e409dfe Mon Sep 17 00:00:00 2001 From: stakx Date: Fri, 12 Jun 2020 19:26:28 +0200 Subject: [PATCH 08/10] No point anymore in keeping hooks etc. serializable --- docs/dynamicproxy-serializable-types.md | 3 -- docs/dynamicproxy.md | 1 - .../Classes/ClassWithInterface.cs | 3 -- .../Classes/HasNonInheritableAttribute.cs | 3 -- .../Classes/InheritableAttribute.cs | 3 -- .../Classes/NonInheritableAttribute.cs | 3 -- .../NonInheritableWithArray2Attribute.cs | 3 -- .../NonInheritableWithArrayAttribute.cs | 3 -- .../Explicit/SimpleInterfaceExplicit.cs | 3 -- .../GenerationHookTestCase.cs | 3 -- .../InterClasses/AlwaysThrowsServiceImpl.cs | 3 -- .../InterClasses/IMyInterface2.cs | 3 -- .../InterClasses/ServiceImpl.cs | 3 -- .../DynamicProxy.Tests/InterClasses/Two.cs | 3 -- .../InterClasses/WithRefOut.cs | 3 -- .../InterceptorSelectorTargetTypeTestCase.cs | 3 -- .../InterceptorSelectorTestCase.cs | 28 ---------------- .../Interceptors/AddTwoInterceptor.cs | 3 -- .../Interceptors/CallCountingInterceptor.cs | 3 -- .../DynamicProxy.Tests/Interfaces/IEmpty.cs | 3 -- .../Interfaces/IGenericWithRefOut.cs | 3 -- .../DynamicProxy.Tests/MixinTestCase.cs | 3 -- .../ClassImplementingIDerivedSImpleMixin.cs | 3 -- .../Mixins/ClassImplementingISimpleMixin.cs | 3 -- .../DynamicProxy.Tests/Mixins/ComplexMixin.cs | 3 -- ...tructorArgumentsOrderWithMixinsTestCase.cs | 9 ------ .../DynamicProxy.Tests/Mixins/OtherMixin.cs | 3 -- .../DynamicProxy.Tests/Mixins/SimpleMixin.cs | 3 -- .../Mixins/SortingIssueOnMixin.cs | 6 ---- .../OutRefParamsTestCase.cs | 3 -- .../DynamicProxy.Tests/ProxyNothingHook.cs | 3 -- ...xyTypeCachingWithDifferentHooksTestCase.cs | 3 -- .../DynamicProxy/AllMethodsHook.cs | 3 -- .../DynamicProxy/Generators/CacheKey.cs | 3 -- .../DynamicProxy/ProxyGenerationOptions.cs | 32 ------------------- .../DynamicProxy/StandardInterceptor.cs | 3 -- 36 files changed, 169 deletions(-) delete mode 100644 docs/dynamicproxy-serializable-types.md diff --git a/docs/dynamicproxy-serializable-types.md b/docs/dynamicproxy-serializable-types.md deleted file mode 100644 index 54accefe7e..0000000000 --- a/docs/dynamicproxy-serializable-types.md +++ /dev/null @@ -1,3 +0,0 @@ -# Make your supporting classes serializable - -If you're going to be serializing your proxies, you should make all the classes that go with it serializable. That includes proxy generation hooks, interceptors and interceptor selectors. Otherwise, you will get an exception when trying to serialize your proxies. It is not mandatory, but I find it useful. Notice that you will need this also when persisting your proxy assembly to disk. \ No newline at end of file diff --git a/docs/dynamicproxy.md b/docs/dynamicproxy.md index ac8a4bcab6..9447c0bb0a 100644 --- a/docs/dynamicproxy.md +++ b/docs/dynamicproxy.md @@ -15,7 +15,6 @@ If you're new to DynamicProxy you can read a [quick introduction](dynamicproxy-i * [Leaking this](dynamicproxy-leaking-this.md) * [Make proxy generation hooks purely functional](dynamicproxy-generation-hook-pure-function.md) * [Overriding Equals/GetHashCode on proxy generation hook](dynamicproxy-generation-hook-override-equals-gethashcode.md) -* [Make your supporting classes serializable](dynamicproxy-serializable-types.md) * [Use proxy generation hooks and interceptor selectors for fine grained control](dynamicproxy-fine-grained-control.md) * [SRP applies to interceptors](dynamicproxy-srp-applies-to-interceptors.md) * [Behavior of by-reference parameters during interception](dynamicproxy-by-ref-parameters.md) diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/ClassWithInterface.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/ClassWithInterface.cs index fb4c098f8b..627e8d0bf4 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/ClassWithInterface.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/ClassWithInterface.cs @@ -16,9 +16,6 @@ namespace Castle.DynamicProxy.Tests.Classes { using System; -#if FEATURE_SERIALIZATION - [Serializable] -#endif public class ClassWithInterface : ISimpleInterface { public int Do() diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/HasNonInheritableAttribute.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/HasNonInheritableAttribute.cs index 6290a9b781..393ed1c792 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/HasNonInheritableAttribute.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/HasNonInheritableAttribute.cs @@ -60,9 +60,6 @@ public virtual void Do2() } } -#if FEATURE_SERIALIZATION - [Serializable] -#endif [AttributeUsage(AttributeTargets.All, Inherited = false)] public class ComplexNonInheritableAttribute : Attribute { diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/InheritableAttribute.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/InheritableAttribute.cs index 41a6a199dc..8ae798b981 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/InheritableAttribute.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/InheritableAttribute.cs @@ -16,9 +16,6 @@ namespace Castle.DynamicProxy.Tests.Classes { using System; -#if FEATURE_SERIALIZATION - [Serializable] -#endif [AttributeUsage(AttributeTargets.All, Inherited = true)] public class InheritableAttribute : Attribute { diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/NonInheritableAttribute.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/NonInheritableAttribute.cs index 8c43a2481e..99f9603f28 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/NonInheritableAttribute.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/NonInheritableAttribute.cs @@ -16,9 +16,6 @@ namespace Castle.DynamicProxy.Tests.Classes { using System; -#if FEATURE_SERIALIZATION - [Serializable] -#endif [AttributeUsage(AttributeTargets.All, Inherited = false)] public class NonInheritableAttribute : Attribute { diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/NonInheritableWithArray2Attribute.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/NonInheritableWithArray2Attribute.cs index b6ff00cec2..accec0e77d 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/NonInheritableWithArray2Attribute.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/NonInheritableWithArray2Attribute.cs @@ -16,9 +16,6 @@ namespace Castle.DynamicProxy.Tests.Classes { using System; -#if FEATURE_SERIALIZATION - [Serializable] -#endif [AttributeUsage(AttributeTargets.All, Inherited = false)] public class NonInheritableWithArray2Attribute : Attribute { diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/NonInheritableWithArrayAttribute.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/NonInheritableWithArrayAttribute.cs index b84c9c55e7..541457a30d 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/NonInheritableWithArrayAttribute.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/Classes/NonInheritableWithArrayAttribute.cs @@ -16,9 +16,6 @@ namespace Castle.DynamicProxy.Tests.Classes { using System; -#if FEATURE_SERIALIZATION - [Serializable] -#endif [AttributeUsage(AttributeTargets.All, Inherited = false)] public class NonInheritableWithArrayAttribute : Attribute { diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/Explicit/SimpleInterfaceExplicit.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/Explicit/SimpleInterfaceExplicit.cs index de832812a4..0e59edf6ee 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/Explicit/SimpleInterfaceExplicit.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/Explicit/SimpleInterfaceExplicit.cs @@ -16,9 +16,6 @@ namespace Castle.DynamicProxy.Tests.Explicit { using System; -#if FEATURE_SERIALIZATION - [Serializable] -#endif public class SimpleInterfaceExplicit : ISimpleInterface { int ISimpleInterface.Do() diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/GenerationHookTestCase.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/GenerationHookTestCase.cs index 470f416f75..eb12bc5737 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/GenerationHookTestCase.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/GenerationHookTestCase.cs @@ -149,9 +149,6 @@ public void Hook_can_see_ToString_method() } } -#if FEATURE_SERIALIZATION - [Serializable] -#endif public class LogHook : IProxyGenerationHook { private readonly Type targetTypeToAssert; diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/InterClasses/AlwaysThrowsServiceImpl.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/InterClasses/AlwaysThrowsServiceImpl.cs index ee0c8271da..e043cfe931 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/InterClasses/AlwaysThrowsServiceImpl.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/InterClasses/AlwaysThrowsServiceImpl.cs @@ -16,9 +16,6 @@ namespace Castle.DynamicProxy.Tests.InterClasses { using System; -#if FEATURE_SERIALIZATION - [Serializable] -#endif public class AlwaysThrowsServiceImpl : IService { public int Sum(int b1, int b2) diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/InterClasses/IMyInterface2.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/InterClasses/IMyInterface2.cs index 14bfeb7f1e..138f359358 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/InterClasses/IMyInterface2.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/InterClasses/IMyInterface2.cs @@ -27,9 +27,6 @@ public interface IMyInterface2 int Calc(int x, int y, int z, Single k); } -#if FEATURE_SERIALIZATION - [Serializable] -#endif [MyAttribute("MyInterfaceImpl")] public class MyInterfaceImpl : IMyInterface2 { diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/InterClasses/ServiceImpl.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/InterClasses/ServiceImpl.cs index 2338b7fdb6..31b3cb1b57 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/InterClasses/ServiceImpl.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/InterClasses/ServiceImpl.cs @@ -16,9 +16,6 @@ namespace Castle.DynamicProxy.Tests.InterClasses { using System; -#if FEATURE_SERIALIZATION - [Serializable] -#endif public class ServiceImpl : IService, IExtendedService { public int Sum(int b1, int b2) diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/InterClasses/Two.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/InterClasses/Two.cs index bf4ceed64c..4dfde88251 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/InterClasses/Two.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/InterClasses/Two.cs @@ -18,9 +18,6 @@ namespace Castle.DynamicProxy.Tests.InterClasses using Castle.DynamicProxy.Tests.Interfaces; -#if FEATURE_SERIALIZATION - [Serializable] -#endif public class Two : ITwo { public int TwoMethod() diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/InterClasses/WithRefOut.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/InterClasses/WithRefOut.cs index 77328af227..d075a4de6f 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/InterClasses/WithRefOut.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/InterClasses/WithRefOut.cs @@ -18,9 +18,6 @@ namespace Castle.DynamicProxy.Tests.InterClasses using Castle.DynamicProxy.Tests.Interfaces; -#if FEATURE_SERIALIZATION - [Serializable] -#endif public class WithRefOut : IWithRefOut { public void Do(out int i) diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/InterceptorSelectorTargetTypeTestCase.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/InterceptorSelectorTargetTypeTestCase.cs index 2ea9956218..1e82a27370 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/InterceptorSelectorTargetTypeTestCase.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/InterceptorSelectorTargetTypeTestCase.cs @@ -155,9 +155,6 @@ public override void Method() } } -#if FEATURE_SERIALIZATION - [Serializable] -#endif public sealed class InterceptorSelector : IInterceptorSelector { public Type ReceivedType { get; private set; } diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/InterceptorSelectorTestCase.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/InterceptorSelectorTestCase.cs index 56d34b7f4b..1a78d1dcad 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/InterceptorSelectorTestCase.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/InterceptorSelectorTestCase.cs @@ -17,9 +17,6 @@ namespace Castle.DynamicProxy.Tests using System; using System.Collections.Generic; using System.Reflection; -#if FEATURE_SERIALIZATION - using System.Xml.Serialization; -#endif using Castle.DynamicProxy.Generators; using Castle.DynamicProxy.Internal; @@ -342,9 +339,6 @@ public interface IMultiGenericInterface T2 Method(T1 p); } -#if FEATURE_SERIALIZATION - [Serializable] -#endif public class GenericClass : IGenericInterface { #region IGenericInterface Members @@ -375,9 +369,6 @@ public int Age } } -#if FEATURE_SERIALIZATION - [Serializable] -#endif internal class TypeInterceptorSelector : IInterceptorSelector where TInterceptor : IInterceptor { #region IInterceptorSelector Members @@ -398,9 +389,6 @@ public IInterceptor[] SelectInterceptors(Type type, MethodInfo method, IIntercep #endregion } -#if FEATURE_SERIALIZATION - [Serializable] -#endif public class AllInterceptorSelector : IInterceptorSelector { #region IInterceptorSelector Members @@ -413,9 +401,6 @@ public IInterceptor[] SelectInterceptors(Type type, MethodInfo method, IIntercep #endregion } -#if FEATURE_SERIALIZATION - [Serializable] -#endif public class SelectorWithState : IInterceptorSelector { private readonly int state; @@ -458,9 +443,6 @@ protected bool Equals(SelectorWithState other) } } -#if FEATURE_SERIALIZATION - [Serializable] -#endif public class SimpleClass : ISimpleInterface { #region ISimpleInterface Members @@ -484,22 +466,12 @@ public class FakeProxy public static ProxyGenerationOptions proxyGenerationOptions; public static MethodInfo token_Do; -#if FEATURE_SERIALIZATION - [XmlIgnore] -#endif public IInterceptor[] __interceptors; public IInterceptorSelector __selector; -#if FEATURE_SERIALIZATION - [XmlIgnore] -#endif public SimpleClass __target; -#if FEATURE_SERIALIZATION - [NonSerialized] - [XmlIgnore] -#endif public IInterceptor[] interceptors_Do; public virtual int Do() diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/Interceptors/AddTwoInterceptor.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/Interceptors/AddTwoInterceptor.cs index 8fc7ab9f14..3436d5dc33 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/Interceptors/AddTwoInterceptor.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/Interceptors/AddTwoInterceptor.cs @@ -16,9 +16,6 @@ namespace Castle.DynamicProxy.Tests.Interceptors { using System; -#if FEATURE_SERIALIZATION - [Serializable] -#endif public class AddTwoInterceptor : IInterceptor { #region IInterceptor Members diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/Interceptors/CallCountingInterceptor.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/Interceptors/CallCountingInterceptor.cs index dd0cc2cb21..34782e8ecd 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/Interceptors/CallCountingInterceptor.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/Interceptors/CallCountingInterceptor.cs @@ -16,9 +16,6 @@ namespace Castle.DynamicProxy.Tests.Interceptors { using System; -#if FEATURE_SERIALIZATION - [Serializable] -#endif public class CallCountingInterceptor : IInterceptor { private int count; diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/Interfaces/IEmpty.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/Interfaces/IEmpty.cs index b634ef1ba7..2d9bad1f8c 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/Interfaces/IEmpty.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/Interfaces/IEmpty.cs @@ -20,9 +20,6 @@ public interface IEmpty { } -#if FEATURE_SERIALIZATION - [Serializable] -#endif public class Empty : IEmpty { } diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/Interfaces/IGenericWithRefOut.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/Interfaces/IGenericWithRefOut.cs index ce8fd8d3eb..01c27b8d8a 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/Interfaces/IGenericWithRefOut.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/Interfaces/IGenericWithRefOut.cs @@ -22,9 +22,6 @@ public interface IGenericWithRefOut void Did(ref T i); } -#if FEATURE_SERIALIZATION - [Serializable] -#endif public class GenericWithRefOut : IGenericWithRefOut { public void Do(out T i) diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/MixinTestCase.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/MixinTestCase.cs index 4b721dcb52..f6d77438e1 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/MixinTestCase.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/MixinTestCase.cs @@ -52,9 +52,6 @@ public interface IMyInterface int Calc(int x, int y, int z, Single k); } -#if FEATURE_SERIALIZATION - [Serializable] -#endif public class MyInterfaceImpl : IMyInterface { private String _name; diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/Mixins/ClassImplementingIDerivedSImpleMixin.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/Mixins/ClassImplementingIDerivedSImpleMixin.cs index a4ef39db2f..21bfb1eb32 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/Mixins/ClassImplementingIDerivedSImpleMixin.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/Mixins/ClassImplementingIDerivedSImpleMixin.cs @@ -16,9 +16,6 @@ namespace Castle.DynamicProxy.Tests.Mixins { using System; -#if FEATURE_SERIALIZATION - [Serializable] -#endif public class ClassImplementingIDerivedSimpleMixin : IDerivedSimpleMixin { public int DoSomething() diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/Mixins/ClassImplementingISimpleMixin.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/Mixins/ClassImplementingISimpleMixin.cs index e762239db2..3a9bea7b84 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/Mixins/ClassImplementingISimpleMixin.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/Mixins/ClassImplementingISimpleMixin.cs @@ -16,9 +16,6 @@ namespace Castle.DynamicProxy.Tests.Mixins { using System; -#if FEATURE_SERIALIZATION - [Serializable] -#endif public class ClassImplementingISimpleMixin : ISimpleMixin { public int DoSomething() diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/Mixins/ComplexMixin.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/Mixins/ComplexMixin.cs index bc7175e6c9..a09f242a1a 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/Mixins/ComplexMixin.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/Mixins/ComplexMixin.cs @@ -31,9 +31,6 @@ public interface IThird : ISecond void DoThird(); } -#if FEATURE_SERIALIZATION - [Serializable] -#endif public class ComplexMixin : IThird { public ComplexMixin() diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/Mixins/ConstructorArgumentsOrderWithMixinsTestCase.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/Mixins/ConstructorArgumentsOrderWithMixinsTestCase.cs index 40cf53f0fa..807fe72e62 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/Mixins/ConstructorArgumentsOrderWithMixinsTestCase.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/Mixins/ConstructorArgumentsOrderWithMixinsTestCase.cs @@ -39,9 +39,6 @@ public interface IFoo1 { } -#if FEATURE_SERIALIZATION - [Serializable] -#endif public class Foo : IFoo1 { } @@ -50,9 +47,6 @@ public interface IFoo2 { } -#if FEATURE_SERIALIZATION - [Serializable] -#endif public class Bar : IFoo2 { } @@ -61,9 +55,6 @@ public interface IFoo3 { } -#if FEATURE_SERIALIZATION - [Serializable] -#endif public class Baz : IFoo3 { } diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/Mixins/OtherMixin.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/Mixins/OtherMixin.cs index ea73f8b475..8b6400d9f7 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/Mixins/OtherMixin.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/Mixins/OtherMixin.cs @@ -17,9 +17,6 @@ public interface IOtherMixin int Sum(int x, int y); } -#if FEATURE_SERIALIZATION - [Serializable] -#endif public class OtherMixin : IOtherMixin { public OtherMixin() diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/Mixins/SimpleMixin.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/Mixins/SimpleMixin.cs index 7d40c0fac0..baf23e1ef4 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/Mixins/SimpleMixin.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/Mixins/SimpleMixin.cs @@ -21,9 +21,6 @@ public interface ISimpleMixin int DoSomething(); } -#if FEATURE_SERIALIZATION - [Serializable] -#endif public class SimpleMixin : ISimpleMixin { public SimpleMixin() diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/Mixins/SortingIssueOnMixin.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/Mixins/SortingIssueOnMixin.cs index 4d0a53beca..7c68d50ea8 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/Mixins/SortingIssueOnMixin.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/Mixins/SortingIssueOnMixin.cs @@ -25,17 +25,11 @@ public interface IDomainsAs void DoSomethingElse(); } -#if FEATURE_SERIALIZATION - [Serializable] -#endif public class DomainWithMixin : IDomainWithMixin { public void DoSomething() { } } -#if FEATURE_SERIALIZATION - [Serializable] -#endif public class DomainsAs : IDomainsAs { public void DoSomethingElse() { } diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/OutRefParamsTestCase.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/OutRefParamsTestCase.cs index b1ff5dc61d..44818c60bb 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/OutRefParamsTestCase.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/OutRefParamsTestCase.cs @@ -29,9 +29,6 @@ public interface IClassHasMethodThrowException int MethodWithRefParam(ref int refParam, out string outParam); } -#if FEATURE_SERIALIZATION - [Serializable] -#endif public class ClassHasMethodThrowException : IClassHasMethodThrowException { public virtual int MethodWithRefParam(ref int refParam, out string outParam) diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/ProxyNothingHook.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/ProxyNothingHook.cs index dc18003344..e8c373fced 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/ProxyNothingHook.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/ProxyNothingHook.cs @@ -17,9 +17,6 @@ namespace Castle.DynamicProxy.Tests using System; using System.Reflection; -#if FEATURE_SERIALIZATION - [Serializable] -#endif public class ProxyNothingHook : IProxyGenerationHook { public bool ShouldInterceptMethod(Type type, MethodInfo methodInfo) diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/ProxyTypeCachingWithDifferentHooksTestCase.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/ProxyTypeCachingWithDifferentHooksTestCase.cs index a804a68594..88aac34dfb 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/ProxyTypeCachingWithDifferentHooksTestCase.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/ProxyTypeCachingWithDifferentHooksTestCase.cs @@ -24,9 +24,6 @@ namespace Castle.DynamicProxy.Tests [TestFixture] public class ProxyTypeCachingWithDifferentHooksTestCase : BasePEVerifyTestCase { -#if FEATURE_SERIALIZATION - [Serializable] -#endif public class CustomHook : AllMethodsHook { } [Test] diff --git a/src/Castle.Core/DynamicProxy/AllMethodsHook.cs b/src/Castle.Core/DynamicProxy/AllMethodsHook.cs index f367493274..6cf830f484 100644 --- a/src/Castle.Core/DynamicProxy/AllMethodsHook.cs +++ b/src/Castle.Core/DynamicProxy/AllMethodsHook.cs @@ -18,9 +18,6 @@ namespace Castle.DynamicProxy using System.Collections.Generic; using System.Reflection; -#if FEATURE_SERIALIZATION - [Serializable] -#endif public class AllMethodsHook : IProxyGenerationHook { protected static readonly ICollection SkippedTypes = new[] diff --git a/src/Castle.Core/DynamicProxy/Generators/CacheKey.cs b/src/Castle.Core/DynamicProxy/Generators/CacheKey.cs index ee2a8f9c94..833b933f1f 100644 --- a/src/Castle.Core/DynamicProxy/Generators/CacheKey.cs +++ b/src/Castle.Core/DynamicProxy/Generators/CacheKey.cs @@ -17,9 +17,6 @@ namespace Castle.DynamicProxy.Generators using System; using System.Reflection; -#if FEATURE_SERIALIZATION - [Serializable] -#endif internal class CacheKey { private readonly MemberInfo target; diff --git a/src/Castle.Core/DynamicProxy/ProxyGenerationOptions.cs b/src/Castle.Core/DynamicProxy/ProxyGenerationOptions.cs index 1e06d3720d..dce7127b33 100644 --- a/src/Castle.Core/DynamicProxy/ProxyGenerationOptions.cs +++ b/src/Castle.Core/DynamicProxy/ProxyGenerationOptions.cs @@ -18,9 +18,6 @@ namespace Castle.DynamicProxy using System.Collections.Generic; using System.Reflection; using System.Reflection.Emit; -#if FEATURE_SERIALIZATION - using System.Runtime.Serialization; -#endif using Castle.Core.Internal; using Castle.DynamicProxy.Internal; @@ -36,22 +33,13 @@ namespace Castle.DynamicProxy /// used to create a proxy (or proxy type). /// /// -#if FEATURE_SERIALIZATION - [Serializable] -#endif public class ProxyGenerationOptions -#if FEATURE_SERIALIZATION - : ISerializable -#endif { public static readonly ProxyGenerationOptions Default = new ProxyGenerationOptions(); private List mixins; private readonly IList additionalAttributes = new List(); -#if FEATURE_SERIALIZATION - [NonSerialized] -#endif private MixinData mixinData; // this is calculated dynamically on proxy type creation /// @@ -72,16 +60,6 @@ public ProxyGenerationOptions() { } -#if FEATURE_SERIALIZATION - private ProxyGenerationOptions(SerializationInfo info, StreamingContext context) - { - Hook = (IProxyGenerationHook)info.GetValue("hook", typeof(IProxyGenerationHook)); - Selector = (IInterceptorSelector)info.GetValue("selector", typeof(IInterceptorSelector)); - mixins = (List)info.GetValue("mixins", typeof(List)); - BaseTypeForInterfaceProxy = Type.GetType(info.GetString("baseTypeForInterfaceProxy.AssemblyQualifiedName")); - } -#endif - public void Initialize() { if (mixinData == null) @@ -98,16 +76,6 @@ public void Initialize() } } -#if FEATURE_SERIALIZATION - public void GetObjectData(SerializationInfo info, StreamingContext context) - { - info.AddValue("hook", Hook); - info.AddValue("selector", Selector); - info.AddValue("mixins", mixins); - info.AddValue("baseTypeForInterfaceProxy.AssemblyQualifiedName", BaseTypeForInterfaceProxy.AssemblyQualifiedName); - } -#endif - /// /// Gets or sets the that should be used during proxy type /// generation. Defaults to an instance of . diff --git a/src/Castle.Core/DynamicProxy/StandardInterceptor.cs b/src/Castle.Core/DynamicProxy/StandardInterceptor.cs index 0ccb24d86e..ce2edaeea8 100644 --- a/src/Castle.Core/DynamicProxy/StandardInterceptor.cs +++ b/src/Castle.Core/DynamicProxy/StandardInterceptor.cs @@ -16,9 +16,6 @@ namespace Castle.DynamicProxy { using System; -#if FEATURE_SERIALIZATION - [Serializable] -#endif public class StandardInterceptor : IInterceptor { public void Intercept(IInvocation invocation) From 69f768707931e40737e535c4a6303528c7c44360 Mon Sep 17 00:00:00 2001 From: stakx Date: Fri, 12 Jun 2020 19:47:10 +0200 Subject: [PATCH 09/10] Update `ref/` contracts --- ref/Castle.Core-net45.cs | 29 +---------------------------- ref/Castle.Core-netstandard2.0.cs | 1 - ref/Castle.Core-netstandard2.1.cs | 1 - 3 files changed, 1 insertion(+), 30 deletions(-) diff --git a/ref/Castle.Core-net45.cs b/ref/Castle.Core-net45.cs index 8d714e94e1..2b954e485e 100644 --- a/ref/Castle.Core-net45.cs +++ b/ref/Castle.Core-net45.cs @@ -2639,7 +2639,6 @@ public class ModuleScope public string StrongNamedModuleName { get; } public string WeakNamedModuleDirectory { get; } public string WeakNamedModuleName { get; } - public void LoadAssemblyIntoCache(System.Reflection.Assembly assembly) { } public string SaveAssembly() { } public string SaveAssembly(bool strongNamed) { } public static byte[] GetKeyPair() { } @@ -2650,7 +2649,7 @@ public class PersistentProxyBuilder : Castle.DynamicProxy.DefaultProxyBuilder public string SaveAssembly() { } } public class ProxyGenerationException : System.Exception { } - public class ProxyGenerationOptions : System.Runtime.Serialization.ISerializable + public class ProxyGenerationOptions { public static readonly Castle.DynamicProxy.ProxyGenerationOptions Default; public ProxyGenerationOptions() { } @@ -2666,7 +2665,6 @@ public class ProxyGenerationOptions : System.Runtime.Serialization.ISerializable public void AddMixinInstance(object instance) { } public override bool Equals(object obj) { } public override int GetHashCode() { } - public void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { } public void Initialize() { } public object[] MixinsAsArray() { } } @@ -2794,30 +2792,5 @@ public static class TypeUtil { public static System.Type[] GetAllInterfaces(this System.Type type) { } public static System.Type GetTypeOrNull(object target) { } - public static System.Reflection.MemberInfo[] Sort(System.Reflection.MemberInfo[] members) { } - } -} -namespace Castle.DynamicProxy.Serialization -{ - [System.AttributeUsage(System.AttributeTargets.Assembly | System.AttributeTargets.All, AllowMultiple=false)] - [System.CLSCompliant(false)] - public class CacheMappingsAttribute : System.Attribute - { - public CacheMappingsAttribute(byte[] serializedCacheMappings) { } - public byte[] SerializedCacheMappings { get; } - } - public class ProxyObjectReference : System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.IObjectReference, System.Runtime.Serialization.ISerializable - { - protected ProxyObjectReference(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { } - public static Castle.DynamicProxy.ModuleScope ModuleScope { get; } - public void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { } - public object GetRealObject(System.Runtime.Serialization.StreamingContext context) { } - protected void InvokeCallback(object target) { } - public void OnDeserialization(object sender) { } - public object RecreateClassProxy() { } - public object RecreateInterfaceProxy(string generatorType) { } - protected virtual object RecreateProxy() { } - public static void ResetScope() { } - public static void SetScope(Castle.DynamicProxy.ModuleScope scope) { } } } \ No newline at end of file diff --git a/ref/Castle.Core-netstandard2.0.cs b/ref/Castle.Core-netstandard2.0.cs index 78891ae052..33df26142f 100644 --- a/ref/Castle.Core-netstandard2.0.cs +++ b/ref/Castle.Core-netstandard2.0.cs @@ -2763,6 +2763,5 @@ public static class TypeUtil { public static System.Type[] GetAllInterfaces(this System.Type type) { } public static System.Type GetTypeOrNull(object target) { } - public static System.Reflection.MemberInfo[] Sort(System.Reflection.MemberInfo[] members) { } } } \ No newline at end of file diff --git a/ref/Castle.Core-netstandard2.1.cs b/ref/Castle.Core-netstandard2.1.cs index 1bce4a0b95..0f27214f8e 100644 --- a/ref/Castle.Core-netstandard2.1.cs +++ b/ref/Castle.Core-netstandard2.1.cs @@ -2763,6 +2763,5 @@ public static class TypeUtil { public static System.Type[] GetAllInterfaces(this System.Type type) { } public static System.Type GetTypeOrNull(object target) { } - public static System.Reflection.MemberInfo[] Sort(System.Reflection.MemberInfo[] members) { } } } \ No newline at end of file From a1dac2c26995598277e21326364a671be2241112 Mon Sep 17 00:00:00 2001 From: stakx Date: Fri, 12 Jun 2020 20:19:43 +0200 Subject: [PATCH 10/10] Update the changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a1595c8378..19f98a1112 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,10 @@ Deprecations: - Removed support for the .NET Framework < 4.5 and .NET Standard 1.x. (@stakx, #495, #496) - Removed support for Code Access Security (CAS). (@stakx, #502) - Removed support for Remoting. This library no longer defines any types deriving from `MarshalByRefObject`, and `ProxyUtil.IsProxy` (which used to recognize remoting/"transparent" proxies) now tests only for DynamicProxy proxies. (@stakx, #507) + - Removed support for serialization in DynamicProxy. To be more specific, this means that: + - Previously saved dynamic assemblies can no longer be loaded into a `ProxyGenerator`'s type cache (via `ModuleScope.LoadAssemblyIntoCache`). + - Generated proxies and invocations are no longer marked as `[Serializable]` nor do they automatically implement `ISerializable`. + - There is no longer any requirement to make your interceptors, interceptor selectors, proxy generation hooks, or mixins serializable. (@stakx, #512) - The following public members have been removed: - `Castle.Core.Internal.Lock` (class) along with all related types and methods - `Castle.Core.Internal.PermissionUtil.IsGranted` (method)