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.
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
;
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;
79 this.isSensitive
= isSensitive
;
82 public bool IsPersonalizable
{
83 get { return isPersonalizable; }
86 public bool IsSensitive
{
87 get { return isSensitive; }
90 public PersonalizationScope Scope
{
94 public override bool Equals (object obj
)
96 PersonalizableAttribute attr
= obj
as PersonalizableAttribute
;
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
)
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
)) {
125 ret
= new List
<PropertyInfo
> ();
131 static bool PropertyQualifies (PropertyInfo pi
)
133 object[] attributes
= pi
.GetCustomAttributes (false);
134 if (attributes
== null || attributes
.Length
== 0)
137 PersonalizableAttribute attr
;
139 foreach (object a
in attributes
) {
140 attr
= a
as PersonalizableAttribute
;
141 if (attr
== null || !attr
.IsPersonalizable
)
143 mi
= pi
.GetSetMethod (false);
145 throw new HttpException ("A public property on the type is marked as personalizable but is read-only.");
152 public override bool IsDefaultAttribute ()
154 return PersonalizableAttribute
.Equals (this, Default
);
157 public override bool Match (object obj
)
159 PersonalizableAttribute attr
= obj
as PersonalizableAttribute
;
162 return (this.isPersonalizable
== attr
.IsPersonalizable
);