The Labs \ Source Viewer \ SSCLI \ System \ MarshalByRefObject

  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. ** File:    MarshalByRefObject.cs
  18. **
  19. **             
  20. **
  21. ** Purpose: Defines the root type for all marshal by reference aka
  22. **          AppDomain bound types
  23. **         
  24. **
  25. **
  26. ===========================================================*/
  27. namespace System
  28. {
  29.    
  30.     using System;
  31.     using System.Security;
  32.     using System.Security.Permissions;
  33.     using System.Threading;
  34.     using System.Runtime.Remoting;
  35.     using System.Runtime.Remoting.Lifetime;
  36.     using System.Runtime.Remoting.Services;
  37.     using System.Runtime.InteropServices;
  38.     using System.Reflection;
  39.     using System.Runtime.CompilerServices;
  40.     using CultureInfo = System.Globalization.CultureInfo;
  41.    
  42.     [Serializable()]
  43.     [System.Runtime.InteropServices.ComVisible(true)]
  44.     public abstract class MarshalByRefObject
  45.     {
  46.         private object __identity;
  47.        
  48.         private object Identity {
  49.             get { return __identity; }
  50.             set { __identity = value; }
  51.         }
  52.        
  53.         // (1) for remote COM objects IsInstance of can't be executed on
  54.         // the proxies, so we need this method to be executed on the
  55.         // actual object.
  56.         // (2) for remote objects that do not have the complete type information
  57.         // we intercept calls to check the type and execute it on the actual
  58.         // object
  59.         internal bool IsInstanceOfType(Type T)
  60.         {
  61.             return T.IsInstanceOfType(this);
  62.         }
  63.        
  64.         // for remote COM Objects the late binding methods can't be
  65.         // executed on proxies, so we need this method to execute on
  66.         // the real object
  67.         internal object InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters)
  68.         {
  69.             Type t = GetType();
  70.            
  71.             // Sanity check
  72.             if (!t.IsCOMObject)
  73.                 throw new InvalidOperationException(Environment.GetResourceString("Arg_InvokeMember"));
  74.            
  75.             // Call into the runtime to invoke on the COM object.
  76.             return t.InvokeMember(name, invokeAttr, binder, this, args, modifiers, culture, namedParameters);
  77.         }
  78.        
  79.         // Returns a new cloned MBR instance that is a memberwise copy of this
  80.         // with the identity nulled out, so there are no identity conflicts
  81.         // when the cloned object is marshalled
  82.         protected MarshalByRefObject MemberwiseClone(bool cloneIdentity)
  83.         {
  84.             MarshalByRefObject mbr = (MarshalByRefObject)base.MemberwiseClone();
  85.             // set the identity on the cloned object to null
  86.             if (!cloneIdentity)
  87.                 mbr.Identity = null;
  88.             return mbr;
  89.         }
  90.        
  91.         // A helper routine to extract the identity either from the marshalbyrefobject base
  92.         // class if it is not a proxy, otherwise from the real proxy.
  93.         // A flag is set to indicate whether the object passed in is a server or a proxy
  94.         static internal Identity GetIdentity(MarshalByRefObject obj, out bool fServer)
  95.         {
  96.             fServer = true;
  97.             Identity id = null;
  98.            
  99.             if (null != obj) {
  100.                 if (!RemotingServices.IsTransparentProxy(obj)) {
  101.                     id = (Identity)obj.Identity;
  102.                 }
  103.                 else {
  104.                     // Toggle flag to indicate that we have a proxy
  105.                     fServer = false;
  106.                     id = RemotingServices.GetRealProxy(obj).IdentityObject;
  107.                 }
  108.             }
  109.            
  110.             return id;
  111.         }
  112.        
  113.         // Another helper that delegates to the helper above
  114.         static internal Identity GetIdentity(MarshalByRefObject obj)
  115.         {
  116.             BCLDebug.Assert(!RemotingServices.IsTransparentProxy(obj), "Use this method for server objects only");
  117.            
  118.             bool fServer;
  119.             return GetIdentity(obj, out fServer);
  120.         }
  121.        
  122.         internal ServerIdentity __RaceSetServerIdentity(ServerIdentity id)
  123.         {
  124.             if (__identity == null) {
  125.                 // For strictly MBR types, the TP field in the identity
  126.                 // holds the real server
  127.                 if (!id.IsContextBound) {
  128.                     id.RaceSetTransparentProxy(this);
  129.                 }
  130.                 Interlocked.CompareExchange(ref __identity, id, null);
  131.             }
  132.             return (ServerIdentity)__identity;
  133.         }
  134.        
  135.        
  136.         internal void __ResetServerIdentity()
  137.         {
  138.             __identity = null;
  139.         }
  140.        
  141.         // This method is used return a lifetime service object which
  142.         // is used to control the lifetime policy to the object.
  143.         // For the default Lifetime service this will be an object of typoe ILease.
  144.         //
  145.         [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
  146.         public object GetLifetimeService()
  147.         {
  148.             return LifetimeServices.GetLease(this);
  149.         }
  150.        
  151.         // This method is used return lifetime service object. This method
  152.         // can be overridden to return a LifetimeService object with properties unique to
  153.         // this object.
  154.         // For the default Lifetime service this will be an object of type ILease.
  155.         //
  156.         [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
  157.         public virtual object InitializeLifetimeService()
  158.         {
  159.             return LifetimeServices.GetLeaseInitial(this);
  160.         }
  161.        
  162.         [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
  163.         public virtual ObjRef CreateObjRef(Type requestedType)
  164.         {
  165.             if (__identity == null) {
  166.                 throw new RemotingException(Environment.GetResourceString("Remoting_NoIdentityEntry"));
  167.             }
  168.             return new ObjRef(this, requestedType);
  169.         }
  170.        
  171.        
  172.         // This is for casting interop ObjRefLite's.
  173.         // ObjRefLite's have been deprecated. These methods are not exposed
  174.         // through any user APIs and would be removed in the future
  175.         internal bool CanCastToXmlType(string xmlTypeName, string xmlTypeNamespace)
  176.         {
  177.             Type castType = SoapServices.GetInteropTypeFromXmlType(xmlTypeName, xmlTypeNamespace);
  178.             if (castType == null) {
  179.                 string typeNamespace;
  180.                 string assemblyName;
  181.                 if (!SoapServices.DecodeXmlNamespaceForClrTypeNamespace(xmlTypeNamespace, out typeNamespace, out assemblyName))
  182.                     return false;
  183.                
  184.                 string typeName;
  185.                 if ((typeNamespace != null) && (typeNamespace.Length > 0))
  186.                     typeName = typeNamespace + "." + xmlTypeName;
  187.                 else
  188.                     typeName = xmlTypeName;
  189.                
  190.                 try {
  191.                     Assembly asm = Assembly.Load(assemblyName);
  192.                     castType = asm.GetType(typeName, false, false);
  193.                 }
  194.                 catch {
  195.                     return false;
  196.                 }
  197.             }
  198.            
  199.             if (castType != null)
  200.                 return castType.IsAssignableFrom(this.GetType());
  201.            
  202.             return false;
  203.         }
  204.         // CanCastToXmlType
  205.         // helper method for calling CanCastToXmlType
  206.         // ObjRefLite's have been deprecated. These methods are not exposed
  207.         // through any user APIs and would be removed in the future
  208.         static internal bool CanCastToXmlTypeHelper(Type castType, MarshalByRefObject o)
  209.         {
  210.             if (castType == null)
  211.                 throw new ArgumentNullException("castType");
  212.            
  213.             // MarshalByRefObject's can only be casted to MarshalByRefObject's or interfaces.
  214.             if (!castType.IsInterface && !castType.IsMarshalByRef)
  215.                 return false;
  216.            
  217.             // figure out xml type name
  218.             string xmlTypeName = null;
  219.             string xmlTypeNamespace = null;
  220.             if (!SoapServices.GetXmlTypeForInteropType(castType, out xmlTypeName, out xmlTypeNamespace)) {
  221.                 // There's no registered interop type name, so just use the default.
  222.                 xmlTypeName = castType.Name;
  223.                 xmlTypeNamespace = SoapServices.CodeXmlNamespaceForClrTypeNamespace(castType.Namespace, castType.Module.Assembly.nGetSimpleName());
  224.             }
  225.            
  226.             return o.CanCastToXmlType(xmlTypeName, xmlTypeNamespace);
  227.         }
  228.         // CanCastToXmlType
  229.     }
  230. }
  231. // namespace

Developer Fusion