User: xtoff
Date: 2009/10/31 03:31 PM

Added:
 /DynamicProxy/trunk/src/Castle.DynamicProxy.Tests/
  InterfaceProxyBaseTypeTestCase.cs
 /DynamicProxy/trunk/src/Castle.DynamicProxy/Serialization/
  ProxyTypeConstants.cs

Modified:
 /DynamicProxy/trunk/src/Castle.DynamicProxy.Tests/
  Castle.DynamicProxy.Tests-vs2008.csproj, SerializableClassTestCase.cs
 /DynamicProxy/trunk/src/Castle.DynamicProxy/
  Castle.DynamicProxy-vs2008.csproj
 /DynamicProxy/trunk/src/Castle.DynamicProxy/Contributors/
  ClassProxyInstanceContributor.cs, InterfaceProxyInstanceContributor.cs, 
ProxyInstanceContributor.cs
 /DynamicProxy/trunk/src/Castle.DynamicProxy/Generators/
  InterfaceMethodGenerator.cs, InterfaceProxyWithTargetGenerator.cs, 
InterfaceProxyWithTargetInterfaceGenerator.cs, 
InterfaceProxyWithoutTargetGenerator.cs
 /DynamicProxy/trunk/src/Castle.DynamicProxy/Serialization/
  ProxyObjectReference.cs

Log:
 - Fixed issues with serialization. I'm not very happy with the fix, but it 
should do for v2.2. When we need more flexibility it will have to be changed.
 - Uncommented ignored test that now passes.

File Changes:

Directory: /DynamicProxy/trunk/src/Castle.DynamicProxy/
=======================================================

File [modified]: Castle.DynamicProxy-vs2008.csproj
Delta lines: +7 -5
===================================================================

--- 
DynamicProxy/trunk/src/Castle.DynamicProxy/Contributors/ClassProxyInstanceContributor.cs
    2009-10-30 13:10:49 UTC (rev 6296)
+++ 
DynamicProxy/trunk/src/Castle.DynamicProxy/Contributors/ClassProxyInstanceContributor.cs
    2009-10-31 22:31:23 UTC (rev 6297)
@@ -19,11 +19,13 @@
        using System.Diagnostics;
        using System.Reflection;
        using System.Runtime.Serialization;
-       using Generators.Emitters;
-       using Generators.Emitters.CodeBuilders;
-       using Generators.Emitters.SimpleAST;
-       using Tokens;
 
+       using Castle.DynamicProxy.Generators.Emitters;
+       using Castle.DynamicProxy.Generators.Emitters.CodeBuilders;
+       using Castle.DynamicProxy.Generators.Emitters.SimpleAST;
+       using Castle.DynamicProxy.Serialization;
+       using Castle.DynamicProxy.Tokens;
+
        public class ClassProxyInstanceContributor : ProxyInstanceContributor
        {
                private readonly bool delegateToBaseGetObjectData;
@@ -32,7 +34,7 @@
                private readonly IList<FieldReference> serializedFields = new 
List<FieldReference>();
 
                public ClassProxyInstanceContributor(Type targetType, 
IList<MethodInfo> methodsToSkip, Type[] interfaces)
-                       : base(targetType, interfaces)
+                       : base(targetType, interfaces,ProxyTypeConstants.Class)
                {
 #if !SILVERLIGHT

Directory: /DynamicProxy/trunk/src/Castle.DynamicProxy.Tests/
=============================================================

File [modified]: Castle.DynamicProxy.Tests-vs2008.csproj
Delta lines: +24 -0
===================================================================

--- 
DynamicProxy/trunk/src/Castle.DynamicProxy.Tests/InterfaceProxyBaseTypeTestCase.cs
                          (rev 0)
+++ 
DynamicProxy/trunk/src/Castle.DynamicProxy.Tests/InterfaceProxyBaseTypeTestCase.cs
  2009-10-31 22:31:23 UTC (rev 6297)
@@ -0,0 +1,24 @@
+// Copyright 2004-2009 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 NUnit.Framework;
+
+       [TestFixture]
+       public class InterfaceProxyBaseTypeTestCase:BasePEVerifyTestCase
+       {
+               //TODO: implement me
+       }
+}

File [added]: InterfaceProxyBaseTypeTestCase.cs
Delta lines: +1 -1
===================================================================

--- 
DynamicProxy/trunk/src/Castle.DynamicProxy.Tests/SerializableClassTestCase.cs   
    2009-10-30 13:10:49 UTC (rev 6296)
+++ 
DynamicProxy/trunk/src/Castle.DynamicProxy.Tests/SerializableClassTestCase.cs   
    2009-10-31 22:31:23 UTC (rev 6297)
@@ -440,7 +440,7 @@
                        Assert.AreSame(ProxyGenerationOptions.Default, 
field.GetValue(proxy));
                }
 
