Skip to content

Commit ee88543

Browse files
committed
Serialize system types in preparation for .NET Core.
Serialize System.Type, ConstructorInfo, FieldInfo, MethodInfo, and PropertyInfo using only basic types.
1 parent 1cb6cf6 commit ee88543

27 files changed

+466
-170
lines changed

src/NHibernate/Async/Type/ArrayType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,4 @@ public override async Task<object> ReplaceElementsAsync(object original, object
7070
return result;
7171
}
7272
}
73-
}
73+
}

src/NHibernate/Async/Type/SerializableType.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System.Runtime.Serialization.Formatters.Binary;
1717
using NHibernate.Engine;
1818
using NHibernate.SqlTypes;
19+
using NHibernate.Util;
1920

2021
namespace NHibernate.Type
2122
{
@@ -56,4 +57,4 @@ public override Task<object> DisassembleAsync(object value, ISessionImplementor
5657
}
5758
}
5859
}
59-
}
60+
}

src/NHibernate/Engine/Query/QueryPlanCache.cs

Lines changed: 7 additions & 3 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 System.Type queryTypeDiscriminator;
179+
private readonly string queryTypeDiscriminatorAssemblyQualifiedName;
180180

181181
public HQLQueryPlanKey(string query, bool shallow, IDictionary<string, IFilter> enabledFilters)
182182
: this(typeof(object), query, shallow, enabledFilters)
@@ -190,7 +190,11 @@ 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.queryTypeDiscriminator = queryTypeDiscriminator;
193+
this.queryTypeDiscriminatorAssemblyQualifiedName =
194+
(queryTypeDiscriminator == typeof(object))
195+
? "object"
196+
: queryTypeDiscriminator.AssemblyQualifiedName;
197+
194198
this.query = query;
195199
this.shallow = shallow;
196200

@@ -240,7 +244,7 @@ public bool Equals(HQLQueryPlanKey that)
240244
return false;
241245
}
242246

243-
if (queryTypeDiscriminator != that.queryTypeDiscriminator)
247+
if (queryTypeDiscriminatorAssemblyQualifiedName != that.queryTypeDiscriminatorAssemblyQualifiedName)
244248
{
245249
return false;
246250
}

src/NHibernate/Impl/CriteriaImpl.cs

Lines changed: 20 additions & 5 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 System.Type persistentClass;
20+
private readonly string persistentClassAssemblyQualifiedName;
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,6 +37,9 @@ public partial class CriteriaImpl : ICriteria
3737
private FlushMode? sessionFlushMode;
3838
private bool? readOnly;
3939

40+
[NonSerialized]
41+
private System.Type persistentClass;
42+
4043
private readonly List<Subcriteria> subcriteriaList = new List<Subcriteria>();
4144
private readonly string rootAlias;
4245

@@ -52,12 +55,14 @@ public CriteriaImpl(System.Type persistentClass, ISessionImplementor session)
5255
: this(persistentClass.FullName, CriteriaSpecification.RootAlias, session)
5356
{
5457
this.persistentClass = persistentClass;
58+
this.persistentClassAssemblyQualifiedName = persistentClass.AssemblyQualifiedName;
5559
}
5660

5761
public CriteriaImpl(System.Type persistentClass, string alias, ISessionImplementor session)
5862
: this(persistentClass.FullName, alias, session)
5963
{
6064
this.persistentClass = persistentClass;
65+
this.persistentClassAssemblyQualifiedName = persistentClass.AssemblyQualifiedName;
6166
}
6267

