@@ -24,6 +24,12 @@ internal interface IInternal
24
24
{
25
25
int Id { get ; }
26
26
}
27
+
28
+ public interface IWithEqualsAndGetHashCode
29
+ {
30
+ bool Equals ( object that ) ;
31
+ int GetHashCode ( ) ;
32
+ }
27
33
28
34
[ Serializable ]
29
35
public class InternalInterfaceTestClass : IInternal
@@ -70,6 +76,30 @@ public PublicExplicitInterfaceTestClass()
70
76
}
71
77
}
72
78
79
+ [ Serializable ]
80
+ public class PublicExplicitInterfaceWithSameMembersTestClass : IPublic
81
+ {
82
+ public virtual int Id { get ; set ; }
83
+ public virtual string Name { get ; set ; }
84
+
85
+ int IPublic . Id { get ; set ; }
86
+ string IPublic . Name { get ; set ; }
87
+
88
+ public PublicExplicitInterfaceWithSameMembersTestClass ( )
89
+ {
90
+ // Check access to properties from the default constructor do not fail once proxified
91
+ Id = - 1 ;
92
+ Assert . That ( Id , Is . EqualTo ( - 1 ) ) ;
93
+ Name = "Unknown" ;
94
+ Assert . That ( Name , Is . EqualTo ( "Unknown" ) ) ;
95
+ IPublic pub = this ;
96
+ pub . Id = - 2 ;
97
+ Assert . That ( pub . Id , Is . EqualTo ( - 2 ) ) ;
98
+ pub . Name = "Unknown2" ;
99
+ Assert . That ( pub . Name , Is . EqualTo ( "Unknown2" ) ) ;
100
+ }
101
+ }
102
+
73
103
[ Serializable ]
74
104
public abstract class AbstractTestClass : IPublic
75
105
{
@@ -218,6 +248,39 @@ public void VerifyProxyForClassWithAdditionalInterface()
218
248
#endif
219
249
}
220
250
251
+ [ Test ]
252
+ public void VerifyProxyForInterfaceWithEqualsAndGetHashCode ( )
253
+ {
254
+ var factory = new StaticProxyFactory ( ) ;
255
+ factory . PostInstantiate (
256
+ typeof ( IWithEqualsAndGetHashCode ) . FullName ,
257
+ typeof ( object ) ,
258
+ new HashSet < System . Type > { typeof ( IWithEqualsAndGetHashCode ) , typeof ( INHibernateProxy ) } ,
259
+ null , null , null , false ) ;
260
+
261
+ #if NETFX
262
+ VerifyGeneratedAssembly (
263
+ ( ) =>
264
+ {
265
+ #endif
266
+ var proxy = factory . GetProxy ( 1 , null ) ;
267
+ Assert . That ( proxy , Is . Not . Null ) ;
268
+ Assert . That ( proxy , Is . InstanceOf < IWithEqualsAndGetHashCode > ( ) ) ;
269
+ var proxyType = proxy . GetType ( ) ;
270
+ var proxyMap = proxyType . GetInterfaceMap ( typeof ( IWithEqualsAndGetHashCode ) ) ;
271
+ Assert . That (
272
+ proxyMap . TargetMethods ,
273
+ Has . One . Property ( "Name" ) . EqualTo ( "Equals" ) . And . Property ( "IsPublic" ) . EqualTo ( true ) ,
274
+ "Equals is not implicitly implemented" ) ;
275
+ Assert . That (
276
+ proxyMap . TargetMethods ,
277
+ Has . One . Property ( "Name" ) . EqualTo ( "GetHashCode" ) . And . Property ( "IsPublic" ) . EqualTo ( true ) ,
278
+ "GetHashCode is not implicitly implemented" ) ;
279
+ #if NETFX
280
+ } ) ;
281
+ #endif
282
+ }
283
+
221
284
[ Test ]
222
285
public void VerifyProxyForClassWithInterface ( )
223
286
{
@@ -237,12 +300,30 @@ public void VerifyProxyForClassWithInterface()
237
300
Assert . That ( proxy , Is . Not . Null ) ;
238
301
Assert . That ( proxy , Is . InstanceOf < IPublic > ( ) ) ;
239
302
Assert . That ( proxy , Is . InstanceOf < PublicInterfaceTestClass > ( ) ) ;
303
+ var proxyType = proxy . GetType ( ) ;
304
+ var proxyMap = proxyType . GetInterfaceMap ( typeof ( IPublic ) ) ;
305
+ Assert . That (
306
+ proxyMap . TargetMethods ,
307
+ Has . One . EqualTo ( proxyType . GetProperty ( nameof ( PublicInterfaceTestClass . Name ) ) . GetMethod ) ,
308
+ "Name getter does not implement IPublic" ) ;
309
+ Assert . That (
310
+ proxyMap . TargetMethods ,
311
+ Has . One . EqualTo ( proxyType . GetProperty ( nameof ( PublicInterfaceTestClass . Name ) ) . SetMethod ) ,
312
+ "Name setter does not implement IPublic" ) ;
313
+ Assert . That (
314
+ proxyMap . TargetMethods ,
315
+ Has . One . EqualTo ( proxyType . GetProperty ( nameof ( PublicInterfaceTestClass . Id ) ) . GetMethod ) ,
316
+ "Id setter does not implement IPublic" ) ;
317
+ Assert . That (
318
+ proxyMap . TargetMethods ,
319
+ Has . One . EqualTo ( proxyType . GetProperty ( nameof ( PublicInterfaceTestClass . Id ) ) . SetMethod ) ,
320
+ "Id setter does not implement IPublic" ) ;
240
321
241
322
// Check interface and implicit implementations do both call the delegated state
242
323
var state = new PublicInterfaceTestClass { Id = 5 , Name = "State" } ;
243
324
proxy . HibernateLazyInitializer . SetImplementation ( state ) ;
244
- var pub = ( IPublic ) proxy ;
245
325
var ent = ( PublicInterfaceTestClass ) proxy ;
326
+ IPublic pub = ent ;
246
327
Assert . That ( pub . Id , Is . EqualTo ( 5 ) , "IPublic.Id" ) ;
247
328
Assert . That ( ent . Id , Is . EqualTo ( 5 ) , "entity.Id" ) ;
248
329
Assert . That ( pub . Name , Is . EqualTo ( "State" ) , "IPublic.Name" ) ;
@@ -276,16 +357,21 @@ public void VerifyProxyForClassWithExplicitInterface()
276
357
Assert . That ( proxy , Is . Not . Null ) ;
277
358
Assert . That ( proxy , Is . InstanceOf < IPublic > ( ) ) ;
278
359
Assert . That ( proxy , Is . InstanceOf < PublicExplicitInterfaceTestClass > ( ) ) ;
279
-
280
- // Check interface and implicit implementations do both call the delegated state
360
+ var proxyType = proxy . GetType ( ) ;
361
+ Assert . That ( proxyType . GetMethod ( $ "get_{ nameof ( IPublic . Name ) } ") , Is . Null , "get Name is implicitly implemented" ) ;
362
+ Assert . That ( proxyType . GetMethod ( $ "set_{ nameof ( IPublic . Name ) } ") , Is . Null , "set Name is implicitly implemented" ) ;
363
+ Assert . That ( proxyType . GetMethod ( $ "get_{ nameof ( IPublic . Id ) } ") , Is . Null , "get Id is implicitly implemented" ) ;
364
+ Assert . That ( proxyType . GetMethod ( $ "set_{ nameof ( IPublic . Id ) } ") , Is . Null , "set Id is implicitly implemented" ) ;
365
+
366
+ // Check explicit implementation
281
367
IPublic state = new PublicExplicitInterfaceTestClass ( ) ;
282
368
state . Id = 5 ;
283
369
state . Name = "State" ;
284
370
proxy . HibernateLazyInitializer . SetImplementation ( state ) ;
285
371
var entity = ( IPublic ) proxy ;
286
372
Assert . That ( entity . Id , Is . EqualTo ( 5 ) , "Id" ) ;
287
373
Assert . That ( entity . Name , Is . EqualTo ( "State" ) , "Name" ) ;
288
-
374
+
289
375
entity . Id = 10 ;
290
376
entity . Name = "Test" ;
291
377
Assert . That ( entity . Id , Is . EqualTo ( 10 ) , "entity.Id" ) ;
@@ -297,6 +383,75 @@ public void VerifyProxyForClassWithExplicitInterface()
297
383
#endif
298
384
}
299
385
386
+ [ Test ]
387
+ public void VerifyProxyForClassWithExplicitInterfaceWithSameMembers ( )
388
+ {
389
+ var factory = new StaticProxyFactory ( ) ;
390
+ factory . PostInstantiate (
391
+ typeof ( PublicExplicitInterfaceWithSameMembersTestClass ) . FullName ,
392
+ typeof ( PublicExplicitInterfaceWithSameMembersTestClass ) ,
393
+ new HashSet < System . Type > { typeof ( INHibernateProxy ) } ,
394
+ null , null , null , true ) ;
395
+ #if NETFX
396
+ VerifyGeneratedAssembly (
397
+ ( ) =>
398
+ {
399
+ #endif
400
+ var proxy = factory . GetProxy ( 1 , null ) ;
401
+ Assert . That ( proxy , Is . Not . Null ) ;
402
+ Assert . That ( proxy , Is . InstanceOf < IPublic > ( ) ) ;
403
+ Assert . That ( proxy , Is . InstanceOf < PublicExplicitInterfaceWithSameMembersTestClass > ( ) ) ;
404
+ var proxyType = proxy . GetType ( ) ;
405
+ var proxyMap = proxyType . GetInterfaceMap ( typeof ( IPublic ) ) ;
406
+ Assert . That (
407
+ proxyMap . TargetMethods ,
408
+ Has . None . EqualTo ( proxyType . GetProperty ( nameof ( PublicExplicitInterfaceWithSameMembersTestClass . Name ) ) . GetMethod ) ,
409
+ "class Name getter does implement IPublic" ) ;
410
+ Assert . That (
411
+ proxyMap . TargetMethods ,
412
+ Has . None . EqualTo ( proxyType . GetProperty ( nameof ( PublicExplicitInterfaceWithSameMembersTestClass . Name ) ) . SetMethod ) ,
413
+ "class Name setter does implement IPublic" ) ;
414
+ Assert . That (
415
+ proxyMap . TargetMethods ,
416
+ Has . None . EqualTo ( proxyType . GetProperty ( nameof ( PublicExplicitInterfaceWithSameMembersTestClass . Id ) ) . GetMethod ) ,
417
+ "class Id setter does implement IPublic" ) ;
418
+ Assert . That (
419
+ proxyMap . TargetMethods ,
420
+ Has . None . EqualTo ( proxyType . GetProperty ( nameof ( PublicExplicitInterfaceWithSameMembersTestClass . Id ) ) . SetMethod ) ,
421
+ "class Id setter does implement IPublic" ) ;
422
+
423
+ // Check interface and implicit implementations do both call the delegated state
424
+ var state = new PublicExplicitInterfaceWithSameMembersTestClass ( ) ;
425
+ IPublic pubState = state ;
426
+ state . Id = 5 ;
427
+ state . Name = "State" ;
428
+ pubState . Id = 10 ;
429
+ pubState . Name = "State2" ;
430
+ proxy . HibernateLazyInitializer . SetImplementation ( state ) ;
431
+ var entity = ( PublicExplicitInterfaceWithSameMembersTestClass ) proxy ;
432
+ IPublic pubEntity = entity ;
433
+ Assert . That ( entity . Id , Is . EqualTo ( 5 ) , "Id member" ) ;
434
+ Assert . That ( entity . Name , Is . EqualTo ( "State" ) , "Name member" ) ;
435
+ Assert . That ( pubEntity . Id , Is . EqualTo ( 10 ) , "Id from interface" ) ;
436
+ Assert . That ( pubEntity . Name , Is . EqualTo ( "State2" ) , "Name from interface" ) ;
437
+
438
+ entity . Id = 15 ;
439
+ entity . Name = "Test" ;
440
+ pubEntity . Id = 20 ;
441
+ pubEntity . Name = "Test2" ;
442
+ Assert . That ( entity . Id , Is . EqualTo ( 15 ) , "entity.Id" ) ;
443
+ Assert . That ( state . Id , Is . EqualTo ( 15 ) , "state.Id" ) ;
444
+ Assert . That ( entity . Name , Is . EqualTo ( "Test" ) , "entity.Name" ) ;
445
+ Assert . That ( state . Name , Is . EqualTo ( "Test" ) , "state.Name" ) ;
446
+ Assert . That ( pubEntity . Id , Is . EqualTo ( 20 ) , "pubEntity.Id" ) ;
447
+ Assert . That ( pubState . Id , Is . EqualTo ( 20 ) , "pubState.Id" ) ;
448
+ Assert . That ( pubEntity . Name , Is . EqualTo ( "Test2" ) , "pubEntity.Name" ) ;
449
+ Assert . That ( pubState . Name , Is . EqualTo ( "Test2" ) , "pubState.Name" ) ;
450
+ #if NETFX
451
+ } ) ;
452
+ #endif
453
+ }
454
+
300
455
[ Test ]
301
456
public void VerifyProxyForRefOutClass ( )
302
457
{
0 commit comments