-               [Test, Ignore("This test is currently broken. See 
DYNPROXY-ISSUE-117")]
+               [Test]
                public void 
BaseTypeForInterfaceProxy_is_honored_after_deserialization()
                {

File [modified]: SerializableClassTestCase.cs
Delta lines: +0 -0
===================================================================

Directory: /DynamicProxy/trunk/src/Castle.DynamicProxy/Contributors/
====================================================================

File [modified]: ClassProxyInstanceContributor.cs
Delta lines: +2 -10
===================================================================

--- 
DynamicProxy/trunk/src/Castle.DynamicProxy/Contributors/InterfaceProxyInstanceContributor.cs
        2009-10-30 13:10:49 UTC (rev 6296)
+++ 
DynamicProxy/trunk/src/Castle.DynamicProxy/Contributors/InterfaceProxyInstanceContributor.cs
        2009-10-31 22:31:23 UTC (rev 6297)
@@ -23,17 +23,15 @@
 
        public class InterfaceProxyInstanceContributor : 
ProxyInstanceContributor
        {
-               private readonly InterfaceGeneratorType generatorType;
 
                protected override Expression 
GetTargetReferenceExpression(ClassEmitter emitter)
                {
                        return emitter.GetField("__target").ToExpression();
                }
 
-               public InterfaceProxyInstanceContributor(Type targetType, 
InterfaceGeneratorType generatorType, Type[] interfaces)
-                       : base(targetType, interfaces)
+               public InterfaceProxyInstanceContributor(Type targetType, 
string proxyGeneratorId, Type[] interfaces)
+                       : base(targetType, interfaces,proxyGeneratorId)
                {
-                       this.generatorType = generatorType;
                }
 
 #if !SILVERLIGHT
@@ -49,12 +47,6 @@
                                                                                
        ToExpression())));
 
                        codebuilder.AddStatement(new ExpressionStatement(
-                                                       new 
MethodInvocationExpression(serializationInfo, 
SerializationInfoMethods.AddValue_Int32,
-                                                                               
       new ConstReference("__interface_generator_type").
-                                                                               
        ToExpression(),
-                                                                               
       new ConstReference((int) generatorType).ToExpression())));
-
-                       codebuilder.AddStatement(new ExpressionStatement(
                                                        new 
MethodInvocationExpression(serializationInfo, 
SerializationInfoMethods.AddValue_Object,
                                                                                
       new ConstReference("__theInterface").ToExpression(),

File [modified]: InterfaceProxyInstanceContributor.cs
Delta lines: +12 -1
===================================================================

--- 
DynamicProxy/trunk/src/Castle.DynamicProxy/Contributors/ProxyInstanceContributor.cs
 2009-10-30 13:10:49 UTC (rev 6296)
+++ 
DynamicProxy/trunk/src/Castle.DynamicProxy/Contributors/ProxyInstanceContributor.cs
 2009-10-31 22:31:23 UTC (rev 6297)
@@ -31,11 +31,13 @@
                // TODO: this whole type (and its descendants) should be #if 
!SILVERLIGHT... and empty type should be used instead for SL
 
                protected readonly Type targetType;
+               private readonly string proxyTypeId;
                private readonly Type[] interfaces;
 
-               protected ProxyInstanceContributor(Type targetType, Type[] 
interfaces)
+               protected ProxyInstanceContributor(Type targetType, Type[] 
interfaces,string proxyTypeId)
                {
                        this.targetType = targetType;
+                       this.proxyTypeId = proxyTypeId;
                        this.interfaces = interfaces ?? Type.EmptyTypes;
                }
 
@@ -146,6 +148,15 @@
                                                new 
ConstReference("__proxyGenerationOptions").ToExpression(),
                                                
emitter.GetField("proxyGenerationOptions").ToExpression())));
 
+
+
+                       getObjectData.CodeBuilder.AddStatement(
+                               new ExpressionStatement(
+                                       new 
MethodInvocationExpression(serializationInfo,
+                                                                      
SerializationInfoMethods.AddValue_Object,
+                                                                      new 
ConstReference("__proxyTypeId").ToExpression(),
+                                                                      new 
ConstReference(proxyTypeId).ToExpression())));
+
                        CustomizeGetObjectData(getObjectData.CodeBuilder, 
serializationInfo, streamingContext,emitter);
 

File [modified]: ProxyInstanceContributor.cs
Delta lines: +0 -1
===================================================================

--- 
DynamicProxy/trunk/src/Castle.DynamicProxy/Generators/InterfaceMethodGenerator.cs
   2009-10-30 13:10:49 UTC (rev 6296)
+++ 
DynamicProxy/trunk/src/Castle.DynamicProxy/Generators/InterfaceMethodGenerator.cs
   2009-10-31 22:31:23 UTC (rev 6297)
@@ -127,7 +127,6 @@
                        else
                        {
                                var cctor = @class.ClassConstructor;
-                               // TODO: same here...INamingScope
                                var interfaceMethodToken = 
@class.CreateStaticField(namingScope.GetUniqueName("token_" + 
method.Method.Name),
                                                                                
    typeof(MethodInfo));

Directory: /DynamicProxy/trunk/src/Castle.DynamicProxy/Generators/
==================================================================

File [modified]: InterfaceMethodGenerator.cs
Delta lines: +4 -13
===================================================================

--- 
DynamicProxy/trunk/src/Castle.DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs
  2009-10-30 13:10:49 UTC (rev 6296)
+++ 
DynamicProxy/trunk/src/Castle.DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs
  2009-10-31 22:31:23 UTC (rev 6297)
@@ -23,6 +23,8 @@
        using Castle.Core.Internal;
        using Castle.DynamicProxy.Generators.Emitters;
        using Castle.DynamicProxy.Generators.Emitters.SimpleAST;
+       using Castle.DynamicProxy.Serialization;
+
        using Contributors;
 
        /// <summary>
@@ -170,9 +172,9 @@
 #endif
                }
 
-               protected virtual InterfaceGeneratorType GeneratorType
+               protected virtual string GeneratorType
                {
-                       get { return InterfaceGeneratorType.WithTarget; }
+                       get { return ProxyTypeConstants.InterfaceWithTarget; }
                }
 
                protected virtual bool AllowChangeTarget
@@ -292,15 +294,4 @@
                        return targetInterfaces.Contains(@interface);
                }
        }
