/src/LinFu.Proxy/ProxyObjectReference.cs

http://github.com/philiplaureano/LinFu · C# · 72 lines · 40 code · 10 blank · 22 comment · 1 complexity · 99e8535121907661b3874597337db183 MD5 · raw file

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