Skip to content

Commit c774a99

Browse files
committed
Do not serialize FieldInfos in FieldInterceptorObjectReference
The fields should not change between serialization and deserialization. Fixes nhibernate#1964
1 parent f490609 commit c774a99

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

src/NHibernate.Test/StaticProxyTest/Model.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,19 @@ public class InterfacedEntity : IEntity
3333
}
3434

3535
[Serializable]
36-
public class LazyTextEntity
36+
public class LazyTextEntity : BaseTextEntity
3737
{
3838
public virtual Guid Id { get; set; }
3939
public virtual string Name { get; set; }
4040
public virtual string Text { get; set; }
4141
}
4242

43+
[Serializable]
44+
public class BaseTextEntity
45+
{
46+
public virtual int Test { get; set; }
47+
}
48+
4349
[Serializable]
4450
public class InterfacedLazyTextEntity : ILazyTextEntity
4551
{

src/NHibernate/Proxy/FieldInterceptorObjectReference.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public sealed class FieldInterceptorObjectReference : IObjectReference, ISeriali
1717
private readonly object _deserializedProxy;
1818

1919
private const string HasAdditionalDataName = "proxy$hasAdditionalData";
20-
private const string AdditionalMemberName = "proxy$additionalMembers";
2120

2221
public FieldInterceptorObjectReference(NHibernateProxyFactoryInfo proxyFactoryInfo, IFieldInterceptor fieldInterceptorField)
2322
{
@@ -30,39 +29,36 @@ private FieldInterceptorObjectReference(SerializationInfo info, StreamingContext
3029
_proxyFactoryInfo = info.GetValue<NHibernateProxyFactoryInfo>(nameof(_proxyFactoryInfo));
3130
_fieldInterceptor = info.GetValue<IFieldInterceptor>(nameof(_fieldInterceptor));
3231

32+
var proxy = _proxyFactoryInfo.CreateProxyFactory().GetFieldInterceptionProxy(null);
3333
if (info.GetBoolean(HasAdditionalDataName))
3434
{
35-
_deserializedProxy = _proxyFactoryInfo.CreateProxyFactory().GetFieldInterceptionProxy(null);
36-
37-
var additionalMembers = info.GetValue<MemberInfo[]>(AdditionalMemberName);
38-
if (additionalMembers == null)
39-
return;
40-
41-
foreach (var member in additionalMembers)
35+
var members = FormatterServices.GetSerializableMembers(_proxyFactoryInfo.PersistentClass, context);
36+
foreach (var member in members)
4237
{
4338
switch (member)
4439
{
4540
case FieldInfo field:
4641
field.SetValue(
47-
_deserializedProxy,
42+
proxy,
4843
info.GetValue(GetAdditionalMemberName(field), field.FieldType));
4944
break;
5045
case PropertyInfo property:
5146
property.SetValue(
52-
_deserializedProxy,
47+
proxy,
5348
info.GetValue(GetAdditionalMemberName(property), property.PropertyType));
5449
break;
5550
default:
5651
throw new NotSupportedException(
5752
$"Deserializing a member of type {member.GetType()} is not supported.");
5853
}
5954
}
55+
_deserializedProxy = proxy;
6056
}
6157
else
6258
{
6359
// Base type has a custom serialization, we need to call the proxy deserialization for deserializing
6460
// base type members too.
65-
var proxyType = _proxyFactoryInfo.CreateProxyFactory().GetFieldInterceptionProxy(null).GetType();
61+
var proxyType = proxy.GetType();
6662
var deserializationConstructor = proxyType.GetConstructor(
6763
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
6864
null,
@@ -95,8 +91,6 @@ public void GetBaseData(SerializationInfo info, StreamingContext context, object
9591
info.AddValue(HasAdditionalDataName, true);
9692

9793
var members = FormatterServices.GetSerializableMembers(proxyBaseType, context);
98-
info.AddValue(AdditionalMemberName, members);
99-
10094
foreach (var member in members)
10195
{
10296
switch (member)

src/NHibernate/Proxy/NHibernateProxyFactoryInfo.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ internal NHibernateProxyFactoryInfo(string entityName, System.Type persistentCla
2727
_componentIdType = componentIdType;
2828
}
2929

30+
internal System.Type PersistentClass => _persistentClass;
31+
3032
public IProxyFactory CreateProxyFactory()
3133
{
3234
var factory = new StaticProxyFactory();

0 commit comments

Comments
 (0)