* SiteMapNode.cs (GetExplicitResourceString): implement.
[mono-project.git] / mcs / class / System.Web / System.Web.UI.WebControls.WebParts / PersonalizableAttribute.cs
blob7a6523825a82fcb61255e3efc18dab34fb090ffb
1 //
2 // System.Web.Compilation.PersonalizableAttribute
3 //
4 // Authors:
5 // Marek Habersack (grendello@gmail.com)
6 //
7 // (C) 2006 Marek Habersack
8 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 #if NET_2_0
31 using System;
32 using System.Collections;
33 using System.Collections.Generic;
34 using System.Reflection;
36 namespace System.Web.UI.WebControls.WebParts
38 [AttributeUsageAttribute(AttributeTargets.Property)]
39 public sealed class PersonalizableAttribute : Attribute
41 public static readonly PersonalizableAttribute Default;
42 public static readonly PersonalizableAttribute NotPersonalizable;
43 public static readonly PersonalizableAttribute Personalizable;
44 public static readonly PersonalizableAttribute SharedPersonalizable;
45 public static readonly PersonalizableAttribute UserPersonalizable;
47 bool isPersonalizable;
48 bool isSensitive;
49 PersonalizationScope scope;
51 static PersonalizableAttribute ()
53 Default = new PersonalizableAttribute (false);
54 NotPersonalizable = Default;
55 Personalizable = new PersonalizableAttribute (PersonalizationScope.User, false);
56 SharedPersonalizable = new PersonalizableAttribute (PersonalizationScope.Shared, false);
57 UserPersonalizable = new PersonalizableAttribute (PersonalizationScope.User, false);
60 public PersonalizableAttribute () : this (true)
64 public PersonalizableAttribute (bool isPersonalizable)
66 this.isPersonalizable = isPersonalizable;
67 this.scope = PersonalizationScope.User;
68 this.isSensitive = false;
71 public PersonalizableAttribute (PersonalizationScope scope) : this (scope, false)
75 public PersonalizableAttribute (PersonalizationScope scope, bool isSensitive)
77 this.isPersonalizable = true;
78 this.scope = scope;
79 this.isSensitive = isSensitive;
82 public bool IsPersonalizable {
83 get { return isPersonalizable; }
86 public bool IsSensitive {
87 get { return isSensitive; }
90 public PersonalizationScope Scope {
91 get { return scope; }
94 public override bool Equals (object obj)
96 PersonalizableAttribute attr = obj as PersonalizableAttribute;
97 if (attr == null)
98 return false;
100 return (this.isPersonalizable == attr.IsPersonalizable &&
101 this.isSensitive == attr.IsSensitive &&
102 this.scope == attr.Scope);
105 public override int GetHashCode ()
107 return (this.isPersonalizable.GetHashCode () ^
108 this.isSensitive.GetHashCode () ^
109 this.scope.GetHashCode ());
112 public static ICollection GetPersonalizableProperties (Type type)
114 if (type == null)
115 throw new ArgumentNullException ("type");
117 PropertyInfo[] properties = type.GetProperties ();
118 if (properties == null || properties.Length == 0)
119 return new PropertyInfo [0];
120 List <PropertyInfo> ret = null;
122 foreach (PropertyInfo pi in properties)
123 if (PropertyQualifies (pi)) {
124 if (ret == null)
125 ret = new List <PropertyInfo> ();
126 ret.Add (pi);
128 return ret;
131 static bool PropertyQualifies (PropertyInfo pi)
133 object[] attributes = pi.GetCustomAttributes (false);
134 if (attributes == null || attributes.Length == 0)
135 return false;
137 PersonalizableAttribute attr;
138 MethodInfo mi;
139 foreach (object a in attributes) {
140 attr = a as PersonalizableAttribute;
141 if (attr == null || !attr.IsPersonalizable)
142 continue;
143 mi = pi.GetSetMethod (false);
144 if (mi == null)
145 throw new HttpException ("A public property on the type is marked as personalizable but is read-only.");
146 return true;
149 return false;
152 public override bool IsDefaultAttribute ()
154 return PersonalizableAttribute.Equals (this, Default);
157 public override bool Match (object obj)
159 PersonalizableAttribute attr = obj as PersonalizableAttribute;
160 if (obj == null)
161 return false;
162 return (this.isPersonalizable == attr.IsPersonalizable);
166 #endif