1 //------------------------------------------------------------------------------
2 // <copyright file="ColumnTypeConverter.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
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 //------------------------------------------------------------------------------
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;
20 /// <para>Provides a type
21 /// converter that can be used to populate a list box with available types.</para>
23 internal sealed class ColumnTypeConverter : TypeConverter {
24 private static Type[] types = new Type[] {
61 private StandardValuesCollection values;
63 // converter classes should have public ctor
64 public ColumnTypeConverter() {
68 /// <para>Gets a value indicating whether this converter can
69 /// convert an object to the given destination type using the context.</para>
71 public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
72 if (destinationType == typeof(InstanceDescriptor)) {
75 return base.CanConvertTo(context, destinationType);
79 /// <para>Converts the given value object to the specified destination type.</para>
81 public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
82 if (destinationType == null) {
83 throw new ArgumentNullException("destinationType");
86 if (destinationType == typeof(string)) {
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))
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});
111 return base.ConvertTo(context, culture, value, destinationType);
114 public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
115 if (sourceType == typeof(string)) {
118 return base.CanConvertTo(context, sourceType);
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))
128 return typeof(string);
131 return base.ConvertFrom(context, culture, value);
135 /// <para>Gets a collection of standard values for the data type this validator is
136 /// designed for.</para>
138 public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) {
139 if (values == null) {
143 objTypes = new object[types.Length];
144 Array.Copy(types, objTypes, types.Length);
150 values = new StandardValuesCollection(objTypes);
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>
159 public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) {
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
168 public override bool GetStandardValuesSupported(ITypeDescriptorContext context) {