comparison Source/ComponentModel/TypeTypeConverter.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.ComponentModel;
3 using System.Globalization;
4 using System.ComponentModel.Design;
5
6 namespace BLToolkit.ComponentModel
7 {
8 /// <summary>
9 /// Converts the value of an object into a <see cref="System.Type"/>.
10 /// </summary>
11 public class TypeTypeConverter: TypeConverter
12 {
13 // Human readable text for 'nothing selected'.
14 //
15 private const string NoType = "(none)";
16
17 /// <summary>
18 /// Returns whether this converter can convert an object of the given type to
19 /// a <see cref="System.Type"/>, using the specified context.
20 /// </summary>
21 /// <param name="context">An <see cref="System.ComponentModel.ITypeDescriptorContext"/>
22 /// that provides a format context. </param>
23 /// <param name="sourceType">A <see cref="System.Type"/> that represents the type
24 /// you want to convert from. </param>
25 /// <returns>
26 /// <see langword="true"/> if this converter can perform the conversion;
27 /// otherwise, <see langword="false"/>.
28 /// </returns>
29 public override bool CanConvertFrom(
30 ITypeDescriptorContext context,
31 Type sourceType)
32 {
33 return sourceType == typeof(string) ||
34 base.CanConvertFrom(context, sourceType);
35 }
36
37 /// <summary>
38 /// Converts the given object to the corresponding <see cref="System.Type"/>,
39 /// using the specified context and culture information.
40 /// </summary>
41 /// <param name="culture">The <see cref="System.Globalization.CultureInfo"/> to
42 /// use as the current culture. </param>
43 /// <param name="context">An
44 /// <see cref="System.ComponentModel.ITypeDescriptorContext"/> that provides a
45 /// format context. </param>
46 /// <param name="value">The <see cref="System.Object"/> to convert. </param>
47 /// <returns>
48 /// An <see cref="System.Object"/> that represents the converted value.
49 /// </returns>
50 /// <exception cref="System.NotSupportedException">The conversion cannot be
51 /// performed. </exception>
52 public override object ConvertFrom(
53 ITypeDescriptorContext context,
54 CultureInfo culture,
55 object value)
56 {
57 if (value == null)
58 return null;
59
60 if (!(value is string))
61 return base.ConvertFrom(context, culture, value);
62
63 string str = (string)value;
64
65 if (str.Length == 0 || str == NoType)
66 return null;
67
68 // Try VisualStudio own service first.
69 //
70 ITypeResolutionService typeResolver =
71 (ITypeResolutionService)context.GetService(typeof(ITypeResolutionService));
72
73 if (typeResolver != null)
74 {
75 Type type = typeResolver.GetType(str);
76
77 if (type != null)
78 return type;
79 }
80
81 return Type.GetType(str);
82 }
83
84 /// <summary>
85 /// Returns whether this converter can convert the object to the specified type,
86 /// using the specified context.
87 /// </summary>
88 /// <param name="context">An
89 /// <see cref="System.ComponentModel.ITypeDescriptorContext"/> that provides
90 /// a format context. </param>
91 /// <param name="destinationType">A <see cref="System.Type"/> that represents
92 /// the type you want to convert to. </param>
93 /// <returns>
94 /// <see langword="true"/> if this converter can perform the conversion;
95 /// otherwise, <see langword="false"/>.
96 /// </returns>
97 public override bool CanConvertTo(
98 ITypeDescriptorContext context,
99 Type destinationType)
100 {
101 return destinationType == typeof(string) ||
102 base.CanConvertTo(context, destinationType);
103 }
104
105 /// <summary>
106 /// Converts the given value object to the specified type, using the specified
107 /// context and culture information.
108 /// </summary>
109 /// <param name="culture">A <see cref="System.Globalization.CultureInfo"/>.
110 /// If null is passed, the current culture is assumed. </param>
111 /// <param name="context">An
112 /// <see cref="System.ComponentModel.ITypeDescriptorContext"/> that provides
113 /// a format context. </param>
114 /// <param name="destinationType">The <see cref="System.Type"/> to convert
115 /// the value parameter to. </param>
116 /// <param name="value">The <see cref="System.Object"/> to convert. </param>
117 /// <returns>
118 /// An <see cref="System.Object"/> that represents the converted value.
119 /// </returns>
120 /// <exception cref="System.NotSupportedException">The conversion cannot be
121 /// performed. </exception>
122 /// <exception cref="System.ArgumentNullException">
123 /// The <paramref name="destinationType"/> parameter is null. </exception>
124 public override object ConvertTo(
125 ITypeDescriptorContext context,
126 CultureInfo culture,
127 object value,
128 Type destinationType)
129 {
130 if (destinationType != typeof(string))
131 return base.ConvertTo(context, culture, value, destinationType);
132
133 if (value == null || value.ToString().Length == 0)
134 return NoType;
135
136 return value.ToString();
137 }
138 }
139 }