The Labs \ Source Viewer \ SSCLI \ System.Runtime.Remoting \ ObjectHandle

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. /*============================================================
  16. **
  17. ** Class:  ObjectHandle
  18. **
  19. **
  20. ** ObjectHandle wraps object references. A Handle allows a
  21. ** marshal by value object to be returned through an
  22. ** indirection allowing the caller to control when the
  23. ** object is loaded into their domain.
  24. **
  25. **
  26. ===========================================================*/
  27. namespace System.Runtime.Remoting
  28. {
  29.    
  30.     using System;
  31.     using System.Security.Permissions;
  32.     using System.Runtime.InteropServices;
  33.     using System.Runtime.Remoting;
  34.     using System.Runtime.Remoting.Activation;
  35.     using System.Runtime.Remoting.Lifetime;
  36.    
  37.     [ClassInterface(ClassInterfaceType.AutoDual)]
  38.     [System.Runtime.InteropServices.ComVisible(true)]
  39.     public class ObjectHandle : MarshalByRefObject, IObjectHandle
  40.     {
  41.         private object WrappedObject;
  42.        
  43.         private ObjectHandle()
  44.         {
  45.         }
  46.        
  47.         public ObjectHandle(object o)
  48.         {
  49.             WrappedObject = o;
  50.         }
  51.        
  52.         public object Unwrap()
  53.         {
  54.             return WrappedObject;
  55.         }
  56.        
  57.         // ObjectHandle has a finite lifetime. For now the default
  58.         // lifetime is being used, this can be changed in this method to
  59.         // specify a custom lifetime.
  60.         [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
  61.         public override object InitializeLifetimeService()
  62.         {
  63.             BCLDebug.Trace("REMOTE", "ObjectHandle.InitializeLifetimeService");
  64.            
  65.             //
  66.             // If the wrapped object has implemented InitializeLifetimeService to return null,
  67.             // we don't want to go to the base class (which will result in a lease being
  68.             // requested from the MarshalByRefObject, which starts up the LeaseManager,
  69.             // which starts up the ThreadPool, adding three threads to the process.
  70.             // We check if the wrapped object is a MarshalByRef object, and call InitializeLifetimeServices on it
  71.             // and if it returns null, we return null. Otherwise we fall back to the old behavior.
  72.             //
  73.            
  74.             MarshalByRefObject mbr = WrappedObject as MarshalByRefObject;
  75.             if (mbr != null) {
  76.                 object o = mbr.InitializeLifetimeService();
  77.                 if (o == null)
  78.                     return null;
  79.             }
  80.             ILease lease = (ILease)base.InitializeLifetimeService();
  81.             return lease;
  82.         }
  83.     }
  84. }

Developer Fusion