forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNHibernateProxyObjectReference.cs
60 lines (52 loc) · 2.01 KB
/
NHibernateProxyObjectReference.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
using System.Runtime.Serialization;
using System.Security;
namespace NHibernate.Proxy
{
[Serializable]
public sealed class NHibernateProxyObjectReference : IObjectReference, ISerializable
{
private readonly NHibernateProxyFactoryInfo _proxyFactoryInfo;
private readonly object _identifier;
private readonly object _implementation;
[Obsolete("Use overload taking an implementation parameter")]
public NHibernateProxyObjectReference(NHibernateProxyFactoryInfo proxyFactoryInfo, object identifier)
: this (proxyFactoryInfo, identifier, null)
{}
public NHibernateProxyObjectReference(NHibernateProxyFactoryInfo proxyFactoryInfo, object identifier, object implementation)
{
_proxyFactoryInfo = proxyFactoryInfo;
_identifier = identifier;
_implementation = implementation;
}
private NHibernateProxyObjectReference(SerializationInfo info, StreamingContext context)
{
_proxyFactoryInfo = (NHibernateProxyFactoryInfo) info.GetValue(nameof(_proxyFactoryInfo), typeof(NHibernateProxyFactoryInfo));
_identifier = info.GetValue(nameof(_identifier), typeof(object));
// 6.0 TODO: simplify with info.GetValue(nameof(_implementation), typeof(object));
foreach (var entry in info)
{
if (entry.Name == nameof(_implementation))
{
_implementation = entry.Value;
}
}
}
[SecurityCritical]
public object GetRealObject(StreamingContext context)
{
var proxy = _proxyFactoryInfo.CreateProxyFactory().GetProxy(_identifier, null);
if (_implementation != null)
proxy.HibernateLazyInitializer.SetImplementation(_implementation);
return proxy;
}
[SecurityCritical]
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
//Save a copy as it seems IObjectReference deserialization can't properly handle multiple objects with the same reference
info.AddValue(nameof(_proxyFactoryInfo), _proxyFactoryInfo.Clone());
info.AddValue(nameof(_identifier), _identifier);
info.AddValue(nameof(_implementation), _implementation);
}
}
}