Skip to content

Commit b8d7014

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 fff308f commit b8d7014

27 files changed

+485
-527
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/EntityType.cs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ public override async Task<object> ReplaceAsync(object original, object target,
9898
{
9999
return target;
100100
}
101-
if (session.GetContextEntityIdentifier(original) == null && (await (ForeignKeys.IsTransientFastAsync(associatedEntityName, original, session, cancellationToken)).ConfigureAwait(false)).GetValueOrDefault())
101+
if (session.GetContextEntityIdentifier(original) == null && (await (ForeignKeys.IsTransientFastAsync(_associatedEntityName, original, session, cancellationToken)).ConfigureAwait(false)).GetValueOrDefault())
102102
{
103-
object copy = session.Factory.GetEntityPersister(associatedEntityName).Instantiate(null);
103+
object copy = session.Factory.GetEntityPersister(_associatedEntityName).Instantiate(null);
104104
//TODO: should this be Session.instantiate(Persister, ...)?
105105
copyCache.Add(original, copy);
106106
return copy;
@@ -129,7 +129,7 @@ public override async Task<object> ReplaceAsync(object original, object target,
129129
/// <returns>
130130
/// An instance of the object or <see langword="null" /> if the identifer was null.
131131
/// </returns>
132-
public override sealed async Task<object> NullSafeGetAsync(DbDataReader rs, string[] names, ISessionImplementor session, object owner, CancellationToken cancellationToken)
132+
public sealed override async Task<object> NullSafeGetAsync(DbDataReader rs, string[] names, ISessionImplementor session, object owner, CancellationToken cancellationToken)
133133
{
134134
cancellationToken.ThrowIfCancellationRequested();
135135
return await (ResolveIdentifierAsync(await (HydrateAsync(rs, names, session, owner, cancellationToken)).ConfigureAwait(false), session, owner, cancellationToken)).ConfigureAwait(false);
@@ -144,10 +144,10 @@ protected async Task<object> ResolveIdentifierAsync(object id, ISessionImplement
144144
{
145145
cancellationToken.ThrowIfCancellationRequested();
146146
string entityName = GetAssociatedEntityName();
147-
bool isProxyUnwrapEnabled = unwrapProxy && session.Factory
147+
bool isProxyUnwrapEnabled = _unwrapProxy && session.Factory
148148
.GetEntityPersister(entityName).IsInstrumented;
149149

150-
object proxyOrEntity = await (session.InternalLoadAsync(entityName, id, eager, IsNullable && !isProxyUnwrapEnabled, cancellationToken)).ConfigureAwait(false);
150+
object proxyOrEntity = await (session.InternalLoadAsync(entityName, id, _eager, IsNullable && !isProxyUnwrapEnabled, cancellationToken)).ConfigureAwait(false);
151151

152152
if (proxyOrEntity.IsProxy())
153153
{
@@ -178,21 +178,19 @@ public override Task<object> ResolveIdentifierAsync(object value, ISessionImplem
178178
{
179179
return Task.FromResult<object>(null);
180180
}
181-
else
181+
182+
if (IsNull(owner, session))
182183
{
183-
if (IsNull(owner, session))
184-
{
185-
return Task.FromResult<object>(null); //EARLY EXIT!
186-
}
184+
return Task.FromResult<object>(null); //EARLY EXIT!
185+
}
187186

188-
if (IsReferenceToPrimaryKey)
189-
{
190-
return ResolveIdentifierAsync(value, session, cancellationToken);
191-
}
192-
else
193-
{
194-
return LoadByUniqueKeyAsync(GetAssociatedEntityName(), uniqueKeyPropertyName, value, session, cancellationToken);
195-
}
187+
if (IsReferenceToPrimaryKey)
188+
{
189+
return ResolveIdentifierAsync(value, session, cancellationToken);
190+
}
191+
else
192+
{
193+
return LoadByUniqueKeyAsync(GetAssociatedEntityName(), uniqueKeyPropertyName, value, session, cancellationToken);
196194
}
197195
}
198196
catch (Exception ex)

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: 1 addition & 1 deletion
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 SerializableSystemType queryTypeDiscriminator;
180180

181181
public HQLQueryPlanKey(string query, bool shallow, IDictionary<string, IFilter> enabledFilters)
182182
: this(typeof(object), query, shallow, enabledFilters)

src/NHibernate/Impl/CriteriaImpl.cs

Lines changed: 3 additions & 3 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 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>();
@@ -521,7 +521,7 @@ public object Clone()
521521
CriteriaImpl clone;
522522
if (persistentClass != null)
523523
{
524-
clone = new CriteriaImpl(persistentClass, Alias, Session);
524+
clone = new CriteriaImpl((System.Type)persistentClass, Alias, Session);
525525
}
526526
else
527527
{
@@ -994,7 +994,7 @@ public override string ToString()
994994
public System.Type GetRootEntityTypeIfAvailable()
995995
{
996996
if (persistentClass != null)
997-
return persistentClass;
997+
return (System.Type)persistentClass;
998998
throw new HibernateException("Cannot provide root entity type because this criteria was initialized with an entity name.");
999999
}
10001000
}

src/NHibernate/InstantiationException.cs

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

66
namespace NHibernate
77
{
@@ -11,15 +11,12 @@ namespace NHibernate
1111
[Serializable]
1212
public class InstantiationException : HibernateException
1313
{
14-
private readonly System.Type type;
14+
private readonly SerializableSystemType _type;
1515

1616
public InstantiationException(string message, System.Type type)
1717
: base(message)
1818
{
19-
if (type == null)
20-
throw new ArgumentNullException("type");
21-
22-
this.type = type;
19+
_type = type ?? throw new ArgumentNullException(nameof(type));
2320
}
2421

2522
/// <summary>
@@ -35,19 +32,13 @@ public InstantiationException(string message, System.Type type)
3532
public InstantiationException(string message, Exception innerException, System.Type type)
3633
: base(message, innerException)
3734
{
38-
if (type == null)
39-
throw new ArgumentNullException("type");
40-
41-
this.type = type;
35+
_type = type ?? throw new ArgumentNullException(nameof(type));
4236
}
4337

4438
/// <summary>
4539
/// Gets the <see cref="System.Type"/> that NHibernate was trying to instantiate.
4640
/// </summary>
47-
public System.Type PersistentType
48-
{
49-
get { return type; }
50-
}
41+
public System.Type PersistentType => _type?.TryGetType();
5142

5243
/// <summary>
5344
/// Gets a message that describes the current <see cref="InstantiationException"/>.
@@ -56,10 +47,7 @@ public System.Type PersistentType
5647
/// The error message that explains the reason for this exception and the Type that
5748
/// was trying to be instantiated.
5849
/// </value>
59-
public override string Message
60-
{
61-
get { return base.Message + (type == null ? "" : type.FullName); }
62-
}
50+
public override string Message => base.Message + (_type == null ? "" : _type.FullName);
6351

6452
#region ISerializable Members
6553

@@ -76,7 +64,19 @@ public override string Message
7664
/// </param>
7765
protected InstantiationException(SerializationInfo info, StreamingContext context) : base(info, context)
7866
{
79-
this.type = info.GetValue("type", typeof(System.Type)) as System.Type;
67+
foreach (SerializationEntry entry in info)
68+
{
69+
switch (entry.Name)
70+
{
71+
// TODO 6.0: remove "type" deserialization
72+
case "type":
73+
_type = (System.Type)entry.Value;
74+
break;
75+
case "_type":
76+
_type = (SerializableSystemType) entry.Value;
77+
break;
78+
}
79+
}
8080
}
8181

8282
/// <summary>
@@ -94,7 +94,7 @@ protected InstantiationException(SerializationInfo info, StreamingContext contex
9494
public override void GetObjectData(SerializationInfo info, StreamingContext context)
9595
{
9696
base.GetObjectData(info, context);
97-
info.AddValue("type", type, typeof(System.Type));
97+
info.AddValue("_type", _type);
9898
}
9999

100100
#endregion

src/NHibernate/Intercept/AbstractFieldInterceptor.cs

Lines changed: 3 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
{
@@ -16,7 +17,7 @@ public abstract class AbstractFieldInterceptor : IFieldInterceptor
1617
private readonly ISet<string> unwrapProxyFieldNames;
1718
private readonly HashSet<string> loadedUnwrapProxyFieldNames = new HashSet<string>();
1819
private readonly string entityName;
19-
private readonly System.Type mappedClass;
20+
private readonly SerializableSystemType mappedClass;
2021

2122
[NonSerialized]
2223
private bool initializing;
@@ -71,7 +72,7 @@ public string EntityName
7172

7273
public System.Type MappedClass
7374
{
74-
get { return mappedClass; }
75+
get { return (System.Type)mappedClass; }
7576
}
7677

7778
#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: 6 additions & 5 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,7 @@ public abstract class Collection : IFetchable, IValue, IFilterable
4243
private bool orphanDelete;
4344
private int batchSize = -1;
4445
private FetchMode fetchMode;
45-
private System.Type collectionPersisterClass;
46+
private SerializableSystemType collectionPersisterClass;
4647
private string referencedPropertyName;
4748
private string typeName;
4849

@@ -65,7 +66,7 @@ public abstract class Collection : IFetchable, IValue, IFilterable
6566
private ExecuteUpdateResultCheckStyle deleteAllCheckStyle;
6667

6768
private bool isGeneric;
68-
private System.Type[] genericArguments;
69+
private SerializableSystemType[] genericArguments;
6970
private readonly Dictionary<string, string> filters = new Dictionary<string, string>();
7071
private readonly Dictionary<string, string> manyToManyFilters = new Dictionary<string, string>();
7172
private bool subselectLoadable;
@@ -138,7 +139,7 @@ public PersistentClass Owner
138139

139140
public System.Type CollectionPersisterClass
140141
{
141-
get { return collectionPersisterClass; }
142+
get { return (System.Type) collectionPersisterClass; }
142143
set { collectionPersisterClass = value; }
143144
}
144145

@@ -323,8 +324,8 @@ public bool IsGeneric
323324
/// </summary>
324325
public System.Type[] GenericArguments
325326
{
326-
get { return genericArguments; }
327-
set { genericArguments = value; }
327+
get => genericArguments.Select(sst => (System.Type)sst).ToArray();
328+
set => genericArguments = value?.Select(t => (SerializableSystemType)t).ToArray();
328329
}
329330

330331
protected void CheckGenericArgumentsLength(int expectedLength)

src/NHibernate/Mapping/Component.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ namespace NHibernate.Mapping
1414
public class Component : SimpleValue, IMetaAttributable
1515
{
1616
private readonly List<Property> properties = new List<Property>();
17+
18+
[NonSerialized]
1719
private System.Type componentClass;
20+
1821
private bool embedded;
1922
private Property parentProperty;
2023
private PersistentClass owner;
@@ -133,26 +136,27 @@ public System.Type ComponentClass
133136
get
134137
{
135138
// NH Different implementation (we use reflection only when needed)
136-
if (componentClass == null)
139+
if (componentClass != null) return componentClass;
140+
141+
try
137142
{
138-
try
139-
{
140-
componentClass = ReflectHelper.ClassForName(componentClassName);
141-
}
142-
catch (Exception cnfe)
143-
{
144-
if (!IsDynamic) // TODO remove this if leave the Exception
145-
throw new MappingException("component class not found: " + componentClassName, cnfe);
146-
return null;
147-
}
143+
componentClass = ReflectHelper.ClassForName(componentClassName);
144+
}
145+
catch (Exception cnfe)
146+
{
147+
if (!IsDynamic) // TODO remove this if leave the Exception
148+
throw new MappingException("component class not found: " + componentClassName, cnfe);
149+
return null;
148150
}
149151
return componentClass;
150152
}
151153
set // TODO NH: Remove the setter
152154
{
153155
componentClass = value;
154156
if (componentClass != null)
157+
{
155158
componentClassName = componentClass.AssemblyQualifiedName;
159+
}
156160
}
157161
}
158162

0 commit comments

Comments
 (0)