Files
Dec2017-Script-Dump/System/ComponentModel/CharConverter.cs
T
2026-06-04 11:42:34 +02:00

74 lines
3.6 KiB
C#

using System;
using System.Globalization;
namespace System.ComponentModel
{
/// <summary>Provides a type converter to convert Unicode character objects to and from various other representations.</summary>
// Token: 0x02000060 RID: 96
public class CharConverter : TypeConverter
{
/// <summary>Gets a value indicating whether this converter can convert an object in the given source type to a Unicode character object using the specified context.</summary>
/// <returns>true if this converter can perform the conversion; otherwise, false.</returns>
/// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext" /> that provides a format context. </param>
/// <param name="sourceType">A <see cref="T:System.Type" /> that represents the type you want to convert from. </param>
// Token: 0x060003B3 RID: 947 RVA: 0x0000B5E8 File Offset: 0x000097E8
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
}
/// <summary>Converts the given object to a Unicode character object.</summary>
/// <returns>An <see cref="T:System.Object" /> that represents the converted <paramref name="value" />.</returns>
/// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext" /> that provides a format context. </param>
/// <param name="culture">The culture into which <paramref name="value" /> will be converted.</param>
/// <param name="value">The <see cref="T:System.Object" /> to convert. </param>
/// <exception cref="T:System.FormatException">
/// <paramref name="value" /> is not a valid value for the target type. </exception>
/// <exception cref="T:System.NotSupportedException">The conversion cannot be performed. </exception>
// Token: 0x060003B4 RID: 948 RVA: 0x0000B604 File Offset: 0x00009804
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
string text = value as string;
if (text == null)
{
return base.ConvertFrom(context, culture, value);
}
if (text.Length > 1)
{
text = text.Trim();
}
if (text.Length > 1)
{
throw new FormatException(string.Format("String {0} is not a valid Char: it has to be less than or equal to one char long.", text));
}
if (text.Length == 0)
{
return '\0';
}
return text[0];
}
/// <summary>Converts the given value object to a Unicode character object using the arguments.</summary>
/// <returns>An <see cref="T:System.Object" /> that represents the converted value.</returns>
/// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext" /> that provides a format context. </param>
/// <param name="culture">The culture into which <paramref name="value" /> will be converted.</param>
/// <param name="value">The <see cref="T:System.Object" /> to convert. </param>
/// <param name="destinationType">The <see cref="T:System.Type" /> to convert the value to. </param>
/// <exception cref="T:System.NotSupportedException">The conversion cannot be performed. </exception>
// Token: 0x060003B5 RID: 949 RVA: 0x0000B678 File Offset: 0x00009878
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType != typeof(string) || value == null || !(value is char))
{
return base.ConvertTo(context, culture, value, destinationType);
}
char c = (char)value;
if (c == '\0')
{
return string.Empty;
}
return new string(c, 1);
}
}
}