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)) {
121 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 protected PropertyDescriptorCollection
SortProperties (PropertyDescriptorCollection props
, string[] names
)
271 public class StandardValuesCollection
: ICollection
, IEnumerable
273 private ICollection values
;
275 public StandardValuesCollection (ICollection values
)
277 this.values
= values
;
280 void ICollection
.CopyTo (Array array
, int index
) {
281 CopyTo (array
, index
);
284 public void CopyTo (Array array
, int index
)
286 values
.CopyTo (array
, index
);
289 IEnumerator IEnumerable
.GetEnumerator () {
290 return GetEnumerator ();
293 public IEnumerator
GetEnumerator ()
295 return values
.GetEnumerator ();
298 bool ICollection
.IsSynchronized
{
299 get { return false; }
302 object ICollection
.SyncRoot
{
306 int ICollection
.Count
{
307 get { return this.Count; }
311 get { return values.Count; }
314 public object this [int index
] {
315 get { return ((IList) values) [index]; }
319 protected abstract class SimplePropertyDescriptor
: PropertyDescriptor
321 private Type componentType
;
322 private Type propertyType
;
324 public SimplePropertyDescriptor (Type componentType
,
327 this (componentType
, name
, propertyType
, null)
331 public SimplePropertyDescriptor (Type componentType
,
334 Attribute
[] attributes
) : base (name
, attributes
)
336 this.componentType
= componentType
;
337 this.propertyType
= propertyType
;
340 public override Type ComponentType
{
341 get { return componentType; }
344 public override Type PropertyType
{
345 get { return propertyType; }
348 public override bool IsReadOnly
{
349 get { return Attributes.Contains (ReadOnlyAttribute.Yes); }
352 public override bool ShouldSerializeValue (object component
)
357 public override bool CanResetValue (object component
)
359 DefaultValueAttribute Attrib
= ((DefaultValueAttribute
) Attributes
[typeof (DefaultValueAttribute
)]);
360 if (Attrib
== null) {
363 return (Attrib
.Value
== GetValue (component
));
366 public override void ResetValue (object component
)
368 DefaultValueAttribute Attrib
= ((DefaultValueAttribute
) Attributes
[typeof (DefaultValueAttribute
)]);
369 if (Attrib
!= null) {
370 SetValue (component
, Attrib
.Value
);