Skip to content

Commit e3fee27

Browse files
committed
better fix
1 parent 8edd15d commit e3fee27

File tree

1 file changed

+35
-36
lines changed

1 file changed

+35
-36
lines changed

src/NHibernate/Type/TypeFactory.cs

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ public static IType HeuristicType(string typeName)
535535
/// </remarks>
536536
public static IType HeuristicType(System.Type type)
537537
{
538-
return HeuristicType(type, typeName: null, parameters: null, length: null);
538+
return HeuristicType(type, parameters: null, length: null);
539539
}
540540

541541
/// <summary>
@@ -558,56 +558,50 @@ public static IType HeuristicType(string typeName, IDictionary<string, string> p
558558
/// <returns></returns>
559559
public static IType HeuristicType(string typeName, IDictionary<string, string> parameters, int? length)
560560
{
561-
return HeuristicType(/*resolvedType:*/ null, typeName, parameters, length);
562-
}
563-
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-
IType type = Basic(resolvedType?.AssemblyQualifiedName ?? typeName, parameters);
561+
IType type = Basic(typeName, parameters);
570562

571563
if (type != null)
572564
return type;
573565

574-
if (resolvedType == null)
566+
string[] parsedTypeName;
567+
var typeClassification = GetTypeClassification(typeName);
568+
if (typeClassification == TypeClassification.LengthOrScale)
575569
{
576-
string[] parsedTypeName;
577-
var typeClassification = GetTypeClassification(typeName);
578-
if (typeClassification == TypeClassification.LengthOrScale)
570+
parsedTypeName = typeName.Split(LengthSplit);
571+
if (!int.TryParse(parsedTypeName[1], out int parsedLength))
579572
{
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;
573+
throw new MappingException($"Could not parse length value '{parsedTypeName[1]}' as int for type '{typeName}'");
586574
}
587-
else
588-
parsedTypeName = typeClassification == TypeClassification.PrecisionScale ? typeName.Split(PrecisionScaleSplit) : new[] { typeName };
575+
length = parsedLength;
576+
}
577+
else
578+
parsedTypeName = typeClassification == TypeClassification.PrecisionScale ? typeName.Split(PrecisionScaleSplit) : new[] { typeName };
589579

590-
try
591-
{
592-
resolvedType = ReflectHelper.ClassForName(parsedTypeName[0]); //typeName);
593-
}
594-
catch (Exception)
595-
{
596-
return null;
597-
}
580+
System.Type typeClass;
581+
try
582+
{
583+
typeClass = ReflectHelper.ClassForName(parsedTypeName[0]); //typeName);
584+
}
585+
catch (Exception)
586+
{
587+
return null;
598588
}
599-
var typeClass = resolvedType;
589+
return HeuristicType(typeClass, parameters, length);
590+
}
591+
592+
private static IType HeuristicType(System.Type typeClass, IDictionary<string, string> parameters, int? length)
593+
{
600594
if (typeof(IType).IsAssignableFrom(typeClass))
601595
{
602596
try
603597
{
604-
type = (IType) Environment.ObjectsFactory.CreateInstance(typeClass);
598+
var type = (IType) Environment.ObjectsFactory.CreateInstance(typeClass);
605599
InjectParameters(type, parameters);
606600

607601
var obsolete = typeClass.GetCustomAttribute<ObsoleteAttribute>(false);
608602
if (obsolete != null)
609603
{
610-
_log.Warn("{0} is obsolete. {1}", typeName, obsolete.Message);
604+
_log.Warn("{0} is obsolete. {1}", typeClass.AssemblyQualifiedName, obsolete.Message);
611605
}
612606
return type;
613607
}
@@ -632,7 +626,7 @@ private static IType HeuristicType(System.Type resolvedType, string typeName, ID
632626
var unwrapped = typeClass.UnwrapIfNullable();
633627
if (unwrapped.IsEnum)
634628
{
635-
return (IType) Activator.CreateInstance(typeof (EnumType<>).MakeGenericType(unwrapped));
629+
return (IType) Activator.CreateInstance(typeof(EnumType<>).MakeGenericType(unwrapped));
636630
}
637631

638632
if (!typeClass.IsSerializable)
@@ -641,7 +635,7 @@ private static IType HeuristicType(System.Type resolvedType, string typeName, ID
641635
if (length.HasValue)
642636
return GetSerializableType(typeClass, length.Value);
643637

644-
return GetSerializableType(typeClass);
638+
return GetSerializedOrBasicType(typeClass);
645639
}
646640

647641
/// <summary>
@@ -709,14 +703,19 @@ private static NullableType GetType(NullableType defaultUnqualifiedType, byte pr
709703
/// </para>
710704
/// </remarks>
711705
public static NullableType GetSerializableType(System.Type serializableType)
706+
{
707+
return (NullableType) GetSerializedOrBasicType(serializableType);
708+
}
709+
710+
private static IType GetSerializedOrBasicType(System.Type serializableType)
712711
{
713712
var key = serializableType.AssemblyQualifiedName;
714713

715714
// The value factory may be run concurrently, but only one resulting value will be yielded to all threads.
716715
// So we should add the type with its other key in a later operation in order to ensure we cache the same
717716
// instance for both keys.
718717
var added = false;
719-
var type = (NullableType)typeByTypeOfName.GetOrAdd(
718+
var type = typeByTypeOfName.GetOrAdd(
720719
key,
721720
k =>
722721
{

0 commit comments

Comments
 (0)