/
BaseNumberConverter.cs
120 lines (101 loc) · 4.44 KB
/
BaseNumberConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
namespace System.ComponentModel
{
/// <summary>
/// Provides a base type converter for integral types.
/// </summary>
public abstract class BaseNumberConverter : TypeConverter
{
internal BaseNumberConverter() { }
/// <summary>
/// Determines whether this editor will attempt to convert hex (0x or #) strings
/// </summary>
internal virtual bool AllowHex => true;
/// <summary>
/// The Type this converter is targeting (e.g. Int16, UInt32, etc.)
/// </summary>
internal abstract Type TargetType { get; }
/// <summary>
/// Convert the given value to a string using the given radix
/// </summary>
internal abstract object FromString(string value, int radix);
/// <summary>
/// Convert the given value to a string using the given formatInfo
/// </summary>
internal abstract object FromString(string value, NumberFormatInfo? formatInfo);
/// <summary>
/// Convert the given value from a string using the given formatInfo
/// </summary>
internal abstract string ToString(object value, NumberFormatInfo? formatInfo);
/// <summary>
/// Gets a value indicating whether this converter can convert an object in the
/// given source type to the TargetType object using the specified context.
/// </summary>
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
{
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
}
/// <summary>
/// Converts the given value object to an object of Type TargetType.
/// </summary>
public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
{
if (value is string text)
{
text = text.Trim();
try
{
if (AllowHex && text[0] == '#')
{
return FromString(text.Substring(1), 16);
}
else if (AllowHex && (text.StartsWith("0x", StringComparison.OrdinalIgnoreCase)
|| text.StartsWith("&h", StringComparison.OrdinalIgnoreCase)))
{
return FromString(text.Substring(2), 16);
}
else
{
culture ??= CultureInfo.CurrentCulture;
NumberFormatInfo? formatInfo = (NumberFormatInfo?)culture.GetFormat(typeof(NumberFormatInfo));
return FromString(text, formatInfo);
}
}
catch (Exception e)
{
throw new ArgumentException(SR.Format(SR.ConvertInvalidPrimitive, text, TargetType.Name), nameof(value), e);
}
}
return base.ConvertFrom(context, culture, value);
}
/// <summary>
/// Converts the given value object to the destination type.
/// </summary>
public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType)
{
ArgumentNullException.ThrowIfNull(destinationType);
if (destinationType == typeof(string) && value != null && TargetType.IsInstanceOfType(value))
{
culture ??= CultureInfo.CurrentCulture;
NumberFormatInfo? formatInfo = (NumberFormatInfo?)culture.GetFormat(typeof(NumberFormatInfo));
return ToString(value, formatInfo);
}
if (destinationType.IsPrimitive)
{
return Convert.ChangeType(value, destinationType, culture);
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType)
{
if (destinationType != null && destinationType.IsPrimitive)
{
return true;
}
return base.CanConvertTo(context, destinationType);
}
}
}