-
-       /// <summary>
-       /// This is used by the ProxyObjectReference class during 
de-serialiation, to know
-       /// which generator it should use
-       /// </summary>
-       public enum InterfaceGeneratorType
-       {
-               WithTarget = 1,
-               WithoutTarget = 2,
-               WithTargetInterface = 3
-       }

File [modified]: InterfaceProxyWithTargetGenerator.cs
Delta lines: +5 -2
===================================================================

--- 
DynamicProxy/trunk/src/Castle.DynamicProxy/Generators/InterfaceProxyWithTargetInterfaceGenerator.cs
 2009-10-30 13:10:49 UTC (rev 6296)
+++ 
DynamicProxy/trunk/src/Castle.DynamicProxy/Generators/InterfaceProxyWithTargetInterfaceGenerator.cs
 2009-10-31 22:31:23 UTC (rev 6297)
@@ -16,6 +16,9 @@
 {
        using System;
        using System.Collections.Generic;
+
+       using Castle.DynamicProxy.Serialization;
+
        using Contributors;
        using Emitters;
 
@@ -46,9 +49,9 @@
                        get { return true; }
                }
 
-               protected override InterfaceGeneratorType GeneratorType
+               protected override string GeneratorType
                {
-                       get { return 
InterfaceGeneratorType.WithTargetInterface; }
+                       get { return 
ProxyTypeConstants.InterfaceWithTargetInterface; }
                }
        }

File [modified]: InterfaceProxyWithTargetInterfaceGenerator.cs
Delta lines: +4 -2
===================================================================

--- 
DynamicProxy/trunk/src/Castle.DynamicProxy/Generators/InterfaceProxyWithoutTargetGenerator.cs
       2009-10-30 13:10:49 UTC (rev 6296)
+++ 
DynamicProxy/trunk/src/Castle.DynamicProxy/Generators/InterfaceProxyWithoutTargetGenerator.cs
       2009-10-31 22:31:23 UTC (rev 6297)
