The Labs \ Source Viewer \ SSCLI \ System.Runtime.Remoting.Proxies \ ProxyAttribute

  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:    ProxyAttribute.cs
  18. **
  19. **
  20. ** Purpose: Defines the attribute that is used on types which
  21. **          need custom proxies.
  22. **
  23. **
  24. ===========================================================*/
  25. namespace System.Runtime.Remoting.Proxies
  26. {
  27.    
  28.     using System.Reflection;
  29.     using System.Runtime.Remoting.Activation;
  30.     using System.Runtime.Remoting.Contexts;
  31.     using System.Security.Permissions;
  32.    
  33.     // Attribute for types that need custom proxies
  34.     [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
  35.     [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
  36.     [SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags = SecurityPermissionFlag.Infrastructure)]
  37.     [System.Runtime.InteropServices.ComVisible(true)]
  38.     public class ProxyAttribute : Attribute, IContextAttribute
  39.     {
  40.         public ProxyAttribute()
  41.         {
  42.             // Default constructor
  43.         }
  44.        
  45.         // Default implementation of CreateInstance uses our activation services to create an instance
  46.         // of the transparent proxy or an uninitialized marshalbyrefobject and returns it.
  47.        
  48.         public virtual MarshalByRefObject CreateInstance(Type serverType)
  49.         {
  50.             if (!serverType.IsContextful) {
  51.                 throw new RemotingException(Environment.GetResourceString("Remoting_Activation_MBR_ProxyAttribute"));
  52.             }
  53.             if (serverType.IsAbstract) {
  54.                 throw new RemotingException(Environment.GetResourceString("Acc_CreateAbst"));
  55.             }
  56.             return CreateInstanceInternal(serverType);
  57.         }
  58.        
  59.         internal MarshalByRefObject CreateInstanceInternal(Type serverType)
  60.         {
  61.             return ActivationServices.CreateInstance(serverType);
  62.         }
  63.        
  64.         // Default implementation of CreateProxy creates an instance of our
  65.         // remoting proxy
  66.        
  67.         public virtual RealProxy CreateProxy(ObjRef objRef, Type serverType, object serverObject, Context serverContext)
  68.         {
  69.             RemotingProxy rp = new RemotingProxy(serverType);
  70.            
  71.             // If this is a serverID, set the native context field in the TP
  72.             if (null != serverContext) {
  73.                 RealProxy.SetStubData(rp, serverContext.InternalContextID);
  74.             }
  75.            
  76.             if (objRef != null && objRef.GetServerIdentity().IsAllocated) {
  77.                 rp.SetSrvInfo(objRef.GetServerIdentity(), objRef.GetDomainID());
  78.             }
  79.            
  80.             // Set the flag indicating that the fields of the proxy
  81.             // have been initialized
  82.             rp.Initialized = true;
  83.            
  84.             // Sanity check
  85.             Type t = serverType;
  86.             if (!t.IsContextful && !t.IsMarshalByRef && (null != serverContext)) {
  87.                 throw new RemotingException(Environment.GetResourceString("Remoting_Activation_MBR_ProxyAttribute"));
  88.             }
  89.            
  90.             return rp;
  91.         }
  92.        
  93.         // implementation of interface IContextAttribute
  94.         [System.Runtime.InteropServices.ComVisible(true)]
  95.         public bool IsContextOK(Context ctx, IConstructionCallMessage msg)
  96.         {
  97.             // always happy...
  98.             return true;
  99.         }
  100.        
  101.         [System.Runtime.InteropServices.ComVisible(true)]
  102.         public void GetPropertiesForNewContext(IConstructionCallMessage msg)
  103.         {
  104.             // chill.. do nothing.
  105.             return;
  106.         }
  107.     }
  108. }

Developer Fusion