Skip to content

Commit fd17962

Browse files
committed
Serialize system types in preparation for .NET Core. (fix)
1 parent bc92c9b commit fd17962

13 files changed

+152
-239
lines changed

src/NHibernate/Engine/Query/QueryPlanCache.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ private class HQLQueryPlanKey : IEquatable<HQLQueryPlanKey>
176176
private readonly bool shallow;
177177
private readonly HashSet<string> filterNames;
178178
private readonly int hashCode;
179-
private readonly string queryTypeDiscriminatorAssemblyQualifiedName;
179+
private readonly SerializableSystemType queryTypeDiscriminator;
180180

181181
public HQLQueryPlanKey(string query, bool shallow, IDictionary<string, IFilter> enabledFilters)
182182
: this(typeof(object), query, shallow, enabledFilters)
@@ -190,11 +190,7 @@ public HQLQueryPlanKey(IQueryExpression queryExpression, bool shallow, IDictiona
190190

191191
protected HQLQueryPlanKey(System.Type queryTypeDiscriminator, string query, bool shallow, IDictionary<string, IFilter> enabledFilters)
192192
{
193-
this.queryTypeDiscriminatorAssemblyQualifiedName =
194-
(queryTypeDiscriminator == typeof(object))
195-
? "object"
196-
: queryTypeDiscriminator.AssemblyQualifiedName;
197-
193+
this.queryTypeDiscriminator = queryTypeDiscriminator;
198194
this.query = query;
199195
this.shallow = shallow;
200196

@@ -244,7 +240,7 @@ public bool Equals(HQLQueryPlanKey that)
244240
return false;
245241
}
246242

247-
if (queryTypeDiscriminatorAssemblyQualifiedName != that.queryTypeDiscriminatorAssemblyQualifiedName)
243+
if (queryTypeDiscriminator != that.queryTypeDiscriminator)
248244
{
249245
return false;
250246
}

src/NHibernate/Impl/CriteriaImpl.cs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace NHibernate.Impl
1717
[Serializable]
1818
public partial class CriteriaImpl : ICriteria
1919
{
20-
private readonly string persistentClassAssemblyQualifiedName;
20+
private readonly SerializableSystemType persistentClass;
2121
private readonly List<CriterionEntry> criteria = new List<CriterionEntry>();
2222
private readonly List<OrderEntry> orderEntries = new List<OrderEntry>(10);
2323
private readonly Dictionary<string, FetchMode> fetchModes = new Dictionary<string, FetchMode>();
@@ -37,9 +37,6 @@ public partial class CriteriaImpl : ICriteria
3737
private FlushMode? sessionFlushMode;
3838
private bool? readOnly;
3939

40-
[NonSerialized]
41-
private System.Type persistentClass;
42-
4340
private readonly List<Subcriteria> subcriteriaList = new List<Subcriteria>();
4441
private readonly string rootAlias;
4542

@@ -55,14 +52,12 @@ public CriteriaImpl(System.Type persistentClass, ISessionImplementor session)
5552
: this(persistentClass.FullName, CriteriaSpecification.RootAlias, session)
5653
{
5754
this.persistentClass = persistentClass;
58-
this.persistentClassAssemblyQualifiedName = persistentClass.AssemblyQualifiedName;
5955
}
6056

6157
public CriteriaImpl(System.Type persistentClass, string alias, ISessionImplementor session)
6258
: this(persistentClass.FullName, alias, session)
6359
{
6460
this.persistentClass = persistentClass;
65-
this.persistentClassAssemblyQualifiedName = persistentClass.AssemblyQualifiedName;
6661
}
6762

6863
public CriteriaImpl(string entityOrClassName, ISessionImplementor session)
@@ -198,16 +193,6 @@ public string Comment
198193
get { return comment; }
199194
}
200195

201-
private System.Type PersistentClass
202-
{
203-
get
204-
{
205-
if (persistentClass != null || persistentClassAssemblyQualifiedName == null) return persistentClass;
206-
207-
return (persistentClass = ReflectHelper.ClassForName(persistentClassAssemblyQualifiedName));
208-
}
209-
}
210-
211196
protected internal void Before()
212197
{
213198
if (flushMode.HasValue)
@@ -534,9 +519,9 @@ public ICriteria SetCacheMode(CacheMode cacheMode)
534519
public object Clone()
535520
{
536521
CriteriaImpl clone;
537-
if (PersistentClass != null)
522+
if (persistentClass != null)
538523
{
539-
clone = new CriteriaImpl(PersistentClass, Alias, Session);
524+
clone = new CriteriaImpl((System.Type)persistentClass, Alias, Session);
540525
}
541526
else
542527
{
@@ -1008,8 +993,8 @@ public override string ToString()
1008993

1009994
public System.Type GetRootEntityTypeIfAvailable()
1010995
{
1011-
if (PersistentClass != null)
1012-
return PersistentClass;
996+
if (persistentClass != null)
997+
return (System.Type)persistentClass;
1013998
throw new HibernateException("Cannot provide root entity type because this criteria was initialized with an entity name.");
1014999
}
10151000
}

src/NHibernate/InstantiationException.cs

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Runtime.Serialization;
33
using System.Security;
4-
using System.Security.Permissions;
54
using NHibernate.Util;
65

76
namespace NHibernate
@@ -12,19 +11,12 @@ namespace NHibernate
1211
[Serializable]
1312
public class InstantiationException : HibernateException
1413
{
15-
[NonSerialized]
16-
private System.Type type;
17-
18-
private readonly string typeFullName;
14+
private readonly SerializableSystemType _type;
1915

2016
public InstantiationException(string message, System.Type type)
2117
: base(message)
2218
{
23-
if (type == null)
24-
throw new ArgumentNullException(nameof(type));
25-
26-
this.type = type;
27-
this.typeFullName = type.FullName;
19+
_type = type ?? throw new ArgumentNullException(nameof(type));
2820
}
2921

3022
/// <summary>
@@ -40,25 +32,13 @@ public InstantiationException(string message, System.Type type)
4032
public InstantiationException(string message, Exception innerException, System.Type type)
4133
: base(message, innerException)
4234
{
43-
if (type == null)
44-
throw new ArgumentNullException(nameof(type));
45-
46-
this.type = type;
47-
this.typeFullName = type.FullName;
35+
_type = type ?? throw new ArgumentNullException(nameof(type));
4836
}
4937

5038
/// <summary>
5139
/// Gets the <see cref="System.Type"/> that NHibernate was trying to instantiate.
5240
/// </summary>
53-
public System.Type PersistentType
54-
{
55-
get
56-
{
57-
if (this.type != null) return type;
58-
59-
return (this.type = ReflectHelper.ClassForFullNameOrNull(this.typeFullName));
60-
}
61-
}
41+
public System.Type PersistentType => _type?.TryGetType();
6242

6343
/// <summary>
6444
/// Gets a message that describes the current <see cref="InstantiationException"/>.
@@ -67,10 +47,7 @@ public System.Type PersistentType
6747
/// The error message that explains the reason for this exception and the Type that
6848
/// was trying to be instantiated.
6949
/// </value>
70-
public override string Message
71-
{
72-
get { return base.Message + (typeFullName ?? ""); }
73-
}
50+
public override string Message => base.Message + (_type == null ? "" : _type.FullName);
7451

7552
#region ISerializable Members
7653

@@ -87,7 +64,18 @@ public override string Message
8764
/// </param>
8865
protected InstantiationException(SerializationInfo info, StreamingContext context) : base(info, context)
8966
{
90-
this.typeFullName = info.GetString("typeFullName");
67+
foreach (SerializationEntry entry in info)
68+
{
69+
switch (entry.Name)
70+
{
71+
case "type":
72+
_type = (System.Type)entry.Value;
73+
break;
74+
case "_type":
75+
_type = (SerializableSystemType) entry.Value;
76+
break;
77+
}
78+
}
9179
}
9280

9381
/// <summary>
@@ -105,7 +93,7 @@ protected InstantiationException(SerializationInfo info, StreamingContext contex
10593
public override void GetObjectData(SerializationInfo info, StreamingContext context)
10694
{
10795
base.GetObjectData(info, context);
108-
info.AddValue("typeFullName", typeFullName);
96+
info.AddValue("_type", _type);
10997
}
11098

11199
#endregion

src/NHibernate/Intercept/AbstractFieldInterceptor.cs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,14 @@ public abstract class AbstractFieldInterceptor : IFieldInterceptor
1313

1414
[NonSerialized]
1515
private ISessionImplementor session;
16-
1716
private ISet<string> uninitializedFields;
1817
private readonly ISet<string> unwrapProxyFieldNames;
1918
private readonly HashSet<string> loadedUnwrapProxyFieldNames = new HashSet<string>();
2019
private readonly string entityName;
21-
22-
private readonly string mappedClassAssemblyQualifiedName;
23-
24-
[NonSerialized]
25-
private System.Type mappedClass;
20+
private readonly SerializableSystemType mappedClass;
2621

2722
[NonSerialized]
2823
private bool initializing;
29-
3024
private bool isDirty;
3125

3226
protected internal AbstractFieldInterceptor(ISessionImplementor session, ISet<string> uninitializedFields, ISet<string> unwrapProxyFieldNames, string entityName, System.Type mappedClass)
@@ -36,7 +30,6 @@ protected internal AbstractFieldInterceptor(ISessionImplementor session, ISet<st
3630
this.unwrapProxyFieldNames = unwrapProxyFieldNames ?? new HashSet<string>();
3731
this.entityName = entityName;
3832
this.mappedClass = mappedClass;
39-
this.mappedClassAssemblyQualifiedName = mappedClass?.AssemblyQualifiedName;
4033
}
4134

4235
#region IFieldInterceptor Members
@@ -79,12 +72,7 @@ public string EntityName
7972

8073
public System.Type MappedClass
8174
{
82-
get
83-
{
84-
if (mappedClass != null || mappedClassAssemblyQualifiedName == null) return mappedClass;
85-
86-
return (mappedClass = ReflectHelper.ClassForName(mappedClassAssemblyQualifiedName));
87-
}
75+
get { return (System.Type)mappedClass; }
8876
}
8977

9078
#endregion

src/NHibernate/Mapping/Collection.cs

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,7 @@ public abstract class Collection : IFetchable, IValue, IFilterable
4343
private bool orphanDelete;
4444
private int batchSize = -1;
4545
private FetchMode fetchMode;
46-
47-
private string collectionPersisterClassAssemblyQualifedName;
48-
49-
[NonSerialized]
50-
private System.Type collectionPersisterClass;
51-
46+
private SerializableSystemType collectionPersisterClass;
5247
private string referencedPropertyName;
5348
private string typeName;
5449

@@ -71,12 +66,7 @@ public abstract class Collection : IFetchable, IValue, IFilterable
7166
private ExecuteUpdateResultCheckStyle deleteAllCheckStyle;
7267

7368
private bool isGeneric;
74-
75-
private string[] genericArgumentsAssemblyQualifiedNames;
76-
77-
[NonSerialized]
78-
private System.Type[] genericArguments;
79-
69+
private SerializableSystemType[] genericArguments;
8070
private readonly Dictionary<string, string> filters = new Dictionary<string, string>();
8171
private readonly Dictionary<string, string> manyToManyFilters = new Dictionary<string, string>();
8272
private bool subselectLoadable;
@@ -149,16 +139,8 @@ public PersistentClass Owner
149139

150140
public System.Type CollectionPersisterClass
151141
{
152-
get
153-
{
154-
if (collectionPersisterClass != null || collectionPersisterClassAssemblyQualifedName == null) return collectionPersisterClass;
155-
return (collectionPersisterClass = ReflectHelper.ClassForName(collectionPersisterClassAssemblyQualifedName));
156-
}
157-
set
158-
{
159-
collectionPersisterClass = value;
160-
collectionPersisterClassAssemblyQualifedName = value?.AssemblyQualifiedName;
161-
}
142+
get { return (System.Type) collectionPersisterClass; }
143+
set { collectionPersisterClass = value; }
162144
}
163145

164146
// The type of this property is object, so as to accommodate
@@ -342,26 +324,18 @@ public bool IsGeneric
342324
/// </summary>
343325
public System.Type[] GenericArguments
344326
{
345-
get
346-
{
347-
if (genericArguments != null || genericArgumentsAssemblyQualifiedNames == null) return genericArguments;
348-
return (genericArguments = genericArgumentsAssemblyQualifiedNames.Select(ReflectHelper.ClassForName).ToArray());
349-
}
350-
set
351-
{
352-
genericArguments = value;
353-
genericArgumentsAssemblyQualifiedNames = value?.Select(x => x.AssemblyQualifiedName).ToArray();
354-
}
327+
get { return genericArguments.Cast<System.Type>().ToArray(); }
328+
set { genericArguments = value?.Cast<SerializableSystemType>().ToArray(); }
355329
}
356330

357331
protected void CheckGenericArgumentsLength(int expectedLength)
358332
{
359-
if (GenericArguments.Length != expectedLength)
333+
if (genericArguments.Length != expectedLength)
360334
{
361335
throw new MappingException(
362336
string.Format(
363337
"Error mapping generic collection {0}: expected {1} generic parameters, but the property type has {2}",
364-
Role, expectedLength, GenericArguments.Length));
338+
Role, expectedLength, genericArguments.Length));
365339
}
366340
}
367341

src/NHibernate/Mapping/RootClass.cs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,13 @@ public class RootClass : PersistentClass, ITableOwner
4040
private bool mutable;
4141
private bool embeddedIdentifier = false;
4242
private bool explicitPolymorphism;
43-
private string entityPersisterClassAssemblyQualifiedName;
43+
private SerializableSystemType entityPersisterClass;
4444
private bool forceDiscriminator;
4545
private string where;
4646
private Table table;
4747
private bool discriminatorInsertable = true;
4848
private int nextSubclassId = 0;
4949

50-
[NonSerialized]
51-
private System.Type entityPersisterClass;
52-
5350
public override int SubclassId
5451
{
5552
get { return 0; }
@@ -111,16 +108,8 @@ public override bool IsVersioned
111108

112109
public override System.Type EntityPersisterClass
113110
{
114-
get
115-
{
116-
if (entityPersisterClass != null || entityPersisterClassAssemblyQualifiedName == null) return entityPersisterClass;
117-
return (entityPersisterClass = ReflectHelper.ClassForName(entityPersisterClassAssemblyQualifiedName));
118-
}
119-
set
120-
{
121-
entityPersisterClass = value;
122-
entityPersisterClassAssemblyQualifiedName = value?.AssemblyQualifiedName;
123-
}
111+
get { return (System.Type)entityPersisterClass; }
112+
set { entityPersisterClass = value; }
124113
}
125114

126115
/// <summary>

0 commit comments

Comments
 (0)