* EnumConverter.cs: new file.
* TypeConverter.cs: implemented a few simple methods.
* TypeDescriptor.cs:
(GetConverter): initial support for converters of well-known types.
svn path=/trunk/mcs/; revision=6254
+2002-07-29 Gonzalo Paniagua Javier <gonzalo@ximian.com>
+
+ * EnumConverter.cs: new file.
+
+ * TypeConverter.cs: implemented a few simple methods.
+
+ * TypeDescriptor.cs:
+ (GetConverter): initial support for converters of well-known types.
+
2002-07-28 Gonzalo Paniagua Javier <gonzalo@ximian.com>
* DerivedPropertyDescriptor.cs: New file. Internal class.
--- /dev/null
+//
+// System.ComponentModel.EnumConverter
+//
+// Authors:
+// Gonzalo Paniagua Javier (gonzalo@ximian.com)
+//
+// (C) 2002 Ximian, Inc (http://www.ximian.com)
+//
+using System;
+using System.Globalization;
+
+namespace System.ComponentModel
+{
+ public class EnumConverter : TypeConverter
+ {
+ private Type type;
+ private StandardValuesCollection stdValues;
+
+ public EnumConverter (Type type)
+ {
+ this.type = type;
+ }
+
+ [MonoTODO]
+ public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
+ {
+ return base.CanConvertTo (context, destinationType);
+ }
+
+ [MonoTODO]
+ public override object ConvertTo (ITypeDescriptorContext context,
+ CultureInfo culture,
+ object value,
+ Type destinationType)
+ {
+ if (destinationType == typeof (string))
+ return value.ToString ();
+ return base.ConvertTo (context, culture, value, destinationType);
+ }
+
+ public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
+ {
+ if (sourceType == typeof (string))
+ return true;
+ return base.CanConvertFrom (context, sourceType);
+ }
+
+ public override object ConvertFrom (ITypeDescriptorContext context,
+ CultureInfo culture,
+ object value)
+ {
+ string val = value as string;
+ if (val == null)
+ return base.ConvertFrom(context, culture, value);
+
+ string [] subValues = val.Split (new char [] {','});
+
+ long longResult = 0;
+ foreach (string s in subValues)
+ longResult |= (long) Enum.Parse (type, s, true);
+
+ return Enum.ToObject (type, longResult);
+ }
+
+ public override bool IsValid (ITypeDescriptorContext context, object value)
+ {
+ return Enum.IsDefined (type, value);
+ }
+
+ public override bool GetStandardValuesSupported (ITypeDescriptorContext context)
+ {
+ return true;
+ }
+
+ public override bool GetStandardValuesExclusive (ITypeDescriptorContext context)
+ {
+ return !(type.IsDefined (typeof (FlagsAttribute), false));
+ }
+
+ public override StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
+ {
+ if (stdValues == null) {
+ Array values = Enum.GetValues (type);
+ Array.Sort (values);
+ stdValues = new StandardValuesCollection (values);
+ }
+ return stdValues;
+ }
+ }
+
+}
+
return CanConvertTo (null, destinationType);
}
- [MonoTODO]
public virtual bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
{
- throw new NotImplementedException ();
+ return (destinationType == typeof (string));
}
public object ConvertFrom (object o)
throw new NotImplementedException ();
}
- [MonoTODO]
public object ConvertTo (object value, Type destinationType)
{
- throw new NotImplementedException ();
+ return ConvertTo (null, null, value, destinationType);
}
- [MonoTODO]
+
public virtual object ConvertTo (ITypeDescriptorContext context,
CultureInfo culture,
object value,
Type destinationType)
{
- throw new NotImplementedException ();
+ // context? culture?
+ if (destinationType == null)
+ throw new ArgumentNullException ("destinationType");
+
+ if (destinationType == typeof (string)) {
+ if (value != null)
+ return value.ToString();
+ return String.Empty;
+ }
+
+ throw new NotSupportedException ("Conversion not supported");
}
- [MonoTODO]
public string ConvertToInvariantString (object value)
{
- throw new NotImplementedException ();
+ return ConvertToInvariantString (null, value);
}
[MonoTODO]
throw new NotImplementedException ();
}
- [MonoTODO]
public string ConvertToString (object value)
{
- throw new NotImplementedException ();
+ return (string) ConvertTo (null, CultureInfo.CurrentCulture, value, typeof (string));
}
- [MonoTODO]
public string ConvertToString (ITypeDescriptorContext context, object value)
{
- throw new NotImplementedException ();
+ return (string) ConvertTo (context, CultureInfo.CurrentCulture, value, typeof (string));
}
- [MonoTODO]
public string ConvertToString (ITypeDescriptorContext context, CultureInfo culture, object value)
{
- throw new NotImplementedException ();
+ return (string) ConvertTo (context, culture, value, typeof (string));
}
[MonoTODO]
throw new NotImplementedException ();
}
- [MonoTODO]
public PropertyDescriptorCollection GetProperties (object value)
{
- throw new NotImplementedException ();
+ return GetProperties (null, value);
}
- [MonoTODO]
public PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context, object value)
{
- throw new NotImplementedException ();
+ return GetProperties (context, value, null);
}
- [MonoTODO]
public virtual PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context,
object value,
Attribute[] attributes)
{
- throw new NotImplementedException ();
+ return null;
}
- [MonoTODO]
public bool GetPropertiesSupported ()
{
- throw new NotImplementedException ();
+ return GetPropertiesSupported (null);
}
- [MonoTODO]
public virtual bool GetPropertiesSupported (ITypeDescriptorContext context)
{
- throw new NotImplementedException ();
+ return false;
}
- [MonoTODO]
public ICollection GetStandardValues ()
{
- throw new NotImplementedException ();
+ return GetStandardValues (null);
}
- [MonoTODO]
public virtual StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
{
- throw new NotImplementedException ();
+ return null;
}
- [MonoTODO]
public bool GetStandardValuesExclusive ()
{
- throw new NotImplementedException ();
+ return GetStandardValuesExclusive (null);
}
- [MonoTODO]
public virtual bool GetStandardValuesExclusive (ITypeDescriptorContext context)
{
- throw new NotImplementedException ();
+ return false;
}
- [MonoTODO]
public bool GetStandardValuesSupported ()
{
- throw new NotImplementedException ();
+ return GetStandardValuesSupported (null);
}
- [MonoTODO]
public virtual bool GetStandardValuesSupported (ITypeDescriptorContext context)
{
- throw new NotImplementedException ();
+ return false;
}
- [MonoTODO]
public bool IsValid (object value)
{
- throw new NotImplementedException ();
+ return IsValid (null, value);
}
- [MonoTODO]
public virtual bool IsValid (ITypeDescriptorContext context, object value)
{
- throw new NotImplementedException ();
+ return true;
}
public class StandardValuesCollection : ICollection, IEnumerable
public sealed class TypeDescriptor
{
+ private static readonly string creatingDefaultConverters = "creatingDefaultConverters";
+ private static Hashtable defaultConverters;
+
[MonoTODO]
public static void AddEditorTable (Type editorBaseType, Hashtable table)
{
throw new NotImplementedException ();
}
+ private static Hashtable DefaultConverters
+ {
+ get {
+ if (defaultConverters != null)
+ return defaultConverters;
+
+ lock (creatingDefaultConverters) {
+ if (defaultConverters != null)
+ return defaultConverters;
+
+ //FIXME: add more converters as we implement them
+ defaultConverters = new Hashtable ();
+ defaultConverters.Add (typeof (Enum), typeof (EnumConverter));
+ defaultConverters.Add (typeof (string), typeof (StringConverter));
+ }
+ return defaultConverters;
+ }
+ }
+
public static TypeConverter GetConverter (Type type)
{
- object [] attrs = type.GetCustomAttributes (false);
+ Type t = DefaultConverters [type] as Type;
+ if (t == null && type.IsEnum)
+ t = (Type) DefaultConverters [typeof (Enum)];
+
string converter_name = null;
- foreach (object o in attrs){
- if (o is TypeConverterAttribute){
- TypeConverterAttribute tc = (TypeConverterAttribute) o;
- converter_name = tc.ConverterTypeName;
- break;
+ if (t == null) {
+ object [] attrs = type.GetCustomAttributes (false);
+ foreach (object o in attrs){
+ if (o is TypeConverterAttribute){
+ TypeConverterAttribute tc = (TypeConverterAttribute) o;
+ converter_name = tc.ConverterTypeName;
+ break;
+ }
}
+ } else {
+ converter_name = t.FullName;
}
+
if (converter_name == null)
return null;