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