@@ -522,6 +522,22 @@ public static IType HeuristicType(string typeName)
522
522
return HeuristicType ( typeName , null ) ;
523
523
}
524
524
525
+ /// <summary>
526
+ /// Uses heuristics to deduce a NHibernate type given a string naming the
527
+ /// type.
528
+ /// </summary>
529
+ /// <param name="type"></param>
530
+ /// <returns>An instance of <c>NHibernate.Type.IType</c></returns>
531
+ /// <remarks>
532
+ /// We check to see if it implements IType, ICompositeUserType, IUserType, ILifecycle (Association), or
533
+ /// IPersistentEnum. If none of those are implemented then we will serialize the Type to the
534
+ /// database using NHibernate.Type.SerializableType(typeName)
535
+ /// </remarks>
536
+ public static IType HeuristicType ( System . Type type )
537
+ {
538
+ return HeuristicType ( type , typeName : null , parameters : null , length : null ) ;
539
+ }
540
+
525
541
/// <summary>
526
542
/// Uses heuristics to deduce a NHibernate type given a string naming the type.
527
543
/// </summary>
@@ -532,7 +548,7 @@ public static IType HeuristicType(string typeName, IDictionary<string, string> p
532
548
{
533
549
return HeuristicType ( typeName , parameters , null ) ;
534
550
}
535
-
551
+
536
552
/// <summary>
537
553
/// Uses heuristics to deduce a NHibernate type given a string naming the type.
538
554
/// </summary>
@@ -542,50 +558,63 @@ public static IType HeuristicType(string typeName, IDictionary<string, string> p
542
558
/// <returns></returns>
543
559
public static IType HeuristicType ( string typeName , IDictionary < string , string > parameters , int ? length )
544
560
{
545
- IType type = Basic ( typeName , parameters ) ;
561
+ return HeuristicType ( /*resolvedType:*/ null , typeName , parameters , length ) ;
562
+ }
546
563
547
- if ( type != null )
548
- return type ;
549
-
550
- string [ ] parsedTypeName ;
551
- TypeClassification typeClassification = GetTypeClassification ( typeName ) ;
552
- if ( typeClassification == TypeClassification . LengthOrScale )
553
- parsedTypeName = typeName . Split ( LengthSplit ) ;
554
- else
555
- parsedTypeName = typeClassification == TypeClassification . PrecisionScale ? typeName . Split ( PrecisionScaleSplit ) : new [ ] { typeName } ;
564
+ /// <summary>
565
+ /// Provide either resolvedType or typeName. resolvedType takes precedence.
566
+ /// </summary>
567
+ private static IType HeuristicType ( System . Type resolvedType , string typeName , IDictionary < string , string > parameters , int ? length )
568
+ {
569
+ if ( resolvedType == null )
570
+ {
571
+ IType type = Basic ( typeName , parameters ) ;
556
572
573
+ if ( type != null )
574
+ return type ;
557
575
558
- System . Type typeClass ;
559
- try
560
- {
561
- typeClass = ReflectHelper . ClassForName ( parsedTypeName [ 0 ] ) ; //typeName);
562
- }
563
- catch ( Exception )
564
- {
565
- typeClass = null ;
566
- }
576
+ string [ ] parsedTypeName ;
577
+ var typeClassification = GetTypeClassification ( typeName ) ;
578
+ if ( typeClassification == TypeClassification . LengthOrScale )
579
+ {
580
+ parsedTypeName = typeName . Split ( LengthSplit ) ;
581
+ if ( ! int . TryParse ( parsedTypeName [ 1 ] , out int parsedLength ) )
582
+ {
583
+ throw new MappingException ( $ "Could not parse length value '{ parsedTypeName [ 1 ] } ' as int for type '{ typeName } '") ;
584
+ }
585
+ length = parsedLength ;
586
+ }
587
+ else
588
+ parsedTypeName = typeClassification == TypeClassification . PrecisionScale ? typeName . Split ( PrecisionScaleSplit ) : new [ ] { typeName } ;
567
589
568
- if ( typeClass == null )
569
- return null ;
570
-
590
+ try
591
+ {
592
+ resolvedType = ReflectHelper . ClassForName ( parsedTypeName [ 0 ] ) ; //typeName);
593
+ }
594
+ catch ( Exception )
595
+ {
596
+ return null ;
597
+ }
598
+ }
599
+ var typeClass = resolvedType ;
571
600
if ( typeof ( IType ) . IsAssignableFrom ( typeClass ) )
572
601
{
573
602
try
574
603
{
575
- type = ( IType ) Environment . ObjectsFactory . CreateInstance ( typeClass ) ;
604
+ var type = ( IType ) Environment . ObjectsFactory . CreateInstance ( typeClass ) ;
605
+ InjectParameters ( type , parameters ) ;
606
+
607
+ var obsolete = typeClass . GetCustomAttribute < ObsoleteAttribute > ( false ) ;
608
+ if ( obsolete != null )
609
+ {
610
+ _log . Warn ( "{0} is obsolete. {1}" , typeName , obsolete . Message ) ;
611
+ }
612
+ return type ;
576
613
}
577
614
catch ( Exception e )
578
615
{
579
616
throw new MappingException ( "Could not instantiate IType " + typeClass . Name + ": " + e , e ) ;
580
617
}
581
- InjectParameters ( type , parameters ) ;
582
-
583
- var obsolete = typeClass . GetCustomAttribute < ObsoleteAttribute > ( false ) ;
584
- if ( obsolete != null )
585
- {
586
- _log . Warn ( "{0} is obsolete. {1}" , typeName , obsolete . Message ) ;
587
- }
588
- return type ;
589
618
}
590
619
if ( typeof ( ICompositeUserType ) . IsAssignableFrom ( typeClass ) )
591
620
{
@@ -609,9 +638,6 @@ public static IType HeuristicType(string typeName, IDictionary<string, string> p
609
638
if ( ! typeClass . IsSerializable )
610
639
return null ;
611
640
612
- if ( typeClassification == TypeClassification . LengthOrScale )
613
- return GetSerializableType ( typeClass , Int32 . Parse ( parsedTypeName [ 1 ] ) ) ;
614
-
615
641
if ( length . HasValue )
616
642
return GetSerializableType ( typeClass , length . Value ) ;
617
643
0 commit comments