ContextAttribute.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // System.Runtime.Remoting.Contexts.ContextAttribute..cs
  3. //
  4. // Author:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. // (C) Ximian, Inc. http://www.ximian.com
  8. //
  9. using System.Runtime.Remoting.Activation;
  10. using System.Collections;
  11. namespace System.Runtime.Remoting.Contexts {
  12. [AttributeUsage (AttributeTargets.Class)]
  13. [Serializable]
  14. public class ContextAttribute : Attribute, IContextAttribute, IContextProperty {
  15. protected string AttributeName;
  16. public ContextAttribute ()
  17. {
  18. }
  19. public ContextAttribute (string name)
  20. {
  21. AttributeName = name;
  22. }
  23. public virtual string Name {
  24. get {
  25. return AttributeName;
  26. }
  27. }
  28. public override bool Equals (object o)
  29. {
  30. if (o == null)
  31. return false;
  32. if (!(o is ContextAttribute))
  33. return false;
  34. ContextAttribute ca = (ContextAttribute) o;
  35. if (ca.AttributeName != AttributeName)
  36. return false;
  37. return true;
  38. }
  39. public virtual void Freeze (Context ctx)
  40. {
  41. }
  42. public override int GetHashCode ()
  43. {
  44. if (AttributeName == null)
  45. return 0;
  46. return AttributeName.GetHashCode ();
  47. }
  48. /// <summary>
  49. /// Adds the current context property to the IConstructionCallMessage
  50. /// </summary>
  51. public virtual void GetPropertiesForNewContext (IConstructionCallMessage msg)
  52. {
  53. if (msg == null)
  54. throw new ArgumentNullException ("IConstructionCallMessage");
  55. IList list = msg.ContextProperties;
  56. list.Add (this);
  57. }
  58. // <summary>
  59. // True whether the context arguments satisfies the requirements
  60. // of the current context.
  61. // </summary>
  62. public virtual bool IsContextOK (Context ctx, IConstructionCallMessage msg)
  63. {
  64. if (msg == null)
  65. throw new ArgumentNullException ("IConstructionCallMessage");
  66. if (ctx == null)
  67. throw new ArgumentNullException ("Context");
  68. if (!msg.ActivationType.IsContextful)
  69. return true;
  70. IContextProperty p = ctx.GetProperty (AttributeName);
  71. if (p == null)
  72. return false;
  73. if (this != p)
  74. return false;
  75. return true;
  76. }
  77. public virtual bool IsNewContextOK (Context ctx)
  78. {
  79. return true;
  80. }
  81. }
  82. }