Implement MachineKey.Protect and MachineKey.Unprotect
[mono.git] / mcs / class / System / System.ComponentModel / TypeConverter.cs
1 //
2 // System.ComponentModel.TypeConverter.cs
3 //
4 // Authors:
5 //   Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //
8 // (C) 2002/2003 Ximian, Inc (http://www.ximian.com)
9 // (C) 2003 Andreas Nahr
10 //
11
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System;
34 using System.Collections;
35 using System.ComponentModel.Design.Serialization;
36 using System.Globalization;
37 using System.Runtime.InteropServices;
38
39 namespace System.ComponentModel
40 {
41         [ComVisible (true)]
42         public class TypeConverter
43         {
44                 public bool CanConvertFrom (Type sourceType)
45                 {
46                         return CanConvertFrom (null, sourceType);
47                 }
48
49                 public virtual bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
50                 {
51                         return sourceType == typeof (InstanceDescriptor);
52                 }
53
54                 public bool CanConvertTo (Type destinationType)
55                 {
56                         return CanConvertTo (null, destinationType);
57                 }
58
59                 public virtual bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
60                 {
61                         return (destinationType == typeof (string));
62                 }
63
64                 public object ConvertFrom (object o)
65                 {
66                         return ConvertFrom (null, CultureInfo.CurrentCulture, o);
67                 }
68
69                 public virtual object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
70                 {
71                         if (value is InstanceDescriptor) {
72                                 return ((InstanceDescriptor) value).Invoke ();
73                         }
74
75                         return GetConvertFromException (value);
76                 }
77
78                 public object ConvertFromInvariantString (string text)
79                 {
80                         return ConvertFromInvariantString (null, text); 
81                 }
82
83                 public object ConvertFromInvariantString (ITypeDescriptorContext context, string text)
84                 {
85                         return ConvertFromString (context, CultureInfo.InvariantCulture, text);
86                 }
87
88                 public object ConvertFromString (string text)
89                 {
90                         return ConvertFrom (text);
91                 }
92
93                 public object ConvertFromString (ITypeDescriptorContext context, string text)
94                 {
95                         return ConvertFromString (context, CultureInfo.CurrentCulture, text);
96                 }
97
98                 public object ConvertFromString (ITypeDescriptorContext context, CultureInfo culture, string text)
99                 {
100                         return ConvertFrom (context, culture, text);
101                 }
102
103                 public object ConvertTo (object value, Type destinationType)
104                 {
105                         return ConvertTo (null, null, value, destinationType);
106                 }
107
108                 public virtual object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value,
109                                                  Type destinationType)
110                 {
111                         // context?
112                         if (destinationType == null)
113                                 throw new ArgumentNullException ("destinationType");
114
115                         if (destinationType == typeof (string)) {
116                                 if (value == null)
117                                         return String.Empty;
118
119                                 if (culture != null)
120                                         return Convert.ToString (value, culture);
121
122                                 return value.ToString();
123                         }
124
125                         return GetConvertToException (value, destinationType);
126                 }
127
128                 public string ConvertToInvariantString (object value)
129                 {
130                         return ConvertToInvariantString (null, value);
131                 }
132
133                 public string ConvertToInvariantString (ITypeDescriptorContext context, object value)
134                 {
135                         return (string) ConvertTo (context, CultureInfo.InvariantCulture, value, typeof (string));
136                 }
137
138                 public string ConvertToString (object value)
139                 {
140                         return (string) ConvertTo (null, CultureInfo.CurrentCulture, value, typeof (string));
141                 }
142
143                 public string ConvertToString (ITypeDescriptorContext context, object value)
144                 {
145                         return (string) ConvertTo (context, CultureInfo.CurrentCulture, value, typeof (string));
146                 }
147
148                 public string ConvertToString (ITypeDescriptorContext context, CultureInfo culture, object value)
149                 {
150                         return (string) ConvertTo (context, culture, value, typeof (string));
151                 }
152
153                 protected Exception GetConvertFromException (object value)
154                 {
155                         string destinationType;
156                         if (value == null)
157                                 destinationType = "(null)";
158                         else
159                                 destinationType = value.GetType ().FullName;
160
161                         throw new NotSupportedException (string.Format (CultureInfo.InvariantCulture,
162                                 "{0} cannot convert from {1}.", this.GetType ().Name,
163                                 destinationType));
164                 }
165
166                 protected Exception GetConvertToException (object value, Type destinationType)
167                 {
168                         string sourceType;
169                         if (value == null)
170                                 sourceType = "(null)";
171                         else
172                                 sourceType = value.GetType ().FullName;
173
174                         throw new NotSupportedException (string.Format (CultureInfo.InvariantCulture,
175                                 "'{0}' is unable to convert '{1}' to '{2}'.", this.GetType ().Name,
176                                 sourceType, destinationType.FullName));
177                 }
178
179                 public object CreateInstance (IDictionary propertyValues)
180                 {
181                         return CreateInstance (null, propertyValues);
182                 }
183
184                 public virtual object CreateInstance (ITypeDescriptorContext context, IDictionary propertyValues)
185                 {
186                         return null;
187                 }
188
189                 public bool GetCreateInstanceSupported ()
190                 {
191                         return GetCreateInstanceSupported (null);
192                 }
193
194                 public virtual bool GetCreateInstanceSupported (ITypeDescriptorContext context)
195                 {
196                         return false;
197                 }
198
199                 public PropertyDescriptorCollection GetProperties (object value)
200                 {
201                         return GetProperties (null, value);
202                 }
203
204                 public PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context, object value)
205                 {
206                         return GetProperties (context, value, new Attribute[1] { BrowsableAttribute.Yes });
207                 }
208
209                 public virtual PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context,
210                                                                            object value, Attribute[] attributes)
211                 {
212                         return null;
213                 }
214
215                 public bool GetPropertiesSupported ()
216                 {
217                         return GetPropertiesSupported (null);
218                 }
219
220                 public virtual bool GetPropertiesSupported (ITypeDescriptorContext context)
221                 {
222                         return false;
223                 }
224
225                 public ICollection GetStandardValues ()
226                 {
227                         return GetStandardValues (null);
228                 }
229
230                 public virtual StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
231                 {
232                         return null;
233                 }
234
235                 public bool GetStandardValuesExclusive ()
236                 {
237                         return GetStandardValuesExclusive (null);
238                 }
239
240                 public virtual bool GetStandardValuesExclusive (ITypeDescriptorContext context)
241                 {
242                         return false;
243                 }
244
245                 public bool GetStandardValuesSupported ()
246                 {
247                         return GetStandardValuesSupported (null);
248                 }
249
250                 public virtual bool GetStandardValuesSupported (ITypeDescriptorContext context)
251                 {
252                         return false;
253                 }
254
255                 public bool IsValid (object value)
256                 {
257                         return IsValid (null, value);
258                 }
259
260                 public virtual bool IsValid (ITypeDescriptorContext context, object value)
261                 {
262                         if (value == null)
263                                 return false;
264
265                 if (!this.CanConvertFrom(context, value.GetType()))
266                         return false;
267         
268                 try {
269                         this.ConvertFrom(context, CultureInfo.InvariantCulture, value);
270                         return true;
271                 } catch {
272                         return false;
273                 }
274                 }
275
276                 protected PropertyDescriptorCollection SortProperties (PropertyDescriptorCollection props, string[] names)
277                 {
278                         props.Sort (names);
279                         return props; 
280                 }
281
282                 public class StandardValuesCollection : ICollection, IEnumerable
283                 {
284                         private ICollection values;
285
286                         public StandardValuesCollection (ICollection values)
287                         {
288                                 this.values = values;
289                         }
290
291                         void ICollection.CopyTo (Array array, int index) {
292                                 CopyTo (array, index);
293                         }
294
295                         public void CopyTo (Array array, int index)
296                         {
297                                 values.CopyTo (array, index);
298                         }
299
300                         IEnumerator IEnumerable.GetEnumerator () {
301                                 return GetEnumerator ();
302                         }
303
304                         public IEnumerator GetEnumerator ()
305                         {
306                                 return values.GetEnumerator ();
307                         }
308
309                         bool ICollection.IsSynchronized {
310                                 get { return false; }
311                         }
312
313                         object ICollection.SyncRoot {
314                                 get { return null; }
315                         }
316
317                         int ICollection.Count {
318                                 get { return this.Count; }
319                         }
320
321                         public int Count {
322                                 get { return values.Count; }
323                         }
324
325                         public object this [int index] {
326                                 get { return ((IList) values) [index]; }
327                         }
328                 }
329
330                 protected abstract class SimplePropertyDescriptor : PropertyDescriptor
331                 {
332                         private Type componentType;
333                         private Type propertyType;
334
335 #if NET_4_0
336                         protected
337 #else
338                         public
339 #endif
340                         SimplePropertyDescriptor (Type componentType,
341                                                          string name,
342                                                          Type propertyType) :
343                                 this (componentType, name, propertyType, null)
344                         {
345                         }
346
347 #if NET_4_0
348                         protected
349 #else
350                         public
351 #endif
352                         SimplePropertyDescriptor (Type componentType,
353                                                          string name,
354                                                          Type propertyType,
355                                                          Attribute [] attributes) : base (name, attributes)
356                         {
357                                 this.componentType = componentType;
358                                 this.propertyType = propertyType;
359                         }
360
361                         public override Type ComponentType {
362                                 get { return componentType; }
363                         }
364
365                         public override Type PropertyType {
366                                 get { return propertyType; }
367                         }
368
369                         public override bool IsReadOnly {
370                                 get { return Attributes.Contains (ReadOnlyAttribute.Yes); }
371                         }
372
373                         public override bool ShouldSerializeValue (object component)
374                         {
375                                         return false; 
376                         }
377
378                         public override bool CanResetValue (object component)
379                         {
380                                 DefaultValueAttribute Attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
381                                 if (Attrib == null) {
382                                         return false; 
383                                 }
384                                 return (Attrib.Value == GetValue (component)); 
385                         }
386
387                         public override void ResetValue (object component)
388                         {
389                                 DefaultValueAttribute Attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
390                                 if (Attrib != null) {
391                                         SetValue (component, Attrib.Value); 
392                                 }
393  
394                         } 
395                 }
396         }
397 }
398