@@ -356,16 +356,28 @@ protected virtual void RegisterIdBagMapping<TElement>(Action<IIdBagPropertiesMap
356356 public void Set < TElement > ( string notVisiblePropertyOrFieldName , Action < ISetPropertiesMapper < TEntity , TElement > > collectionMapping , Action < ICollectionElementRelation < TElement > > mapping )
357357 {
358358 MemberInfo member = GetPropertyOrFieldMatchingNameOrThrow ( notVisiblePropertyOrFieldName ) ;
359- var collectionElementType = member . GetPropertyOrFieldType ( ) . DetermineCollectionElementType ( ) ;
360- if ( ! typeof ( TElement ) . Equals ( collectionElementType ) )
361- {
362- throw new MappingException ( string . Format ( "Wrong collection element type. For the property/field '{0}' of {1} was expected a collection of {2} but was {3}" ,
363- notVisiblePropertyOrFieldName , typeof ( TEntity ) . FullName , typeof ( TElement ) . Name , collectionElementType . Name ) ) ;
364- }
359+ AssertCollectionElementType < TElement > ( notVisiblePropertyOrFieldName , member ) ;
360+
365361 MemberInfo memberOf = member . GetMemberFromReflectedType ( typeof ( TEntity ) ) ;
366362 RegisterSetMapping < TElement > ( collectionMapping , mapping , member , memberOf ) ;
367363 }
368364
365+
366+ private static void AssertCollectionElementType < TElement > ( string propertyOrFieldName , MemberInfo memberInfo )
367+ {
368+ System . Type collectionElementType = memberInfo . GetPropertyOrFieldType ( ) . DetermineCollectionElementType ( ) ;
369+
370+ if ( typeof ( TElement ) != collectionElementType )
371+ {
372+ var message = string . Format (
373+ "Wrong collection element type. For the property/field '{0}' of {1} was expected a generic collection of {2} but was {3}" ,
374+ propertyOrFieldName , typeof ( TEntity ) . FullName , typeof ( TElement ) . Name ,
375+ collectionElementType != null ? collectionElementType . Name : "unknown" ) ;
376+ throw new MappingException ( message ) ;
377+ }
378+ }
379+
380+
369381 public void Set < TElement > ( string notVisiblePropertyOrFieldName , Action < ISetPropertiesMapper < TEntity , TElement > > collectionMapping )
370382 {
371383 Set ( notVisiblePropertyOrFieldName , collectionMapping , x => { } ) ;
@@ -374,12 +386,8 @@ public void Set<TElement>(string notVisiblePropertyOrFieldName, Action<ISetPrope
374386 public void Bag < TElement > ( string notVisiblePropertyOrFieldName , Action < IBagPropertiesMapper < TEntity , TElement > > collectionMapping , Action < ICollectionElementRelation < TElement > > mapping )
375387 {
376388 MemberInfo member = GetPropertyOrFieldMatchingNameOrThrow ( notVisiblePropertyOrFieldName ) ;
377- var collectionElementType = member . GetPropertyOrFieldType ( ) . DetermineCollectionElementType ( ) ;
378- if ( ! typeof ( TElement ) . Equals ( collectionElementType ) )
379- {
380- throw new MappingException ( string . Format ( "Wrong collection element type. For the property/field '{0}' of {1} was expected a collection of {2} but was {3}" ,
381- notVisiblePropertyOrFieldName , typeof ( TEntity ) . FullName , typeof ( TElement ) . Name , collectionElementType . Name ) ) ;
382- }
389+ AssertCollectionElementType < TElement > ( notVisiblePropertyOrFieldName , member ) ;
390+
383391 MemberInfo memberOf = member . GetMemberFromReflectedType ( typeof ( TEntity ) ) ;
384392 RegisterBagMapping < TElement > ( collectionMapping , mapping , member , memberOf ) ;
385393 }
@@ -392,12 +400,8 @@ public void Bag<TElement>(string notVisiblePropertyOrFieldName, Action<IBagPrope
392400 public void List < TElement > ( string notVisiblePropertyOrFieldName , Action < IListPropertiesMapper < TEntity , TElement > > collectionMapping , Action < ICollectionElementRelation < TElement > > mapping )
393401 {
394402 MemberInfo member = GetPropertyOrFieldMatchingNameOrThrow ( notVisiblePropertyOrFieldName ) ;
395- var collectionElementType = member . GetPropertyOrFieldType ( ) . DetermineCollectionElementType ( ) ;
396- if ( ! typeof ( TElement ) . Equals ( collectionElementType ) )
397- {
398- throw new MappingException ( string . Format ( "Wrong collection element type. For the property/field '{0}' of {1} was expected a collection of {2} but was {3}" ,
399- notVisiblePropertyOrFieldName , typeof ( TEntity ) . FullName , typeof ( TElement ) . Name , collectionElementType . Name ) ) ;
400- }
403+ AssertCollectionElementType < TElement > ( notVisiblePropertyOrFieldName , member ) ;
404+
401405 MemberInfo memberOf = member . GetMemberFromReflectedType ( typeof ( TEntity ) ) ;
402406 RegisterListMapping < TElement > ( collectionMapping , mapping , member , memberOf ) ;
403407 }
@@ -435,12 +439,8 @@ public void Map<TKey, TElement>(string notVisiblePropertyOrFieldName, Action<IMa
435439 public void IdBag < TElement > ( string notVisiblePropertyOrFieldName , Action < IIdBagPropertiesMapper < TEntity , TElement > > collectionMapping , Action < ICollectionElementRelation < TElement > > mapping )
436440 {
437441 MemberInfo member = GetPropertyOrFieldMatchingNameOrThrow ( notVisiblePropertyOrFieldName ) ;
438- var collectionElementType = member . GetPropertyOrFieldType ( ) . DetermineCollectionElementType ( ) ;
439- if ( ! typeof ( TElement ) . Equals ( collectionElementType ) )
440- {
441- throw new MappingException ( string . Format ( "Wrong collection element type. For the property/field '{0}' of {1} was expected a collection of {2} but was {3}" ,
442- notVisiblePropertyOrFieldName , typeof ( TEntity ) . FullName , typeof ( TElement ) . Name , collectionElementType . Name ) ) ;
443- }
442+ AssertCollectionElementType < TElement > ( notVisiblePropertyOrFieldName , member ) ;
443+
444444 MemberInfo memberOf = member . GetMemberFromReflectedType ( typeof ( TEntity ) ) ;
445445 RegisterIdBagMapping < TElement > ( collectionMapping , mapping , member , memberOf ) ;
446446 }
0 commit comments