@@ -17,6 +17,8 @@
        using System;
        using System.Collections.Generic;
        using Castle.DynamicProxy.Generators.Emitters;
+       using Castle.DynamicProxy.Serialization;
+
        using Contributors;
        using Emitters.SimpleAST;
 
@@ -96,9 +98,9 @@
                }
 
 
-               protected override InterfaceGeneratorType GeneratorType
+               protected override string GeneratorType
                {
-                       get { return InterfaceGeneratorType.WithoutTarget; }
+                       get { return ProxyTypeConstants.InterfaceWithoutTarget; 
}
                }
        }
 }

File [modified]: InterfaceProxyWithoutTargetGenerator.cs
Delta lines: +56 -45
===================================================================

--- 
DynamicProxy/trunk/src/Castle.DynamicProxy/Serialization/ProxyObjectReference.cs
    2009-10-30 13:10:49 UTC (rev 6296)
+++ 
DynamicProxy/trunk/src/Castle.DynamicProxy/Serialization/ProxyObjectReference.cs
    2009-10-31 22:31:23 UTC (rev 6297)
@@ -16,6 +16,8 @@
 namespace Castle.DynamicProxy.Serialization
 {
        using System;
+       using System.Collections.Generic;
+       using System.Diagnostics;
        using System.Reflection;
        using System.Runtime.Serialization;
        using Castle.DynamicProxy;
@@ -103,43 +105,42 @@
 
                protected virtual object RecreateProxy()
                {
-                       if (baseType == typeof (object)) // TODO: replace this 
hack by serializing a flag or something
+                       var generatorType = GetValue<string>("__proxyTypeId");
+                       if (generatorType.Equals(ProxyTypeConstants.Class))
                        {
-                               isInterfaceProxy = true;
-                               return RecreateInterfaceProxy();
-                       }
-                       else
-                       {
                                isInterfaceProxy = false;
                                return RecreateClassProxy();
                        }
+                       isInterfaceProxy = true;
+                       return RecreateInterfaceProxy(generatorType);
                }
 
-               public object RecreateInterfaceProxy()
+               public object RecreateInterfaceProxy(string generatorType)
                {
-                       InterfaceGeneratorType generatorType = 
(InterfaceGeneratorType) info.GetInt32("__interface_generator_type");
 
                        Type theInterface = 
DeserializeTypeFromString("__theInterface");
                        Type targetType = 
DeserializeTypeFromString("__targetFieldType");
 
                        InterfaceProxyWithTargetGenerator generator;
-                       switch (generatorType)
+                       if (generatorType == 
ProxyTypeConstants.InterfaceWithTarget)
                        {
-                               case InterfaceGeneratorType.WithTarget:
-                                       generator = new 
InterfaceProxyWithTargetGenerator(scope, theInterface);
-                                       break;
-                               case InterfaceGeneratorType.WithoutTarget:
-                                       generator = new 
InterfaceProxyWithoutTargetGenerator(scope, theInterface);
-                                       break;
-                               case InterfaceGeneratorType.WithTargetInterface:
-                                       generator = new 
InterfaceProxyWithTargetInterfaceGenerator(scope, theInterface);
-                                       break;
-                               default:
-                                       throw new InvalidOperationException(
-                                               string.Format(
-                                                       "Got value {0} for the 
interface generator type, which is not known for the purpose of serialization.",
-                                                       generatorType));
+                               generator = new 
InterfaceProxyWithTargetGenerator(scope, theInterface);
                        }
+                       else if (generatorType == 
ProxyTypeConstants.InterfaceWithoutTarget)
+                       {
+                               generator = new 
InterfaceProxyWithoutTargetGenerator(scope, theInterface);
+                       }
+                       else if (generatorType == 
ProxyTypeConstants.InterfaceWithTargetInterface)
+                       {
+                               generator = new 
InterfaceProxyWithTargetInterfaceGenerator(scope, theInterface);
+                       }
+                       else
+                       {
+                               throw new InvalidOperationException(
+                                       string.Format(
+                                               "Got value {0} for the 
interface generator type, which is not known for the purpose of serialization.",
+                                               generatorType));
+                       }
 
                        Type proxy_type = generator.GenerateCode(targetType, 
interfaces, proxyGenerationOptions);
                        return 
FormatterServices.GetSafeUninitializedObject(proxy_type);
@@ -147,7 +148,7 @@
 
                public object RecreateClassProxy()
                {
-                       delegateToBase = info.GetBoolean("__delegateToBase");
+                       delegateToBase = GetValue<bool>("__delegateToBase");
 
                        ClassProxyGenerator cpGen = new 
ClassProxyGenerator(scope, baseType);
 
@@ -185,42 +186,47 @@
 
                public void OnDeserialization(object sender)
                {
-                       IInterceptor[] _interceptors = (IInterceptor[]) 
info.GetValue("__interceptors", typeof (IInterceptor[]));
-                       SetInterceptors(_interceptors);
+                       var interceptors = 
GetValue<IInterceptor[]>("__interceptors");
+                       SetInterceptors(interceptors);
 
-                       // mixins
-                       if (proxyGenerationOptions.HasMixins)
-                       {
-                               foreach (Type type in 
proxyGenerationOptions.MixinData.MixinInterfaces)
-                               {
-                                       string mixinFieldName = "__mixin_" + 
type.FullName.Replace(".", "_");
+                       DeserializeProxyMembers();
 
-                                       FieldInfo mixinField = 
proxy.GetType().GetField(mixinFieldName);
-                                       if (mixinField == null)
-                                       {
-                                               throw new 
SerializationException(
-                                                       "The SerializationInfo 
specifies an invalid proxy type, which has no " + mixinFieldName + " field.");
-                                       }
-
-                                       mixinField.SetValue(proxy, 
info.GetValue(mixinFieldName, type));
-                               }
-                       }
-
                        // 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<MemberInfo>();
+                       var deserializedValues = new List<Object>();
+                       for (int 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)
                        {
-                               object target = info.GetValue("__target", 
typeof (object));
+                               var target = GetValue<object>("__target");
                                SetTarget(target);
                        }
                        else if (!delegateToBase)
                        {
-                               object[] baseMemberData = (object[]) 
info.GetValue("__data", typeof (object[]));
+                               var baseMemberData = 
GetValue<object[]>("__data");
                                MemberInfo[] members = 
FormatterServices.GetSerializableMembers(baseType);
                                FormatterServices.PopulateObjectMembers(proxy, 
members, baseMemberData);
                        }
@@ -249,6 +255,11 @@
 
                        interceptorField.SetValue(proxy, interceptors);
                }
+
+               private T GetValue<T>(string name)
+               {
+                       return (T)info.GetValue(name, typeof(T));
+               }
        }
 }
 #endif

Directory: /DynamicProxy/trunk/src/Castle.DynamicProxy/Serialization/
=====================================================================

File [modified]: ProxyObjectReference.cs
Delta lines: +24 -0
===================================================================

--- 
DynamicProxy/trunk/src/Castle.DynamicProxy/Serialization/ProxyTypeConstants.cs  
                            (rev 0)
+++ 
DynamicProxy/trunk/src/Castle.DynamicProxy/Serialization/ProxyTypeConstants.cs  
    2009-10-31 22:31:23 UTC (rev 6297)
@@ -0,0 +1,24 @@
+// Copyright 2004-2009 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 InterfaceWithoutTarget = 
"interface.without.target";
+               public static readonly string InterfaceWithTarget = 
"interface.with.target";
+               public static readonly string InterfaceWithTargetInterface = 
"interface.with.target.interface";
+               public static readonly string Class = "class";
+       }
+}

File [added]: ProxyTypeConstants.cs
Delta lines: +1 -0
===================================================================

--- 
DynamicProxy/trunk/src/Castle.DynamicProxy.Tests/Castle.DynamicProxy.Tests-vs2008.csproj
    2009-10-30 13:10:49 UTC (rev 6296)
+++ 
DynamicProxy/trunk/src/Castle.DynamicProxy.Tests/Castle.DynamicProxy.Tests-vs2008.csproj
    2009-10-31 22:31:23 UTC (rev 6297)
@@ -171,6 +171,7 @@
     <Compile 
Include="InterClasses\IInterfaceWithGenericMethodWithDependentConstraint.cs" />
     <Compile Include="InheritedInterfacesTestCase.cs" />
     <Compile Include="InterceptorSelectorTestCase.cs" />
+    <Compile Include="InterfaceProxyBaseTypeTestCase.cs" />
     <Compile Include="InterfaceProxyWithTargetInterfaceTestCase.cs" />
     <Compile Include="Interfaces\IDecimalOutParam.cs" />


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Castle Project Commits" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/castle-project-commits?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to