forked from philiplaureano/LinFu.DynamicProxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProxyObjectReference.cs
60 lines (49 loc) · 1.88 KB
/
ProxyObjectReference.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.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;
using System.Reflection;
namespace LinFu.DynamicProxy
{
#if !SILVERLIGHT
[Serializable]
public class ProxyObjectReference : IObjectReference, ISerializable
{
private readonly Type _baseType;
private readonly IProxy _proxy;
protected ProxyObjectReference(SerializationInfo info, StreamingContext context)
{
// Deserialize the base type using its assembly qualified name
string qualifiedName = info.GetString("__baseType");
_baseType = Type.GetType(qualifiedName, true, false);
// Rebuild the list of interfaces
List<Type> interfaceList = new List<Type>();
int interfaceCount = info.GetInt32("__baseInterfaceCount");
for(int i = 0; i < interfaceCount; i++)
{
string keyName = string.Format("__baseInterface{0}", i);
string currentQualifiedName = info.GetString(keyName);
Type interfaceType = Type.GetType(currentQualifiedName, true, false);
interfaceList.Add(interfaceType);
}
// Reconstruct the proxy
ProxyFactory factory = new ProxyFactory();
Type proxyType = factory.CreateProxyType(_baseType, interfaceList.ToArray());
// Initialize the proxy with the deserialized data
object[] args = new object[] { info, context };
_proxy = (IProxy)Activator.CreateInstance(proxyType, args);
}
#region IObjectReference Members
public object GetRealObject(StreamingContext context)
{
return _proxy;
}
#endregion
#region ISerializable Members
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
}
#endregion
}
#endif
}