The Labs \ Source Viewer \ SSCLI \ System.Runtime.Remoting.Contexts \ IContextAttribute

  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:    ContextProperty.cs
  18. **
  19. ** A contextProperty is a name-value pair holding the property
  20. ** name and the object representing the property in a context.
  21. ** An array of these is returned by Context::GetContextProperties()
  22. **
  23. **
  24. **
  25. ===========================================================*/
  26. namespace System.Runtime.Remoting.Contexts
  27. {
  28.    
  29.     using System;
  30.     using System.Threading;
  31.     using System.Reflection;
  32.     using System.Runtime.InteropServices;
  33.     using System.Runtime.Remoting.Activation;
  34.     using System.Security.Permissions;
  35.    
  36.     /// <internalonly/>
  37.     [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
  38.     [SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags = SecurityPermissionFlag.Infrastructure)]
  39.     [System.Runtime.InteropServices.ComVisible(true)]
  40.     public class ContextProperty
  41.     {
  42.         internal string _name;
  43.         // property name
  44.         internal object _property;
  45.         // property object
  46.         /// <internalonly/>
  47.         public virtual string Name {
  48.             get { return _name; }
  49.         }
  50.        
  51.         /// <internalonly/>
  52.         public virtual object Property {
  53.             get { return _property; }
  54.         }
  55.        
  56. /* can't create outside the package */       
  57.         internal ContextProperty(string name, object prop)
  58.         {
  59.             _name = name;
  60.             _property = prop;
  61.         }
  62.     }
  63.    
  64.     // The IContextAttribute interface is implemented by attribute classes.
  65.     // The attributes contribute a property which resides in a context and
  66.     // enforces a specific policy for the objects created in that context.
  67.     /// <internalonly/>
  68.     [System.Runtime.InteropServices.ComVisible(true)]
  69.     public interface IContextAttribute
  70.     {
  71.         /// <internalonly/>
  72.         [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
  73.         bool IsContextOK(Context ctx, IConstructionCallMessage msg);
  74.         /// <internalonly/>
  75.         [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
  76.         void GetPropertiesForNewContext(IConstructionCallMessage msg);
  77.     }
  78.    
  79.     // This interface is exposed by the property contributed to a context
  80.     // by an attribute. By default, it is also implemented by the ContextAttribute
  81.     // base class which every attribute class must extend from.
  82.     /// <internalonly/>
  83.     [System.Runtime.InteropServices.ComVisible(true)]
  84.     public interface IContextProperty
  85.     {
  86.         /// <internalonly/>
  87.         // This is the name under which the property will be added
  88.         // to the {name,property} table in a context.
  89.         string Name {
  90.             [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
  91.             get;
  92.         }
  93.         /// <internalonly/>
  94.         // After forming the newCtx, we ask each property if it is happy
  95.         // with the context. We expect most implementations to say yes.
  96.         [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
  97.         bool IsNewContextOK(Context newCtx);
  98.         /// <internalonly/>
  99.        
  100.         // New method. All properties are notified when the context
  101.         // they are in is frozen.
  102.         [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
  103.         void Freeze(Context newContext);
  104.     }
  105.    
  106.     /// <internalonly/>
  107.     [System.Runtime.InteropServices.ComVisible(true)]
  108.     public interface IContextPropertyActivator
  109.     {
  110.         /// <internalonly/>
  111.         // This method lets properties in the current context have a say in
  112.         // whether an activation may be done 'here' or not.
  113.         [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
  114.         bool IsOKToActivate(IConstructionCallMessage msg);
  115.         /// <internalonly/>
  116.         [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
  117.         void CollectFromClientContext(IConstructionCallMessage msg);
  118.         /// <internalonly/>
  119.         [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
  120.         bool DeliverClientContextToServerContext(IConstructionCallMessage msg);
  121.         /// <internalonly/>
  122.         [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
  123.         void CollectFromServerContext(IConstructionReturnMessage msg);
  124.         /// <internalonly/>
  125.         [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
  126.         bool DeliverServerContextToClientContext(IConstructionReturnMessage msg);
  127.     }
  128.    
  129.    
  130.     // All context attribute classes must extend from this base class.
  131.     // This class provides the base implementations which the derived
  132.     // classes are free to over-ride. The base implementations provide
  133.     // the default answers to various questions.
  134.    
  135.     /// <internalonly/>
  136.     [AttributeUsage(AttributeTargets.Class), Serializable()]
  137.     [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
  138.     [SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags = SecurityPermissionFlag.Infrastructure)]
  139.     [System.Runtime.InteropServices.ComVisible(true)]
  140.     public class ContextAttribute : Attribute, IContextAttribute, IContextProperty
  141.     {
  142.         /// <internalonly/>
  143.         protected string AttributeName;
  144.        
  145.         // The derived class must call: base(name);
  146.         /// <internalonly/>
  147.         public ContextAttribute(string name)
  148.         {
  149.             AttributeName = name;
  150.         }
  151.        
  152.         // IContextPropery::Name
  153.         // Default implementation provides AttributeName as the property name.
  154.         /// <internalonly/>
  155.         public virtual string Name {
  156.             get { return AttributeName; }
  157.         }
  158.        
  159.         // IContextProperty::IsNewContextOK
  160.         /// <internalonly/>
  161.         public virtual bool IsNewContextOK(Context newCtx)
  162.         {
  163.             // This will be called before entering the newCtx
  164.             // Default implementation says OK.
  165.             return true;
  166.         }
  167.        
  168.         // IContextProperty::Freeze
  169.         // Default implementation does nothing
  170.         /// <internalonly/>
  171.         public virtual void Freeze(Context newContext)
  172.         {
  173.             BCLDebug.Log("ContextAttribute::ContextProperty::Freeze" + " for context " + newContext);
  174.         }
  175.        
  176.         // Object::Equals
  177.         // Default implementation just compares the names
  178.         /// <internalonly/>
  179.         public override bool Equals(object o)
  180.         {
  181.             IContextProperty prop = o as IContextProperty;
  182.             return (null != prop) && AttributeName.Equals(prop.Name);
  183.         }
  184.        
  185.         /// <internalonly/>
  186.         public override int GetHashCode()
  187.         {
  188.             return this.AttributeName.GetHashCode();
  189.         }
  190.        
  191.         // IContextAttribute::IsContextOK
  192.         // Default calls Object::Equals on the property and does not
  193.         // bother with the ctorMsg.
  194.         /// <internalonly/>
  195.         public virtual bool IsContextOK(Context ctx, IConstructionCallMessage ctorMsg)
  196.         {
  197.             if (ctx == null)
  198.                 throw new ArgumentNullException("ctx");
  199.             if (ctorMsg == null)
  200.                 throw new ArgumentNullException("ctorMsg");
  201.            
  202.             BCLDebug.Assert(ctorMsg.ActivationType.IsMarshalByRef, "Activation on a non MarshalByRef object");
  203.            
  204.             if (!ctorMsg.ActivationType.IsContextful) {
  205.                 return true;
  206.             }
  207.            
  208.             object prop = ctx.GetProperty(AttributeName);
  209.             if ((prop != null) && (Equals(prop))) {
  210.                 return true;
  211.             }
  212.             return false;
  213.         }
  214.        
  215.         // IContextAttribute::GetPropertiesForNewContext
  216.         // Default adds the attribute itself w/o regard to the current
  217.         // list of properties
  218.         /// <internalonly/>
  219.         public virtual void GetPropertiesForNewContext(IConstructionCallMessage ctorMsg)
  220.         {
  221.             if (ctorMsg == null)
  222.                 throw new ArgumentNullException("ctorMsg");
  223.             ctorMsg.ContextProperties.Add((IContextProperty)this);
  224.         }
  225.     }
  226.    
  227.    
  228. }

Developer Fusion