2 // System.ComponentModel.TypeConverter.cs
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 // (C) 2002/2003 Ximian, Inc (http://www.ximian.com)
9 // (C) 2003 Andreas Nahr
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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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.
34 using System.Collections;
35 using System.ComponentModel.Design.Serialization;
36 using System.Globalization;
37 using System.Runtime.InteropServices;
39 namespace System.ComponentModel
42 public class TypeConverter
44 public bool CanConvertFrom (Type sourceType)
46 return CanConvertFrom (null, sourceType);
49 public virtual bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
51 return sourceType == typeof (InstanceDescriptor);
54 public bool CanConvertTo (Type destinationType)
56 return CanConvertTo (null, destinationType);
59 public virtual bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
61 return (destinationType == typeof (string));
64 public object ConvertFrom (object o)
66 return ConvertFrom (null, CultureInfo.CurrentCulture, o);
69 public virtual object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
71 if (value is InstanceDescriptor) {
72 return ((InstanceDescriptor) value).Invoke ();
75 return GetConvertFromException (value);
78 public object ConvertFromInvariantString (string text)
80 return ConvertFromInvariantString (null, text);
83 public object ConvertFromInvariantString (ITypeDescriptorContext context, string text)
85 return ConvertFromString (context, CultureInfo.InvariantCulture, text);
88 public object ConvertFromString (string text)
90 return ConvertFrom (text);
93 public object ConvertFromString (ITypeDescriptorContext context, string text)
95 return ConvertFromString (context, CultureInfo.CurrentCulture, text);
98 public object ConvertFromString (ITypeDescriptorContext context, CultureInfo culture, string text)
100 return ConvertFrom (context, culture, text);
103 public object ConvertTo (object value, Type destinationType)
105 return ConvertTo (null, null, value, destinationType);
108 public virtual object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value,
109 Type destinationType)
112 if (destinationType == null)
113 throw new ArgumentNullException ("destinationType");
115 if (destinationType == typeof (string)) {
120 return Convert.ToString (value, culture);
122 return value.ToString();
125 return GetConvertToException (value, destinationType);
128 public string ConvertToInvariantString (object value)
130 return ConvertToInvariantString (null, value);
133 public string ConvertToInvariantString (ITypeDescriptorContext context, object value)
135 return (string) ConvertTo (context, CultureInfo.InvariantCulture, value, typeof (string));
138 public string ConvertToString (object value)
140 return (string) ConvertTo (null, CultureInfo.CurrentCulture, value, typeof (string));
143 public string ConvertToString (ITypeDescriptorContext context, object value)
145 return (string) ConvertTo (context, CultureInfo.CurrentCulture, value, typeof (string));
148 public string ConvertToString (ITypeDescriptorContext context, CultureInfo culture, object value)
150 return (string) ConvertTo (context, culture, value, typeof (string));
153 protected Exception GetConvertFromException (object value)
155 string destinationType;
157 destinationType = "(null)";
159 destinationType = value.GetType ().FullName;
161 throw new NotSupportedException (string.Format (CultureInfo.InvariantCulture,
162 "{0} cannot convert from {1}.", this.GetType ().Name,
166 protected Exception GetConvertToException (object value, Type destinationType)
170 sourceType = "(null)";
172 sourceType = value.GetType ().FullName;
174 throw new NotSupportedException (string.Format (CultureInfo.InvariantCulture,
175 "'{0}' is unable to convert '{1}' to '{2}'.", this.GetType ().Name,
176 sourceType, destinationType.FullName));
179 public object CreateInstance (IDictionary propertyValues)
181 return CreateInstance (null, propertyValues);
184 public virtual object CreateInstance (ITypeDescriptorContext context, IDictionary propertyValues)
189 public bool GetCreateInstanceSupported ()
191 return GetCreateInstanceSupported (null);
194 public virtual bool GetCreateInstanceSupported (ITypeDescriptorContext context)
199 public PropertyDescriptorCollection GetProperties (object value)
201 return GetProperties (null, value);
204 public PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context, object value)
206 return GetProperties (context, value, new Attribute[1] { BrowsableAttribute.Yes });
209 public virtual PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context,
210 object value, Attribute[] attributes)
215 public bool GetPropertiesSupported ()
217 return GetPropertiesSupported (null);
220 public virtual bool GetPropertiesSupported (ITypeDescriptorContext context)
225 public ICollection GetStandardValues ()
227 return GetStandardValues (null);
230 public virtual StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
235 public bool GetStandardValuesExclusive ()
237 return GetStandardValuesExclusive (null);
240 public virtual bool GetStandardValuesExclusive (ITypeDescriptorContext context)
245 public bool GetStandardValuesSupported ()
247 return GetStandardValuesSupported (null);
250 public virtual bool GetStandardValuesSupported (ITypeDescriptorContext context)
255 public bool IsValid (object value)
257 return IsValid (null, value);
260 public virtual bool IsValid (ITypeDescriptorContext context, object value)
265 if (!this.CanConvertFrom(context, value.GetType()))
269 this.ConvertFrom(context, CultureInfo.InvariantCulture, value);
276 protected PropertyDescriptorCollection SortProperties (PropertyDescriptorCollection props, string[] names)
282 public class StandardValuesCollection : ICollection, IEnumerable
284 private ICollection values;
286 public StandardValuesCollection (ICollection values)
288 this.values = values;
291 void ICollection.CopyTo (Array array, int index) {
292 CopyTo (array, index);
295 public void CopyTo (Array array, int index)
297 values.CopyTo (array, index);
300 IEnumerator IEnumerable.GetEnumerator () {
301 return GetEnumerator ();
304 public IEnumerator GetEnumerator ()
306 return values.GetEnumerator ();
309 bool ICollection.IsSynchronized {
310 get { return false; }
313 object ICollection.SyncRoot {
317 int ICollection.Count {
318 get { return this.Count; }
322 get { return values.Count; }
325 public object this [int index] {
326 get { return ((IList) values) [index]; }
330 protected abstract class SimplePropertyDescriptor : PropertyDescriptor
332 private Type componentType;
333 private Type propertyType;
340 SimplePropertyDescriptor (Type componentType,
343 this (componentType, name, propertyType, null)
352 SimplePropertyDescriptor (Type componentType,
355 Attribute [] attributes) : base (name, attributes)
357 this.componentType = componentType;
358 this.propertyType = propertyType;
361 public override Type ComponentType {
362 get { return componentType; }
365 public override Type PropertyType {
366 get { return propertyType; }
369 public override bool IsReadOnly {
370 get { return Attributes.Contains (ReadOnlyAttribute.Yes); }
373 public override bool ShouldSerializeValue (object component)
378 public override bool CanResetValue (object component)
380 DefaultValueAttribute Attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
381 if (Attrib == null) {
384 return (Attrib.Value == GetValue (component));
387 public override void ResetValue (object component)
389 DefaultValueAttribute Attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
390 if (Attrib != null) {
391 SetValue (component, Attrib.Value);