/src/LinFu.Proxy/ProxyObjectReference.cs
C# | 72 lines | 40 code | 10 blank | 22 comment | 1 complexity | 99e8535121907661b3874597337db183 MD5 | raw file
1using System; 2using System.Collections.Generic; 3using System.Runtime.Serialization; 4using LinFu.AOP.Interfaces; 5using LinFu.Proxy.Interfaces; 6 7namespace LinFu.Proxy 8{ 9 /// <summary> 10 /// Represents a helper class that deserializes proxy instances. 11 /// </summary> 12 [Serializable] 13 public class ProxyObjectReference : IObjectReference, ISerializable 14 { 15 private readonly Type _baseType; 16 private readonly IProxy _proxy; 17 18 /// <summary> 19 /// Initializes a new instance of the ProxyObjectReference class. 20 /// </summary> 21 /// <param name="info">The <see cref="SerializationInfo" /> class that contains the serialized data.</param> 22 /// <param name="context">The <see cref="StreamingContext" /> that describes the serialization state.</param> 23 protected ProxyObjectReference(SerializationInfo info, StreamingContext context) 24 { 25 // Deserialize the base type using its assembly qualified name 26 var qualifiedName = info.GetString("__baseType"); 27 _baseType = Type.GetType(qualifiedName, true, false); 28 29 // Rebuild the list of interfaces 30 var interfaceList = new List<Type>(); 31 var interfaceCount = info.GetInt32("__baseInterfaceCount"); 32 for (var i = 0; i < interfaceCount; i++) 33 { 34 var keyName = string.Format("__baseInterface{0}", i); 35 var currentQualifiedName = info.GetString(keyName); 36 var interfaceType = Type.GetType(currentQualifiedName, true, false); 37 38 interfaceList.Add(interfaceType); 39 } 40 41 // Reconstruct the proxy 42 var factory = new ProxyFactory(); 43 var proxyType = factory.CreateProxyType(_baseType, interfaceList.ToArray()); 44 _proxy = (IProxy) Activator.CreateInstance(proxyType); 45 46 var interceptor = (IInterceptor) info.GetValue("__interceptor", typeof(IInterceptor)); 47 _proxy.Interceptor = interceptor; 48 } 49 50 51 /// <summary> 52 /// Returns the deserialized proxy instance. 53 /// </summary> 54 /// <param name="context">The <see cref="StreamingContext" /> that describes the serialization state.</param> 55 /// <returns></returns> 56 public object GetRealObject(StreamingContext context) 57 { 58 return _proxy; 59 } 60 61 62 /// <summary> 63 /// Serializes the proxy to a stream. 64 /// </summary> 65 /// <remarks>This method override does nothing.</remarks> 66 /// <param name="info">The <see cref="SerializationInfo" /> class that contains the serialized data.</param> 67 /// <param name="context">The <see cref="StreamingContext" /> that describes the serialization state.</param> 68 public void GetObjectData(SerializationInfo info, StreamingContext context) 69 { 70 } 71 } 72}