DNumberConverter.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. 
  2. using System;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.ComponentModel.Design.Serialization;
  6. using System.Globalization;
  7. using System.Reflection;
  8. namespace Dongke.WinForm.Controls
  9. {
  10. /// <summary>
  11. /// Converts a <see cref="T:DNumber" /> object from one data type to another. Access this class through the <see cref="T:System.ComponentModel.TypeDescriptor" /> object.
  12. /// </summary>
  13. /// <filterpriority>1</filterpriority>
  14. public class DNumberConverter : TypeConverter
  15. {
  16. /// <summary>
  17. /// Determines if this converter can convert an object in the given source type to the native type of the converter.
  18. /// </summary>
  19. /// <returns>true if this object can perform the conversion; otherwise, false.
  20. /// </returns>
  21. /// <param name="context">
  22. /// A formatter context. This object can be used to get additional information about the environment this converter is being called from. This may be null, so you should always check. Also, properties on the context object may also return null.
  23. /// </param>
  24. /// <param name="sourceType">
  25. /// The type you want to convert from.
  26. /// </param>
  27. /// <filterpriority>1</filterpriority>
  28. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  29. {
  30. return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
  31. }
  32. /// <summary>
  33. /// Gets a value indicating whether this converter can convert an object to the given destination type using the context.
  34. /// </summary>
  35. /// <returns>true if this converter can perform the conversion; otherwise, false.
  36. /// </returns>
  37. /// <param name="context">
  38. /// An <see cref="T:System.ComponentModel.ITypeDescriptorContext" /> object that provides a format context.
  39. /// </param>
  40. /// <param name="destinationType">
  41. /// A <see cref="T:System.Type" /> object that represents the type you want to convert to.
  42. /// </param>
  43. /// <filterpriority>1</filterpriority>
  44. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  45. {
  46. return destinationType == typeof(InstanceDescriptor) || base.CanConvertTo(context, destinationType);
  47. }
  48. /// <summary>
  49. /// Converts the specified object to a <see cref="T:DNumber" /> object.
  50. /// </summary>
  51. /// <returns>
  52. /// The converted object.
  53. /// </returns>
  54. /// <param name="context">
  55. /// A formatter context. This object can be used to get additional information about the environment this converter is being called from. This may be null, so you should always check. Also, properties on the context object may also return null.
  56. /// </param>
  57. /// <param name="culture">
  58. /// An object that contains culture specific information, such as the language, calendar, and cultural conventions associated with a specific culture. It is based on the RFC 1766 standard.
  59. /// </param>
  60. /// <param name="value">
  61. /// The object to convert.
  62. /// </param>
  63. /// <exception cref="T:System.NotSupportedException">
  64. /// The conversion cannot be completed.
  65. /// </exception>
  66. /// <filterpriority>1</filterpriority>
  67. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  68. {
  69. string text = value as string;
  70. if (text == null)
  71. {
  72. return base.ConvertFrom(context, culture, value);
  73. }
  74. string text2 = text.Trim();
  75. if (text2.Length == 0)
  76. {
  77. return null;
  78. }
  79. if (culture == null)
  80. {
  81. culture = CultureInfo.CurrentCulture;
  82. }
  83. char c = culture.TextInfo.ListSeparator[0];
  84. string[] array = text2.Split(new char[]
  85. {
  86. c
  87. }
  88. );
  89. int[] array2 = new int[array.Length];
  90. TypeConverter converter = TypeDescriptor.GetConverter(typeof(int));
  91. for (int i = 0; i < array2.Length; i++)
  92. {
  93. array2[i] = (int)converter.ConvertFromString(context, culture, array[i]);
  94. }
  95. if (array2.Length == 2)
  96. {
  97. return new DNumber(array2[0], array2[1]);
  98. }
  99. //throw new ArgumentException(SR.GetString("TextParseFailedFormat", new object[]
  100. // {
  101. // text2,
  102. // "x, y"
  103. // })
  104. //);
  105. throw new ArgumentException("TextParseFailedFormat");
  106. }
  107. /// <summary>
  108. /// Converts the specified object to the specified type.
  109. /// </summary>
  110. /// <returns>
  111. /// The converted object.
  112. /// </returns>
  113. /// <param name="context">
  114. /// A formatter context. This object can be used to get additional information about the environment this converter is being called from. This may be null, so you should always check. Also, properties on the context object may also return null.
  115. /// </param>
  116. /// <param name="culture">
  117. /// An object that contains culture specific information, such as the language, calendar, and cultural conventions associated with a specific culture. It is based on the RFC 1766 standard.
  118. /// </param>
  119. /// <param name="value">
  120. /// The object to convert.
  121. /// </param>
  122. /// <param name="destinationType">
  123. /// The type to convert the object to.
  124. /// </param>
  125. /// <exception cref="T:System.NotSupportedException">
  126. /// The conversion cannot be completed.
  127. /// </exception>
  128. /// <filterpriority>1</filterpriority>
  129. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  130. {
  131. if (destinationType == null)
  132. {
  133. throw new ArgumentNullException("destinationType");
  134. }
  135. if (value is DNumber)
  136. {
  137. if (destinationType == typeof(string))
  138. {
  139. DNumber dnumber = (DNumber)value;
  140. if (culture == null)
  141. {
  142. culture = CultureInfo.CurrentCulture;
  143. }
  144. string separator = culture.TextInfo.ListSeparator + " ";
  145. TypeConverter converter = TypeDescriptor.GetConverter(typeof(int));
  146. string[] array = new string[2];
  147. int num = 0;
  148. array[num++] = converter.ConvertToString(context, culture, dnumber.X);
  149. array[num++] = converter.ConvertToString(context, culture, dnumber.Y);
  150. return string.Join(separator, array);
  151. }
  152. if (destinationType == typeof(InstanceDescriptor))
  153. {
  154. DNumber dnumber2 = (DNumber)value;
  155. ConstructorInfo constructor = typeof(DNumber).GetConstructor(new Type[]
  156. {
  157. typeof(int),
  158. typeof(int)
  159. }
  160. );
  161. if (constructor != null)
  162. {
  163. return new InstanceDescriptor(constructor, new object[]
  164. {
  165. dnumber2.X,
  166. dnumber2.Y
  167. }
  168. );
  169. }
  170. }
  171. }
  172. return base.ConvertTo(context, culture, value, destinationType);
  173. }
  174. /// <summary>
  175. /// Creates an instance of this type given a set of property values for the object.
  176. /// </summary>
  177. /// <returns>
  178. /// The newly created object, or null if the object could not be created. The default implementation returns null.
  179. /// </returns>
  180. /// <param name="context">
  181. /// A type descriptor through which additional context can be provided.
  182. /// </param>
  183. /// <param name="propertyValues">
  184. /// A dictionary of new property values. The dictionary contains a series of name-value pairs, one for each property returned from <see cref="M:DNumberConverter.GetProperties(System.ComponentModel.ITypeDescriptorContext,System.Object,System.Attribute[])" />.
  185. /// </param>
  186. /// <filterpriority>1</filterpriority>
  187. public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
  188. {
  189. if (propertyValues == null)
  190. {
  191. throw new ArgumentNullException("propertyValues");
  192. }
  193. object obj = propertyValues["X"];
  194. object obj2 = propertyValues["Y"];
  195. if (obj == null || obj2 == null || !(obj is int) || !(obj2 is int))
  196. {
  197. throw new ArgumentException("PropertyValueInvalidEntry");
  198. }
  199. return new DNumber((int)obj, (int)obj2);
  200. }
  201. /// <summary>
  202. /// Determines if changing a value on this object should require a call to <see cref="M:DNumberConverter.CreateInstance(System.ComponentModel.ITypeDescriptorContext,System.Collections.IDictionary)" /> to create a new value.
  203. /// </summary>
  204. /// <returns>
  205. /// true if the <see cref="M:DNumberConverter.CreateInstance(System.ComponentModel.ITypeDescriptorContext,System.Collections.IDictionary)" /> method should be called when a change is made to one or more properties of this object; otherwise, false.
  206. /// </returns>
  207. /// <param name="context">
  208. /// A <see cref="T:System.ComponentModel.TypeDescriptor" /> through which additional context can be provided.
  209. /// </param>
  210. /// <filterpriority>1</filterpriority>
  211. public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
  212. {
  213. return true;
  214. }
  215. /// <summary>
  216. /// Retrieves the set of properties for this type. By default, a type does not return any properties.
  217. /// </summary>
  218. /// <returns>
  219. /// The set of properties that are exposed for this data type. If no properties are exposed, this method might return null. The default implementation always returns null.
  220. /// </returns>
  221. /// <param name="context">
  222. /// A type descriptor through which additional context can be provided.
  223. /// </param>
  224. /// <param name="value">
  225. /// The value of the object to get the properties for.
  226. /// </param>
  227. /// <param name="attributes">
  228. /// An array of <see cref="T:System.Attribute" /> objects that describe the properties.
  229. /// </param>
  230. /// <filterpriority>1</filterpriority>
  231. public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
  232. {
  233. PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(DNumber), attributes);
  234. return properties.Sort(new string[]
  235. {
  236. "X",
  237. "Y"
  238. }
  239. );
  240. }
  241. /// <summary>
  242. /// Determines if this object supports properties. By default, this is false.
  243. /// </summary>
  244. /// <returns>
  245. /// true if
  246. /// <see cref="M:DNumberConverter.GetProperties(System.ComponentModel.ITypeDescriptorContext,System.Object,System.Attribute[])" />
  247. /// should be called to find the properties of this object; otherwise, false.
  248. /// </returns>
  249. /// <param name="context">
  250. /// A <see cref="T:System.ComponentModel.TypeDescriptor" /> through which additional context can be provided.
  251. /// </param>
  252. /// <filterpriority>1</filterpriority>
  253. public override bool GetPropertiesSupported(ITypeDescriptorContext context)
  254. {
  255. return true;
  256. }
  257. }
  258. }