// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. // changed (simplified) by Alexander Tsyganenko using System.Collections; using System.ComponentModel; using System.ComponentModel.Design.Serialization; using System.Globalization; using System.Reflection; namespace System.Windows.Forms { public class CursorConverter : TypeConverter { private StandardValuesCollection _values; public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { if (sourceType == typeof(string) || sourceType == typeof(byte[])) { return true; } return base.CanConvertFrom(context, sourceType); } public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { if (destinationType == typeof(InstanceDescriptor)) { return true; } return base.CanConvertTo(context, destinationType); } public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string s) { string text = s.Trim(); PropertyInfo[] props = GetProperties(); foreach (var prop in props) { if (string.Equals(prop.Name, text, StringComparison.OrdinalIgnoreCase)) { return prop.GetValue(null, null); } } } return base.ConvertFrom(context, culture, value); } public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (value is Cursor cursor) { if (destinationType == typeof(string)) { PropertyInfo[] props = GetProperties(); int bestMatch = -1; for (int i = 0; i < props.Length; i++) { PropertyInfo prop = props[i]; object[] tempIndex = null; Cursor c = (Cursor)prop.GetValue(null, tempIndex); if (c == cursor) { if (ReferenceEquals(c, value)) { return prop.Name; } else { bestMatch = i; } } } if (bestMatch != -1) { return props[bestMatch].Name; } throw new FormatException("CannotConvertToString"); } else if (destinationType == typeof(InstanceDescriptor)) { PropertyInfo[] props = GetProperties(); foreach (PropertyInfo prop in props) { if (prop.GetValue(null, null) == value) { return new InstanceDescriptor(prop, null); } } } } return base.ConvertTo(context, culture, value, destinationType); } private static PropertyInfo[] GetProperties() { return typeof(Cursors).GetProperties(BindingFlags.Static | BindingFlags.Public); } public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) { if (_values is null) { ArrayList list = new ArrayList(); PropertyInfo[] props = GetProperties(); for (int i = 0; i < props.Length; i++) { PropertyInfo prop = props[i]; object[] tempIndex = null; list.Add(prop.GetValue(null, tempIndex)); } _values = new StandardValuesCollection(list.ToArray()); } return _values; } public override bool GetStandardValuesSupported(ITypeDescriptorContext context) { return true; } } }