Reject incomplete conversion unary user operators. Fixes #7935.
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls.WebParts / PersonalizableAttribute.cs
1 //
2 // System.Web.Compilation.PersonalizableAttribute
3 //
4 // Authors:
5 //   Marek Habersack (grendello@gmail.com)
6 //
7 // (C) 2006 Marek Habersack
8 //
9
10 //
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.
29 //
30 #if NET_2_0
31 using System;
32 using System.Collections;
33 using System.Collections.Generic;
34 using System.Reflection;
35
36 namespace System.Web.UI.WebControls.WebParts
37 {
38         [AttributeUsageAttribute(AttributeTargets.Property)] 
39         public sealed class PersonalizableAttribute : Attribute
40         {
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;
46
47                 bool isPersonalizable;
48                 bool isSensitive;
49                 PersonalizationScope scope;
50                 
51                 static PersonalizableAttribute ()
52                 {
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);
58                 }
59                 
60                 public PersonalizableAttribute () : this (true)
61                 {
62                 }
63
64                 public PersonalizableAttribute (bool isPersonalizable)
65                 {
66                         this.isPersonalizable = isPersonalizable;
67                         this.scope = PersonalizationScope.User;
68                         this.isSensitive = false;
69                 }
70
71                 public PersonalizableAttribute (PersonalizationScope scope) : this (scope, false)
72                 {
73                 }
74
75                 public PersonalizableAttribute (PersonalizationScope scope, bool isSensitive)
76                 {
77                         this.isPersonalizable = true;
78                         this.scope = scope;
79                         this.isSensitive = isSensitive;
80                 }
81
82                 public bool IsPersonalizable {
83                         get { return isPersonalizable; }
84                 }
85
86                 public bool IsSensitive {
87                         get { return isSensitive; }
88                 }
89
90                 public PersonalizationScope Scope {
91                         get { return scope; }
92                 }
93
94                 public override bool Equals (object obj)
95                 {
96                         PersonalizableAttribute attr = obj as PersonalizableAttribute;
97                         if (attr == null)
98                                 return false;
99
100                         return (this.isPersonalizable == attr.IsPersonalizable &&
101                                 this.isSensitive == attr.IsSensitive &&
102                                 this.scope == attr.Scope);
103                 }
104                 
105                 public override int GetHashCode ()
106                 {
107                         return (this.isPersonalizable.GetHashCode () ^
108                                 this.isSensitive.GetHashCode () ^
109                                 this.scope.GetHashCode ());
110                 }
111
112                 public static ICollection GetPersonalizableProperties (Type type)
113                 {
114                         if (type == null)
115                                 throw new ArgumentNullException ("type");
116                         
117                         PropertyInfo[] properties = type.GetProperties ();
118                         if (properties == null || properties.Length == 0)
119                                 return new PropertyInfo [0];
120                         List <PropertyInfo> ret = null;
121                         
122                         foreach (PropertyInfo pi in properties)
123                                 if (PropertyQualifies (pi)) {
124                                         if (ret == null)
125                                                 ret = new List <PropertyInfo> ();
126                                         ret.Add (pi);
127                                 }
128                         return ret;
129                 }
130
131                 static bool PropertyQualifies (PropertyInfo pi)
132                 {
133                         object[] attributes = pi.GetCustomAttributes (false);
134                         if (attributes == null || attributes.Length == 0)
135                                 return false;
136
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;
147                         }
148
149                         return false;
150                 }
151
152                 public override bool IsDefaultAttribute ()
153                 {
154                         return PersonalizableAttribute.Equals (this, Default);
155                 }
156
157                 public override bool Match (object obj)
158                 {
159                         PersonalizableAttribute attr = obj as PersonalizableAttribute;
160                         if (obj == null)
161                                 return false;
162                         return (this.isPersonalizable == attr.IsPersonalizable);
163                 }
164         }
165 }
166 #endif