6368
public CriteriaImpl(string entityOrClassName, ISessionImplementor session)
@@ -193,6 +198,16 @@ public string Comment
193198
get { return comment; }
194199
}
195200

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+
196211
protected internal void Before()
197212
{
198213
if (flushMode.HasValue)
@@ -519,9 +534,9 @@ public ICriteria SetCacheMode(CacheMode cacheMode)
519534
public object Clone()
520535
{
521536
CriteriaImpl clone;
522-
if (persistentClass != null)
537+
if (PersistentClass != null)
523538
{
524-
clone = new CriteriaImpl(persistentClass, Alias, Session);
539+
clone = new CriteriaImpl(PersistentClass, Alias, Session);
525540
}
526541
else
527542
{
@@ -993,8 +1008,8 @@ public override string ToString()
9931008

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

src/NHibernate/InstantiationException.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Runtime.Serialization;
33
using System.Security;
44
using System.Security.Permissions;
5+
using NHibernate.Util;
56

67
namespace NHibernate
78
{
@@ -11,15 +12,19 @@ namespace NHibernate
1112
[Serializable]
1213
public class InstantiationException : HibernateException
1314
{
14-
private readonly System.Type type;
15+
[NonSerialized]
16+
private System.Type type;
17+
18+
private readonly string typeFullName;
1519

1620
public InstantiationException(string message, System.Type type)
1721
: base(message)
1822
{
1923
if (type == null)
20-
throw new ArgumentNullException("type");
24+
throw new ArgumentNullException(nameof(type));
2125

2226
this.type = type;
27+
this.typeFullName = type.FullName;
2328
}
2429

2530
/// <summary>
@@ -36,17 +41,23 @@ public InstantiationException(string message, Exception innerException, System.T
3641
: base(message, innerException)
3742
{
3843
if (type == null)
39-
throw new ArgumentNullException("type");
44+
throw new ArgumentNullException(nameof(type));
4045

4146
this.type = type;
47+
this.typeFullName = type.FullName;
4248
}
4349

4450
/// <summary>
4551
/// Gets the <see cref="System.Type"/> that NHibernate was trying to instantiate.
4652
/// </summary>
4753
public System.Type PersistentType
4854
{
49-
get { return type; }
55+
get
56+
{
57+
if (this.type != null) return type;
58+
59+
return (this.type = ReflectHelper.ClassForFullNameOrNull(this.typeFullName));
60+
}
5061
}
5162

5263
/// <summary>
@@ -58,7 +69,7 @@ public System.Type PersistentType
5869
/// </value>
5970
public override string Message
6071
{
61-
get { return base.Message + (type == null ? "" : type.FullName); }
72+
get { return base.Message + (typeFullName ?? ""); }
6273
}
6374

6475
#region ISerializable Members
@@ -76,7 +87,7 @@ public override string Message
7687
/// </param>
7788
protected InstantiationException(SerializationInfo info, StreamingContext context) : base(info, context)
7889
{
79-
this.type = info.GetValue("type", typeof(System.Type)) as System.Type;
90+
this.typeFullName = info.GetString("typeFullName");
8091
}
8192

8293
/// <summary>
@@ -94,7 +105,7 @@ protected InstantiationException(SerializationInfo info, StreamingContext contex
94105
public override void GetObjectData(SerializationInfo info, StreamingContext context)
95106
{
96107
base.GetObjectData(info, context);
97-
info.AddValue("type", type, typeof(System.Type));
108+
info.AddValue("typeFullName", typeFullName);
98109
}
99110

100111
#endregion

src/NHibernate/Intercept/AbstractFieldInterceptor.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using NHibernate.Engine;
44
using NHibernate.Proxy;
5+
using NHibernate.Util;
56

67
namespace NHibernate.Intercept
78
{
@@ -12,14 +13,20 @@ public abstract class AbstractFieldInterceptor : IFieldInterceptor
1213

1314
[NonSerialized]
1415
private ISessionImplementor session;
16+
1517
private ISet<string> uninitializedFields;
1618
private readonly ISet<string> unwrapProxyFieldNames;
1719
private readonly HashSet<string> loadedUnwrapProxyFieldNames = new HashSet<string>();
1820
private readonly string entityName;
19-
private readonly System.Type mappedClass;
21+
22+
private readonly string mappedClassAssemblyQualifiedName;
23+
24+
[NonSerialized]
25+
private System.Type mappedClass;
2026

2127
[NonSerialized]
2228
private bool initializing;
29+
2330
private bool isDirty;
2431

2532
protected internal AbstractFieldInterceptor(ISessionImplementor session, ISet<string> uninitializedFields, ISet<string> unwrapProxyFieldNames, string entityName, System.Type mappedClass)
@@ -29,6 +36,7 @@ protected internal AbstractFieldInterceptor(ISessionImplementor session, ISet<st
2936
this.unwrapProxyFieldNames = unwrapProxyFieldNames ?? new HashSet<string>();
3037
this.entityName = entityName;
3138
this.mappedClass = mappedClass;
39+
this.mappedClassAssemblyQualifiedName = mappedClass?.AssemblyQualifiedName;
3240
}
3341

3442
#region IFieldInterceptor Members
@@ -71,7 +79,12 @@ public string EntityName
7179

7280
public System.Type MappedClass
7381
{
74-
get { return mappedClass; }
82+
get
83+
{
84+
if (mappedClass != null || mappedClassAssemblyQualifiedName == null) return mappedClass;
85+
86+
return (mappedClass = ReflectHelper.ClassForName(mappedClassAssemblyQualifiedName));
87+
}
7588
}
7689

7790
#endregion

src/NHibernate/Mapping/Array.cs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ namespace NHibernate.Mapping
1010
[Serializable]
1111
public class Array : List
1212
{
13+
[NonSerialized]
1314
private System.Type elementClass;
15+
1416
private string elementClassName;
1517

1618
public Array(PersistentClass owner) : base(owner)
@@ -21,23 +23,22 @@ public System.Type ElementClass
2123
{
2224
get
2325
{
24-
if (elementClass == null)
26+
if (elementClass != null) return elementClass;
27+
28+
if (elementClassName == null)
29+
{
30+
IType elementType = Element.Type;
31+
elementClass = IsPrimitiveArray ? ((PrimitiveType) elementType).PrimitiveClass : elementType.ReturnedClass;
32+
}
33+
else
2534
{
26-
if (elementClassName == null)
35+
try
2736
{
28-
IType elementType = Element.Type;
29-
elementClass = IsPrimitiveArray ? ((PrimitiveType) elementType).PrimitiveClass : elementType.ReturnedClass;
37+
elementClass = ReflectHelper.ClassForName(elementClassName);
3038
}
31-
else
39+
catch (Exception cnfe)
3240
{
33-
try
34-
{
35-
elementClass = ReflectHelper.ClassForName(elementClassName);
36-
}
37-
catch (Exception cnfe)
38-
{
39-
throw new MappingException(cnfe);
40-
}
41+
throw new MappingException(cnfe);
4142
}
4243
}
4344
return elementClass;
@@ -68,4 +69,4 @@ public string ElementClassName
6869
}
6970
}
7071
}
71-
}
72+
}

src/NHibernate/Mapping/Collection.cs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using NHibernate.Engine;
45
using NHibernate.SqlCommand;
56
using NHibernate.Type;
@@ -42,7 +43,12 @@ public abstract class Collection : IFetchable, IValue, IFilterable
4243
private bool orphanDelete;
4344
private int batchSize = -1;
4445
private FetchMode fetchMode;
46+
47+
private string collectionPersisterClassAssemblyQualifedName;
48+
49+
[NonSerialized]
4550
private System.Type collectionPersisterClass;
51+
4652
private string referencedPropertyName;
4753
private string typeName;
4854

@@ -65,7 +71,12 @@ public abstract class Collection : IFetchable, IValue, IFilterable
6571
private ExecuteUpdateResultCheckStyle deleteAllCheckStyle;
6672

6773
private bool isGeneric;
74+
75+
private string[] genericArgumentsAssemblyQualifiedNames;
76+
77+
[NonSerialized]
6878
private System.Type[] genericArguments;
79+
6980
private readonly Dictionary<string, string> filters = new Dictionary<string, string>();
7081
private readonly Dictionary<string, string> manyToManyFilters = new Dictionary<string, string>();
7182
private bool subselectLoadable;
@@ -138,8 +149,16 @@ public PersistentClass Owner
138149

139150
public System.Type CollectionPersisterClass
140151
{
141-
get { return collectionPersisterClass; }
142-
set { collectionPersisterClass = value; }
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+
}
143162
}
144163

145164
// The type of this property is object, so as to accommodate
@@ -323,18 +342,26 @@ public bool IsGeneric
323342
/// </summary>
324343
public System.Type[] GenericArguments
325344
{
326-
get { return genericArguments; }
327-
set { genericArguments = value; }
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+
}
328355
}
329356

330357
protected void CheckGenericArgumentsLength(int expectedLength)
331358
{
332-
if (genericArguments.Length != expectedLength)
359+
if (GenericArguments.Length != expectedLength)
333360
{
334361
throw new MappingException(
335362
string.Format(
336363
"Error mapping generic collection {0}: expected {1} generic parameters, but the property type has {2}",
337-
Role, expectedLength, genericArguments.Length));
364+
Role, expectedLength, GenericArguments.Length));
338365
}
339366
}
340367

0 commit comments

Comments
 (0)