@@ -42,6 +42,12 @@ public class PublicInterfaceTestClass : IPublic
4242 public virtual int Id { get ; set ; }
4343 }
4444
45+ [ Serializable ]
46+ public class SimpleTestClass
47+ {
48+ public virtual int Id { get ; set ; }
49+ }
50+
4551 [ Serializable ]
4652 public class CustomSerializationClass : ISerializable
4753 {
@@ -135,6 +141,58 @@ public void VerifyProxyForClassWithAdditionalInterface()
135141#endif
136142 }
137143
144+ [ Test ]
145+ public void InitializedProxyStaysInitializedAfterDeserialization ( )
146+ {
147+ var factory = new StaticProxyFactory ( ) ;
148+ factory . PostInstantiate ( typeof ( SimpleTestClass ) . FullName , typeof ( SimpleTestClass ) , new HashSet < System . Type > { typeof ( INHibernateProxy ) } , null , null , null ) ;
149+ var proxy = factory . GetProxy ( 2 , null ) ;
150+ Assert . That ( proxy , Is . Not . Null , "proxy" ) ;
151+ Assert . That ( NHibernateUtil . IsInitialized ( proxy ) , Is . False , "proxy already initialized after creation" ) ;
152+ Assert . That ( proxy . HibernateLazyInitializer , Is . Not . Null , "HibernateLazyInitializer" ) ;
153+
154+ var impl = new SimpleTestClass { Id = 2 } ;
155+ proxy . HibernateLazyInitializer . SetImplementation ( impl ) ;
156+ Assert . That ( NHibernateUtil . IsInitialized ( proxy ) , Is . True , "proxy not initialized after setting implementation" ) ;
157+
158+ var serializer = GetFormatter ( ) ;
159+ object deserialized ;
160+ using ( var memoryStream = new MemoryStream ( ) )
161+ {
162+ serializer . Serialize ( memoryStream , proxy ) ;
163+ memoryStream . Seek ( 0L , SeekOrigin . Begin ) ;
164+ deserialized = serializer . Deserialize ( memoryStream ) ;
165+ }
166+ Assert . That ( deserialized , Is . Not . Null , "deserialized" ) ;
167+ Assert . That ( deserialized , Is . InstanceOf < INHibernateProxy > ( ) ) ;
168+ Assert . That ( NHibernateUtil . IsInitialized ( deserialized ) , Is . True , "proxy no more initialized after deserialization" ) ;
169+ Assert . That ( deserialized , Is . InstanceOf < SimpleTestClass > ( ) ) ;
170+ Assert . That ( ( ( SimpleTestClass ) deserialized ) . Id , Is . EqualTo ( 2 ) ) ;
171+ }
172+
173+ [ Test ]
174+ public void NonInitializedProxyStaysNonInitializedAfterSerialization ( )
175+ {
176+ var factory = new StaticProxyFactory ( ) ;
177+ factory . PostInstantiate ( typeof ( SimpleTestClass ) . FullName , typeof ( SimpleTestClass ) , new HashSet < System . Type > { typeof ( INHibernateProxy ) } , null , null , null ) ;
178+ var proxy = factory . GetProxy ( 2 , null ) ;
179+ Assert . That ( proxy , Is . Not . Null , "proxy" ) ;
180+ Assert . That ( NHibernateUtil . IsInitialized ( proxy ) , Is . False , "proxy already initialized after creation" ) ;
181+
182+ var serializer = GetFormatter ( ) ;
183+ object deserialized ;
184+ using ( var memoryStream = new MemoryStream ( ) )
185+ {
186+ serializer . Serialize ( memoryStream , proxy ) ;
187+ Assert . That ( NHibernateUtil . IsInitialized ( proxy ) , Is . False , "proxy initialized after serialization" ) ;
188+ memoryStream . Seek ( 0L , SeekOrigin . Begin ) ;
189+ deserialized = serializer . Deserialize ( memoryStream ) ;
190+ }
191+ Assert . That ( deserialized , Is . Not . Null , "deserialized" ) ;
192+ Assert . That ( deserialized , Is . InstanceOf < INHibernateProxy > ( ) ) ;
193+ Assert . That ( NHibernateUtil . IsInitialized ( deserialized ) , Is . False , "proxy initialized after deserialization" ) ;
194+ }
195+
138196 [ Test ]
139197 public void CanSerializeFieldInterceptorProxy ( )
140198 {
0 commit comments