123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- // 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;
- }
- }
- }
|