CursorConverter.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. // changed (simplified) by Alexander Tsyganenko
  5. using System.Collections;
  6. using System.ComponentModel;
  7. using System.ComponentModel.Design.Serialization;
  8. using System.Globalization;
  9. using System.Reflection;
  10. namespace System.Windows.Forms
  11. {
  12. public class CursorConverter : TypeConverter
  13. {
  14. private StandardValuesCollection _values;
  15. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  16. {
  17. if (sourceType == typeof(string) || sourceType == typeof(byte[]))
  18. {
  19. return true;
  20. }
  21. return base.CanConvertFrom(context, sourceType);
  22. }
  23. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  24. {
  25. if (destinationType == typeof(InstanceDescriptor))
  26. {
  27. return true;
  28. }
  29. return base.CanConvertTo(context, destinationType);
  30. }
  31. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  32. {
  33. if (value is string s)
  34. {
  35. string text = s.Trim();
  36. PropertyInfo[] props = GetProperties();
  37. foreach (var prop in props)
  38. {
  39. if (string.Equals(prop.Name, text, StringComparison.OrdinalIgnoreCase))
  40. {
  41. return prop.GetValue(null, null);
  42. }
  43. }
  44. }
  45. return base.ConvertFrom(context, culture, value);
  46. }
  47. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  48. {
  49. if (value is Cursor cursor)
  50. {
  51. if (destinationType == typeof(string))
  52. {
  53. PropertyInfo[] props = GetProperties();
  54. int bestMatch = -1;
  55. for (int i = 0; i < props.Length; i++)
  56. {
  57. PropertyInfo prop = props[i];
  58. object[] tempIndex = null;
  59. Cursor c = (Cursor)prop.GetValue(null, tempIndex);
  60. if (c == cursor)
  61. {
  62. if (ReferenceEquals(c, value))
  63. {
  64. return prop.Name;
  65. }
  66. else
  67. {
  68. bestMatch = i;
  69. }
  70. }
  71. }
  72. if (bestMatch != -1)
  73. {
  74. return props[bestMatch].Name;
  75. }
  76. throw new FormatException("CannotConvertToString");
  77. }
  78. else if (destinationType == typeof(InstanceDescriptor))
  79. {
  80. PropertyInfo[] props = GetProperties();
  81. foreach (PropertyInfo prop in props)
  82. {
  83. if (prop.GetValue(null, null) == value)
  84. {
  85. return new InstanceDescriptor(prop, null);
  86. }
  87. }
  88. }
  89. }
  90. return base.ConvertTo(context, culture, value, destinationType);
  91. }
  92. private static PropertyInfo[] GetProperties()
  93. {
  94. return typeof(Cursors).GetProperties(BindingFlags.Static | BindingFlags.Public);
  95. }
  96. public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
  97. {
  98. if (_values is null)
  99. {
  100. ArrayList list = new ArrayList();
  101. PropertyInfo[] props = GetProperties();
  102. for (int i = 0; i < props.Length; i++)
  103. {
  104. PropertyInfo prop = props[i];
  105. object[] tempIndex = null;
  106. list.Add(prop.GetValue(null, tempIndex));
  107. }
  108. _values = new StandardValuesCollection(list.ToArray());
  109. }
  110. return _values;
  111. }
  112. public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
  113. {
  114. return true;
  115. }
  116. }
  117. }