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 if (sourceType == typeof (InstanceDescriptor)) {
58 public bool CanConvertTo (Type destinationType)
60 return CanConvertTo (null, destinationType);
63 public virtual bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
65 return (destinationType == typeof (string));
68 public object ConvertFrom (object o)
70 return ConvertFrom (null, CultureInfo.CurrentCulture, o);
73 public virtual object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
75 if (value is InstanceDescriptor) {
76 return ((InstanceDescriptor) value).Invoke ();
79 throw new NotSupportedException ("Cannot convert from null");
80 throw new NotSupportedException (this.ToString() + " cannot be created from '" +
81 value.GetType().ToString() + "'");
84 public object ConvertFromInvariantString (string text)
86 return ConvertFromInvariantString (null, text);
89 public object ConvertFromInvariantString (ITypeDescriptorContext context, string text)
91 return ConvertFromString (context, CultureInfo.InvariantCulture, text);
94 public object ConvertFromString (string text)
96 return ConvertFrom (text);
99 public object ConvertFromString (ITypeDescriptorContext context, string text)
101 return ConvertFromString (context, CultureInfo.CurrentCulture, text);
104 public object ConvertFromString (ITypeDescriptorContext context, CultureInfo culture, string text)
106 return ConvertFrom (context, culture, text);
109 public object ConvertTo (object value, Type destinationType)
111 return ConvertTo (null, null, value, destinationType);
114 public virtual object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value,
115 Type destinationType)
118 if (destinationType == null)
119 throw new ArgumentNullException ("destinationType");
121 if (destinationType == typeof (string)) {
123 return value.ToString();
127 throw new NotSupportedException ("Conversion not supported");
130 public string ConvertToInvariantString (object value)
132 return ConvertToInvariantString (null, value);
135 public string ConvertToInvariantString (ITypeDescriptorContext context, object value)
137 return (string) ConvertTo (context, CultureInfo.InvariantCulture, value, typeof (string));
140 public string ConvertToString (object value)
142 return (string) ConvertTo (null, CultureInfo.CurrentCulture, value, typeof (string));
145 public string ConvertToString (ITypeDescriptorContext context, object value)
147 return (string) ConvertTo (context, CultureInfo.CurrentCulture, value, typeof (string));
150 public string ConvertToString (ITypeDescriptorContext context, CultureInfo culture, object value)
152 return (string) ConvertTo (context, culture, value, typeof (string));
155 protected Exception GetConvertFromException (object value)
157 throw new NotSupportedException (this.ToString() + " cannot convert from '" +
158 value.GetType().ToString() + "'");
161 protected Exception GetConvertToException (object value, Type destinationType)
163 throw new NotSupportedException (this.ToString() + " cannot convert from '" +
164 value.GetType().ToString() + "' to '" + destinationType.ToString() + "'");
167 public object CreateInstance (IDictionary propertyValues)
169 return CreateInstance (null, propertyValues);
172 public virtual object CreateInstance (ITypeDescriptorContext context, IDictionary propertyValues)
177 public bool GetCreateInstanceSupported ()
179 return GetCreateInstanceSupported (null);
182 public virtual bool GetCreateInstanceSupported (ITypeDescriptorContext context)
187 public PropertyDescriptorCollection GetProperties (object value)
189 return GetProperties (null, value);
192 public PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context, object value)
194 return GetProperties (context, value, new Attribute[1] { BrowsableAttribute.Yes });
197 public virtual PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context,
198 object value, Attribute[] attributes)
203 public bool GetPropertiesSupported ()
205 return GetPropertiesSupported (null);
208 public virtual bool GetPropertiesSupported (ITypeDescriptorContext context)
213 public ICollection GetStandardValues ()
215 return GetStandardValues (null);
218 public virtual StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
223 public bool GetStandardValuesExclusive ()
225 return GetStandardValuesExclusive (null);
228 public virtual bool GetStandardValuesExclusive (ITypeDescriptorContext context)
233 public bool GetStandardValuesSupported ()
235 return GetStandardValuesSupported (null);
238 public virtual bool GetStandardValuesSupported (ITypeDescriptorContext context)
243 public bool IsValid (object value)
245 return IsValid (null, value);
248 public virtual bool IsValid (ITypeDescriptorContext context, object value)
253 protected PropertyDescriptorCollection SortProperties (PropertyDescriptorCollection props, string[] names)
259 public class StandardValuesCollection : ICollection, IEnumerable
261 private ICollection values;
263 public StandardValuesCollection (ICollection values)
265 this.values = values;
268 public void CopyTo (Array array, int index)
270 values.CopyTo (array, index);
273 public IEnumerator GetEnumerator ()
275 return values.GetEnumerator ();
278 bool ICollection.IsSynchronized {
279 get { return false; }
282 object ICollection.SyncRoot {
286 int ICollection.Count {
287 get { return this.Count; }
291 get { return values.Count; }
294 public object this [int index] {
295 get { return ((IList) values) [index]; }
299 protected abstract class SimplePropertyDescriptor : PropertyDescriptor
301 private Type componentType;
302 private Type propertyType;
304 public SimplePropertyDescriptor (Type componentType,
307 this (componentType, name, propertyType, null)
311 public SimplePropertyDescriptor (Type componentType,
314 Attribute [] attributes) : base (name, attributes)
316 this.componentType = componentType;
317 this.propertyType = propertyType;
320 public override Type ComponentType {
321 get { return componentType; }
324 public override Type PropertyType {
325 get { return propertyType; }
328 public override bool IsReadOnly {
329 get { return Attributes.Contains (ReadOnlyAttribute.Yes); }
332 public override bool ShouldSerializeValue (object component)
337 public override bool CanResetValue (object component)
339 DefaultValueAttribute Attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
340 if (Attrib == null) {
343 return (Attrib.Value == GetValue (component));
346 public override void ResetValue (object component)
348 DefaultValueAttribute Attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
349 if (Attrib != null) {
350 SetValue (component, Attrib.Value);