1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.DynamicProxy
.Test
18 using System
.Runtime
.Serialization
;
20 using Castle
.DynamicProxy
.Serialization
;
22 using NUnit
.Framework
;
25 public class ExplicitImplementationOfISerializable
27 private GeneratorContext context
;
28 private ProxyGenerator generator
;
33 context
= new GeneratorContext();
34 generator
= new ProxyGenerator();
35 ProxyObjectReference
.ResetScope();
39 public void CreateClassThatImplementISerializable()
41 MyExplicitISerializable proxy
= (MyExplicitISerializable
)this.generator
.CreateCustomClassProxy(typeof(MyExplicitISerializable
),new StandardInterceptor(), this.context
);
42 Assert
.IsTrue(proxy
.GetType().IsSerializable
);
46 public void SimpleProxySerialization()
48 MyExplicitISerializable proxy
= (MyExplicitISerializable
)
49 this.generator
.CreateCustomClassProxy(
50 typeof(MyExplicitISerializable
),new StandardInterceptor(), this.context
);
52 DateTime original
= proxy
.Current
;
53 MyExplicitISerializable serializedProxy
= (MyExplicitISerializable
) SerializableClassTestCase
.SerializeAndDeserialize(proxy
);
54 Assert
.AreEqual(original
, serializedProxy
.Current
);
60 public void ProxySerializationWhileOverridingExplicitInterface()
62 MyExplicitISerializable2 proxy
= (MyExplicitISerializable2
)
63 this.generator
.CreateCustomClassProxy(
64 typeof(MyExplicitISerializable2
),new StandardInterceptor(), this.context
);
66 MyExplicitISerializable2 serializedProxy
= (MyExplicitISerializable2
) SerializableClassTestCase
.SerializeAndDeserialize(proxy
);
67 Assert
.AreEqual(proxy
.Info
, serializedProxy
.Info
);
71 public void ExplicitISerializeDoes_NOT_CallToBaseImplementation()
73 MyExplicitISerializable3 proxy
= (MyExplicitISerializable3
)
74 this.generator
.CreateCustomClassProxy(
75 typeof(MyExplicitISerializable3
),new StandardInterceptor(), this.context
);
77 MyExplicitISerializable3 serializedProxy
= (MyExplicitISerializable3
) SerializableClassTestCase
.SerializeAndDeserialize(proxy
);
78 Assert
.IsFalse( (proxy
.Info
+"_Serialized").Equals( serializedProxy
.Info
) );
85 public class MyExplicitISerializable
87 public DateTime Current
;
89 public MyExplicitISerializable()
91 Current
= DateTime
.Now
;
96 public class MyExplicitISerializable2
: MyExplicitISerializable
, ISerializable
100 public MyExplicitISerializable2()
102 Info
= Current
.ToString();
105 protected MyExplicitISerializable2(SerializationInfo serializationInfo
, StreamingContext streamingContext
)
107 Current
= serializationInfo
.GetDateTime("dt");
108 Info
= serializationInfo
.GetString("info");
111 void ISerializable
.GetObjectData(SerializationInfo info
, StreamingContext context
)
113 info
.AddValue("dt", Current
, typeof(DateTime
));
114 info
.AddValue("info", Info
, typeof(string));
120 public class MyExplicitISerializable3
: MyExplicitISerializable
, ISerializable
124 public MyExplicitISerializable3()
126 Info
= Current
.ToString();
129 protected MyExplicitISerializable3(SerializationInfo serializationInfo
, StreamingContext streamingContext
)
131 Current
= serializationInfo
.GetDateTime("dt");
132 Info
= serializationInfo
.GetString("info");
135 void ISerializable
.GetObjectData(SerializationInfo info
, StreamingContext context
)
137 info
.AddValue("dt", Current
, typeof(DateTime
));
138 info
.AddValue("info", Info
+"_Serialized", typeof(string));