2 // System.Web.Compilation.PersonalizableAttribute
5 // Marek Habersack (grendello@gmail.com)
7 // (C) 2006 Marek Habersack
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
31 using System.Collections;
32 using System.Collections.Generic;
33 using System.Reflection;
35 namespace System.Web.UI.WebControls.WebParts
37 [AttributeUsageAttribute(AttributeTargets.Property)]
38 public sealed class PersonalizableAttribute : Attribute
40 public static readonly PersonalizableAttribute Default;
41 public static readonly PersonalizableAttribute NotPersonalizable;
42 public static readonly PersonalizableAttribute Personalizable;
43 public static readonly PersonalizableAttribute SharedPersonalizable;
44 public static readonly PersonalizableAttribute UserPersonalizable;
46 bool isPersonalizable;
48 PersonalizationScope scope;
50 static PersonalizableAttribute ()
52 Default = new PersonalizableAttribute (false);
53 NotPersonalizable = Default;
54 Personalizable = new PersonalizableAttribute (PersonalizationScope.User, false);
55 SharedPersonalizable = new PersonalizableAttribute (PersonalizationScope.Shared, false);
56 UserPersonalizable = new PersonalizableAttribute (PersonalizationScope.User, false);
59 public PersonalizableAttribute () : this (true)
63 public PersonalizableAttribute (bool isPersonalizable)
65 this.isPersonalizable = isPersonalizable;
66 this.scope = PersonalizationScope.User;
67 this.isSensitive = false;
70 public PersonalizableAttribute (PersonalizationScope scope) : this (scope, false)
74 public PersonalizableAttribute (PersonalizationScope scope, bool isSensitive)
76 this.isPersonalizable = true;
78 this.isSensitive = isSensitive;
81 public bool IsPersonalizable {
82 get { return isPersonalizable; }
85 public bool IsSensitive {
86 get { return isSensitive; }
89 public PersonalizationScope Scope {
93 public override bool Equals (object obj)
95 PersonalizableAttribute attr = obj as PersonalizableAttribute;
99 return (this.isPersonalizable == attr.IsPersonalizable &&
100 this.isSensitive == attr.IsSensitive &&
101 this.scope == attr.Scope);
104 public override int GetHashCode ()
106 return (this.isPersonalizable.GetHashCode () ^
107 this.isSensitive.GetHashCode () ^
108 this.scope.GetHashCode ());
111 public static ICollection GetPersonalizableProperties (Type type)
114 throw new ArgumentNullException ("type");
116 PropertyInfo[] properties = type.GetProperties ();
117 if (properties == null || properties.Length == 0)
118 return new PropertyInfo [0];
119 List <PropertyInfo> ret = null;
121 foreach (PropertyInfo pi in properties)
122 if (PropertyQualifies (pi)) {
124 ret = new List <PropertyInfo> ();
130 static bool PropertyQualifies (PropertyInfo pi)
132 object[] attributes = pi.GetCustomAttributes (false);
133 if (attributes == null || attributes.Length == 0)
136 PersonalizableAttribute attr;
138 foreach (object a in attributes) {
139 attr = a as PersonalizableAttribute;
140 if (attr == null || !attr.IsPersonalizable)
142 mi = pi.GetSetMethod (false);
144 throw new HttpException ("A public property on the type is marked as personalizable but is read-only.");
151 public override bool IsDefaultAttribute ()
153 return PersonalizableAttribute.Equals (this, Default);
156 public override bool Match (object obj)
158 PersonalizableAttribute attr = obj as PersonalizableAttribute;
161 return (this.isPersonalizable == attr.IsPersonalizable);