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 using System.Collections;
14 using System.Globalization;
15 using System.Runtime.InteropServices;
17 namespace System.ComponentModel
20 public class TypeConverter
22 public bool CanConvertFrom (Type sourceType)
24 return CanConvertFrom (null, sourceType);
27 public virtual bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
32 public bool CanConvertTo (Type destinationType)
34 return CanConvertTo (null, destinationType);
37 public virtual bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
39 return (destinationType == typeof (string));
42 public object ConvertFrom (object o)
44 return ConvertFrom (null, CultureInfo.CurrentCulture, o);
47 public virtual object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
49 throw new NotSupportedException (this.ToString() + " cannot be created from '" +
50 value.GetType().ToString() + "'");
53 public object ConvertFromInvariantString (string text)
55 return ConvertFromInvariantString (null, text);
58 public object ConvertFromInvariantString (ITypeDescriptorContext context, string text)
60 return ConvertFromString (context, CultureInfo.InvariantCulture, text);
63 public object ConvertFromString (string text)
65 return ConvertFrom (text);
68 public object ConvertFromString (ITypeDescriptorContext context, string text)
70 return ConvertFromString (context, CultureInfo.CurrentCulture, text);
73 public object ConvertFromString (ITypeDescriptorContext context, CultureInfo culture, string text)
75 return ConvertFrom (context, culture, text);
78 public object ConvertTo (object value, Type destinationType)
80 return ConvertTo (null, null, value, destinationType);
83 public virtual object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value,
87 if (destinationType == null)
88 throw new ArgumentNullException ("destinationType");
90 if (destinationType == typeof (string)) {
92 return value.ToString();
96 throw new NotSupportedException ("Conversion not supported");
99 public string ConvertToInvariantString (object value)
101 return ConvertToInvariantString (null, value);
104 public string ConvertToInvariantString (ITypeDescriptorContext context, object value)
106 return (string) ConvertTo (context, CultureInfo.InvariantCulture, value, typeof (string));
109 public string ConvertToString (object value)
111 return (string) ConvertTo (null, CultureInfo.CurrentCulture, value, typeof (string));
114 public string ConvertToString (ITypeDescriptorContext context, object value)
116 return (string) ConvertTo (context, CultureInfo.CurrentCulture, value, typeof (string));
119 public string ConvertToString (ITypeDescriptorContext context, CultureInfo culture, object value)
121 return (string) ConvertTo (context, culture, value, typeof (string));
124 protected Exception GetConvertFromException (object value)
126 throw new NotSupportedException (this.ToString() + " cannot convert from '" +
127 value.GetType().ToString() + "'");
130 protected Exception GetConvertToException (object value, Type destinationType)
132 throw new NotSupportedException (this.ToString() + " cannot convert from '" +
133 value.GetType().ToString() + "' to '" + destinationType.ToString() + "'");
136 public object CreateInstance (IDictionary propertyValues)
138 return CreateInstance (null, propertyValues);
141 public virtual object CreateInstance (ITypeDescriptorContext context, IDictionary propertyValues)
146 public bool GetCreateInstanceSupported ()
148 return GetCreateInstanceSupported (null);
151 public virtual bool GetCreateInstanceSupported (ITypeDescriptorContext context)
156 public PropertyDescriptorCollection GetProperties (object value)
158 return GetProperties (null, value);
161 public PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context, object value)
163 return GetProperties (context, value, null);
166 public virtual PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context,
167 object value, Attribute[] attributes)
172 public bool GetPropertiesSupported ()
174 return GetPropertiesSupported (null);
177 public virtual bool GetPropertiesSupported (ITypeDescriptorContext context)
182 public ICollection GetStandardValues ()
184 return GetStandardValues (null);
187 public virtual StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
192 public bool GetStandardValuesExclusive ()
194 return GetStandardValuesExclusive (null);
197 public virtual bool GetStandardValuesExclusive (ITypeDescriptorContext context)
202 public bool GetStandardValuesSupported ()
204 return GetStandardValuesSupported (null);
207 public virtual bool GetStandardValuesSupported (ITypeDescriptorContext context)
212 public bool IsValid (object value)
214 return IsValid (null, value);
217 public virtual bool IsValid (ITypeDescriptorContext context, object value)
222 protected PropertyDescriptorCollection SortProperties (PropertyDescriptorCollection props, string[] names)
228 public class StandardValuesCollection : ICollection, IEnumerable
230 private ICollection values;
232 public StandardValuesCollection (ICollection values)
234 this.values = values;
237 public void CopyTo (Array array, int index)
239 values.CopyTo (array, index);
242 public IEnumerator GetEnumerator ()
244 return values.GetEnumerator ();
247 bool ICollection.IsSynchronized {
248 get { return false; }
251 object ICollection.SyncRoot {
255 int ICollection.Count {
256 get { return this.Count; }
260 get { return values.Count; }
263 public object this [int index] {
264 get { return ((IList) values) [index]; }
268 protected abstract class SimplePropertyDescriptor : PropertyDescriptor
270 private Type componentType;
271 private Type propertyType;
273 public SimplePropertyDescriptor (Type componentType,
276 this (componentType, name, propertyType, null)
280 public SimplePropertyDescriptor (Type componentType,
283 Attribute [] attributes) : base (name, attributes)
285 this.componentType = componentType;
286 this.propertyType = propertyType;
289 public override Type ComponentType {
290 get { return componentType; }
293 public override Type PropertyType {
294 get { return propertyType; }
297 public override bool IsReadOnly {
298 get { return Attributes.Contains (ReadOnlyAttribute.Yes); }
301 public override bool ShouldSerializeValue (object component)
306 public override bool CanResetValue (object component)
308 DefaultValueAttribute Attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
309 if (Attrib == null) {
312 return (Attrib.Value == GetValue (component));
315 public override void ResetValue (object component)
317 DefaultValueAttribute Attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
318 if (Attrib != null) {
319 SetValue (component, Attrib.Value);