@@ -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 , 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>
@@ -546,46 +562,64 @@ public static IType HeuristicType(string typeName, IDictionary<string, string> p
546
562
547
563
if ( type != null )
548
564
return type ;
549
-
565
+
550
566
string [ ] parsedTypeName ;
551
- TypeClassification typeClassification = GetTypeClassification ( typeName ) ;
567
+ var typeClassification = GetTypeClassification ( typeName ) ;
552
568
if ( typeClassification == TypeClassification . LengthOrScale )
569
+ {
553
570
parsedTypeName = typeName . Split ( LengthSplit ) ;
571
+ if ( ! int . TryParse ( parsedTypeName [ 1 ] , out int parsedLength ) )
572
+ {
573
+ throw new MappingException ( $ "Could not parse length value '{ parsedTypeName [ 1 ] } ' as int for type '{ typeName } '") ;
574
+ }
575
+ length = parsedLength ;
576
+ }
554
577
else
555
578
parsedTypeName = typeClassification == TypeClassification . PrecisionScale ? typeName . Split ( PrecisionScaleSplit ) : new [ ] { typeName } ;
556
579
557
-
558
580
System . Type typeClass ;
559
581
try
560
582
{
561
583
typeClass = ReflectHelper . ClassForName ( parsedTypeName [ 0 ] ) ; //typeName);
562
584
}
563
585
catch ( Exception )
564
586
{
565
- typeClass = null ;
587
+ return null ;
566
588
}
589
+ return HeuristicType ( typeClass , parameters , length , false ) ;
590
+ }
567
591
568
- if ( typeClass == null )
569
- return null ;
570
-
592
+ private static IType HeuristicType ( System . Type typeClass , IDictionary < string , string > parameters , int ? length , bool tryBasic = true )
593
+ {
594
+ if ( tryBasic )
595
+ {
596
+ IType type = Basic ( typeClass . AssemblyQualifiedName , parameters ) ;
597
+
598
+ if ( type != null )
599
+ return type ;
600
+ }
571
601
if ( typeof ( IType ) . IsAssignableFrom ( typeClass ) )
572
602
{
573
603
try
574
604
{
575
- type = ( IType ) Environment . ObjectsFactory . CreateInstance ( typeClass ) ;
605
+ var type = ( IType ) Environment . ObjectsFactory . CreateInstance ( typeClass ) ;
606
+ InjectParameters ( type , parameters ) ;
607
+
608
+ var obsolete = typeClass . GetCustomAttribute < ObsoleteAttribute > ( false ) ;
609
+ if ( obsolete != null )
610
+ {
611
+ _log . Warn ( "{0} is obsolete. {1}" , typeClass . AssemblyQualifiedName , obsolete . Message ) ;
612
+ }
613
+ return type ;
576
614
}
577
- catch ( Exception e )
615
+ catch ( HibernateException )
578
616
{
579
- throw new MappingException ( "Could not instantiate IType " + typeClass . Name + ": " + e , e ) ;
617
+ throw ;
580
618
}
581
- InjectParameters ( type , parameters ) ;
582
-
583
- var obsolete = typeClass . GetCustomAttribute < ObsoleteAttribute > ( false ) ;
584
- if ( obsolete != null )
619
+ catch ( Exception e )
585
620
{
586
- _log . Warn ( "{0} is obsolete. {1}" , typeName , obsolete . Message ) ;
621
+ throw new MappingException ( "Could not instantiate IType " + typeClass . Name + ": " + e , e ) ;
587
622
}
588
- return type ;
589
623
}
590
624
if ( typeof ( ICompositeUserType ) . IsAssignableFrom ( typeClass ) )
591
625
{
@@ -603,19 +637,16 @@ public static IType HeuristicType(string typeName, IDictionary<string, string> p
603
637
var unwrapped = typeClass . UnwrapIfNullable ( ) ;
604
638
if ( unwrapped . IsEnum )
605
639
{
606
- return ( IType ) Activator . CreateInstance ( typeof ( EnumType < > ) . MakeGenericType ( unwrapped ) ) ;
640
+ return ( IType ) Activator . CreateInstance ( typeof ( EnumType < > ) . MakeGenericType ( unwrapped ) ) ;
607
641
}
608
642
609
643
if ( ! typeClass . IsSerializable )
610
644
return null ;
611
645
612
- if ( typeClassification == TypeClassification . LengthOrScale )
613
- return GetSerializableType ( typeClass , Int32 . Parse ( parsedTypeName [ 1 ] ) ) ;
614
-
615
646
if ( length . HasValue )
616
647
return GetSerializableType ( typeClass , length . Value ) ;
617
648
618
- return GetSerializableType ( typeClass ) ;
649
+ return GetSerializedOrBasicType ( typeClass ) ;
619
650
}
620
651
621
652
/// <summary>
@@ -683,14 +714,19 @@ private static NullableType GetType(NullableType defaultUnqualifiedType, byte pr
683
714
/// </para>
684
715
/// </remarks>
685
716
public static NullableType GetSerializableType ( System . Type serializableType )
717
+ {
718
+ return ( NullableType ) GetSerializedOrBasicType ( serializableType ) ;
719
+ }
720
+
721
+ private static IType GetSerializedOrBasicType ( System . Type serializableType )
686
722
{
687
723
var key = serializableType . AssemblyQualifiedName ;
688
724
689
725
// The value factory may be run concurrently, but only one resulting value will be yielded to all threads.
690
726
// So we should add the type with its other key in a later operation in order to ensure we cache the same
691
727
// instance for both keys.
692
728
var added = false ;
693
- var type = ( NullableType ) typeByTypeOfName . GetOrAdd (
729
+ var type = typeByTypeOfName . GetOrAdd (
694
730
key ,
695
731
k =>
696
732
{
0 commit comments