@@ -548,7 +548,10 @@ public HbmMapping CompileMappingFor(IEnumerable<System.Type> types)
548
548
{
549
549
throw new ArgumentNullException ( "types" ) ;
550
550
}
551
- var typeToMap = new HashSet < System . Type > ( types ) ;
551
+
552
+ var typeToMap = types . Distinct ( )
553
+ . OrderBy ( x => x , new TypeHierarchyComparer ( ) )
554
+ . ToList ( ) ;
552
555
553
556
string defaultAssemblyName = null ;
554
557
string defaultNamespace = null ;
@@ -563,11 +566,11 @@ public HbmMapping CompileMappingFor(IEnumerable<System.Type> types)
563
566
defaultNamespace = firstType . Namespace ;
564
567
}
565
568
var mapping = NewHbmMapping ( defaultAssemblyName , defaultNamespace ) ;
566
- foreach ( System . Type type in RootClasses ( typeToMap ) )
569
+ foreach ( var type in RootClasses ( typeToMap ) )
567
570
{
568
571
MapRootClass ( type , mapping ) ;
569
572
}
570
- foreach ( System . Type type in Subclasses ( typeToMap ) )
573
+ foreach ( var type in Subclasses ( typeToMap ) )
571
574
{
572
575
AddSubclassMapping ( mapping , type ) ;
573
576
}
@@ -580,16 +583,18 @@ public IEnumerable<HbmMapping> CompileMappingForEach(IEnumerable<System.Type> ty
580
583
{
581
584
throw new ArgumentNullException ( "types" ) ;
582
585
}
583
- var typeToMap = new HashSet < System . Type > ( types ) ;
586
+ var typeToMap = types . Distinct ( )
587
+ . OrderBy ( x => x , new TypeHierarchyComparer ( ) )
588
+ . ToList ( ) ;
584
589
585
590
//NH-2831: always use the full name of the assembly because it may come from GAC
586
- foreach ( System . Type type in RootClasses ( typeToMap ) )
591
+ foreach ( var type in RootClasses ( typeToMap ) )
587
592
{
588
593
var mapping = NewHbmMapping ( type . Assembly . GetName ( ) . FullName , type . Namespace ) ;
589
594
MapRootClass ( type , mapping ) ;
590
595
yield return mapping ;
591
596
}
592
- foreach ( System . Type type in Subclasses ( typeToMap ) )
597
+ foreach ( var type in Subclasses ( typeToMap ) )
593
598
{
594
599
var mapping = NewHbmMapping ( type . Assembly . GetName ( ) . FullName , type . Namespace ) ;
595
600
AddSubclassMapping ( mapping , type ) ;
@@ -1815,5 +1820,16 @@ public IEnumerable<HbmMapping> CompileMappingForEachExplicitlyAddedEntity()
1815
1820
{
1816
1821
return CompileMappingForEach ( customizerHolder . GetAllCustomizedEntities ( ) ) ;
1817
1822
}
1823
+
1824
+ private class TypeHierarchyComparer : IComparer < System . Type >
1825
+ {
1826
+ public int Compare ( System . Type x , System . Type y )
1827
+ {
1828
+ if ( x == y ) return 0 ;
1829
+ if ( x . IsAssignableFrom ( y ) ) return - 1 ;
1830
+ if ( y . IsAssignableFrom ( x ) ) return 1 ;
1831
+ return 0 ;
1832
+ }
1833
+ }
1818
1834
}
1819
- }
1835
+ }
0 commit comments