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 return GetConvertFromException (value);
82 public object ConvertFromInvariantString (string text)
84 return ConvertFromInvariantString (null, text);
87 public object ConvertFromInvariantString (ITypeDescriptorContext context, string text)
89 return ConvertFromString (context, CultureInfo.InvariantCulture, text);
92 public object ConvertFromString (string text)
94 return ConvertFrom (text);
97 public object ConvertFromString (ITypeDescriptorContext context, string text)
99 return ConvertFromString (context, CultureInfo.CurrentCulture, text);
102 public object ConvertFromString (ITypeDescriptorContext context, CultureInfo culture, string text)
104 return ConvertFrom (context, culture, text);
107 public object ConvertTo (object value, Type destinationType)
109 return ConvertTo (null, null, value, destinationType);
112 public virtual object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value,
113 Type destinationType)
116 if (destinationType == null)
117 throw new ArgumentNullException ("destinationType");
119 if (destinationType == typeof (string)) {
124 return Convert.ToString (value, culture);
126 return value.ToString();
129 return GetConvertToException (value, destinationType);
132 public string ConvertToInvariantString (object value)
134 return ConvertToInvariantString (null, value);
137 public string ConvertToInvariantString (ITypeDescriptorContext context, object value)
139 return (string) ConvertTo (context, CultureInfo.InvariantCulture, value, typeof (string));
142 public string ConvertToString (object value)
144 return (string) ConvertTo (null, CultureInfo.CurrentCulture, value, typeof (string));
147 public string ConvertToString (ITypeDescriptorContext context, object value)
149 return (string) ConvertTo (context, CultureInfo.CurrentCulture, value, typeof (string));
152 public string ConvertToString (ITypeDescriptorContext context, CultureInfo culture, object value)
154 return (string) ConvertTo (context, culture, value, typeof (string));
157 protected Exception GetConvertFromException (object value)
159 string destinationType;
161 destinationType = "(null)";
163 destinationType = value.GetType ().FullName;
165 throw new NotSupportedException (string.Format (CultureInfo.InvariantCulture,
166 "{0} cannot convert from {1}.", this.GetType ().Name,
170 protected Exception GetConvertToException (object value, Type destinationType)
174 sourceType = "(null)";
176 sourceType = value.GetType ().FullName;
178 throw new NotSupportedException (string.Format (CultureInfo.InvariantCulture,
179 "'{0}' is unable to convert '{1}' to '{2}'.", this.GetType ().Name,
180 sourceType, destinationType.FullName));
183 public object CreateInstance (IDictionary propertyValues)
185 return CreateInstance (null, propertyValues);
188 public virtual object CreateInstance (ITypeDescriptorContext context, IDictionary propertyValues)
193 public bool GetCreateInstanceSupported ()
195 return GetCreateInstanceSupported (null);
198 public virtual bool GetCreateInstanceSupported (ITypeDescriptorContext context)
203 public PropertyDescriptorCollection GetProperties (object value)
205 return GetProperties (null, value);
208 public PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context, object value)
210 return GetProperties (context, value, new Attribute[1] { BrowsableAttribute.Yes });
213 public virtual PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context,
214 object value, Attribute[] attributes)
219 public bool GetPropertiesSupported ()
221 return GetPropertiesSupported (null);
224 public virtual bool GetPropertiesSupported (ITypeDescriptorContext context)
229 public ICollection GetStandardValues ()
231 return GetStandardValues (null);
234 public virtual StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
239 public bool GetStandardValuesExclusive ()
241 return GetStandardValuesExclusive (null);
244 public virtual bool GetStandardValuesExclusive (ITypeDescriptorContext context)
249 public bool GetStandardValuesSupported ()
251 return GetStandardValuesSupported (null);
254 public virtual bool GetStandardValuesSupported (ITypeDescriptorContext context)
259 public bool IsValid (object value)
261 return IsValid (null, value);
264 public virtual bool IsValid (ITypeDescriptorContext context, object value)
269 protected PropertyDescriptorCollection SortProperties (PropertyDescriptorCollection props, string[] names)
275 public class StandardValuesCollection : ICollection, IEnumerable
277 private ICollection values;
279 public StandardValuesCollection (ICollection values)
281 this.values = values;
284 void ICollection.CopyTo (Array array, int index) {
285 CopyTo (array, index);
288 public void CopyTo (Array array, int index)
290 values.CopyTo (array, index);
293 IEnumerator IEnumerable.GetEnumerator () {
294 return GetEnumerator ();
297 public IEnumerator GetEnumerator ()
299 return values.GetEnumerator ();
302 bool ICollection.IsSynchronized {
303 get { return false; }
306 object ICollection.SyncRoot {
310 int ICollection.Count {
311 get { return this.Count; }
315 get { return values.Count; }
318 public object this [int index] {
319 get { return ((IList) values) [index]; }
323 protected abstract class SimplePropertyDescriptor : PropertyDescriptor
325 private Type componentType;
326 private Type propertyType;
333 SimplePropertyDescriptor (Type componentType,
336 this (componentType, name, propertyType, null)
345 SimplePropertyDescriptor (Type componentType,
348 Attribute [] attributes) : base (name, attributes)
350 this.componentType = componentType;
351 this.propertyType = propertyType;
354 public override Type ComponentType {
355 get { return componentType; }
358 public override Type PropertyType {
359 get { return propertyType; }
362 public override bool IsReadOnly {
363 get { return Attributes.Contains (ReadOnlyAttribute.Yes); }
366 public override bool ShouldSerializeValue (object component)
371 public override bool CanResetValue (object component)
373 DefaultValueAttribute Attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
374 if (Attrib == null) {
377 return (Attrib.Value == GetValue (component));
380 public override void ResetValue (object component)
382 DefaultValueAttribute Attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
383 if (Attrib != null) {
384 SetValue (component, Attrib.Value);