c3fb2cbdf26ada1b98cc98897f411cb049262a1e
[mono.git] / mcs / class / referencesource / System.Data / System / Data / ColumnTypeConverter.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="ColumnTypeConverter.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>                                                                
5 // <owner current="true" primary="true">Microsoft</owner>
6 // <owner current="true" primary="false">Microsoft</owner>
7 // <owner current="false" primary="false">Microsoft</owner>
8 //------------------------------------------------------------------------------
9
10 /*
11  */
12 namespace System.Data {
13     using System.ComponentModel;
14     using System.ComponentModel.Design.Serialization;
15     using System.Diagnostics;
16     using System.Globalization;
17     using System.Data.SqlTypes;
18
19     /// <devdoc>
20     ///    <para>Provides a type
21     ///       converter that can be used to populate a list box with available types.</para>
22     /// </devdoc>
23     internal sealed class ColumnTypeConverter : TypeConverter {
24         private static Type[] types = new Type[] {
25             typeof(Boolean),
26             typeof(Byte),
27             typeof(Byte[]),
28             typeof(Char),
29             typeof(DateTime),
30             typeof(Decimal),
31             typeof(Double),
32             typeof(Guid),
33             typeof(Int16),
34             typeof(Int32),
35             typeof(Int64),
36             typeof(object),
37             typeof(SByte),
38             typeof(Single),
39             typeof(string),
40             typeof(TimeSpan),
41             typeof(UInt16),
42             typeof(UInt32),
43             typeof(UInt64),
44             typeof(SqlInt16),
45             typeof(SqlInt32),
46             typeof(SqlInt64),
47             typeof(SqlDecimal),
48             typeof(SqlSingle),
49             typeof(SqlDouble),
50             typeof(SqlString),
51             typeof(SqlBoolean),
52             typeof(SqlBinary),
53             typeof(SqlByte),
54             typeof(SqlDateTime),
55             typeof(SqlGuid),
56             typeof(SqlMoney),
57             typeof(SqlBytes),
58             typeof(SqlChars),
59             typeof(SqlXml)
60         };
61         private StandardValuesCollection values;
62         
63         // converter classes should have public ctor
64         public ColumnTypeConverter() {
65         }
66         
67         /// <devdoc>
68         ///    <para>Gets a value indicating whether this converter can
69         ///       convert an object to the given destination type using the context.</para>
70         /// </devdoc>
71         public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
72             if (destinationType == typeof(InstanceDescriptor)) {
73                 return true;
74             }
75             return base.CanConvertTo(context, destinationType);
76         }
77
78         /// <devdoc>
79         ///    <para>Converts the given value object to the specified destination type.</para>
80         /// </devdoc>
81         public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
82             if (destinationType == null) {
83                 throw new ArgumentNullException("destinationType");
84             }
85
86             if (destinationType == typeof(string)) {
87                 if (value == null) {
88                     return String.Empty;
89                 }
90                 else {
91                     value.ToString();
92                 }
93             }
94             if (value != null && destinationType == typeof(InstanceDescriptor)) {
95                 Object newValue = value;
96                 if (value is string) {
97                     for (int i = 0; i < types.Length; i++) {
98                         if (types[i].ToString().Equals(value))
99                             newValue = types[i];
100                     }
101                 }
102                 
103                 if (value is Type || value is string) {
104                     System.Reflection.MethodInfo method = typeof(Type).GetMethod("GetType", new Type[] {typeof(string)}); // change done for security review 
105                     if (method != null) {
106                         return new InstanceDescriptor(method, new object[] {((Type)newValue).AssemblyQualifiedName});
107                     }
108                 }
109             }
110             
111             return base.ConvertTo(context, culture, value, destinationType);
112         }
113
114         public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
115             if (sourceType == typeof(string)) {
116                 return true;
117             }
118             return base.CanConvertTo(context, sourceType);
119         }
120
121         public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) {
122             if (value != null && value.GetType() == typeof(string)) {
123                 for (int i = 0; i < types.Length; i++) {
124                     if (types[i].ToString().Equals(value))
125                         return types[i];
126                 }
127
128                 return typeof(string);
129             }
130             
131             return base.ConvertFrom(context, culture, value);
132         }
133
134         /// <devdoc>
135         ///    <para>Gets a collection of standard values for the data type this validator is
136         ///       designed for.</para>
137         /// </devdoc>
138         public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) {
139             if (values == null) {
140                 object[] objTypes;
141                 
142                 if (types != null) {
143                     objTypes = new object[types.Length];
144                     Array.Copy(types, objTypes, types.Length);
145                 }
146                 else {
147                     objTypes = null;
148                 }
149                 
150                 values = new StandardValuesCollection(objTypes);
151             }
152             return values;
153         }
154     
155         /// <devdoc>
156         ///    <para>Gets a value indicating whether the list of standard values returned from
157         ///    <see cref='System.ComponentModel.TypeListConverter.GetStandardValues'/> is an exclusive list. </para>
158         /// </devdoc>
159         public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) {
160             return true;
161         }
162         
163         /// <devdoc>
164         ///    <para>Gets a value indicating whether this object supports a
165         ///       standard set of values that can be picked from a list using the specified
166         ///       context.</para>
167         /// </devdoc>
168         public override bool GetStandardValuesSupported(ITypeDescriptorContext context) {
169             return true;
170         }
171     }
172 }
173