Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
BaseNumberConverter.cs
Go to the documentation of this file.
2
4
5public abstract class BaseNumberConverter : TypeConverter
6{
7 internal virtual bool AllowHex => true;
8
9 internal abstract Type TargetType { get; }
10
12 {
13 }
14
15 internal abstract object FromString(string value, int radix);
16
17 internal abstract object FromString(string value, NumberFormatInfo formatInfo);
18
19 internal abstract string ToString(object value, NumberFormatInfo formatInfo);
20
21 public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
22 {
23 if (!(sourceType == typeof(string)))
24 {
25 return base.CanConvertFrom(context, sourceType);
26 }
27 return true;
28 }
29
30 public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
31 {
32 if (value is string text)
33 {
34 string text2 = text.Trim();
35 try
36 {
37 if (AllowHex && text2[0] == '#')
38 {
39 return FromString(text2.Substring(1), 16);
40 }
41 if (AllowHex && (text2.StartsWith("0x", StringComparison.OrdinalIgnoreCase) || text2.StartsWith("&h", StringComparison.OrdinalIgnoreCase)))
42 {
43 return FromString(text2.Substring(2), 16);
44 }
45 if (culture == null)
46 {
48 }
50 return FromString(text2, formatInfo);
51 }
52 catch (Exception innerException)
53 {
54 throw new ArgumentException(System.SR.Format(System.SR.ConvertInvalidPrimitive, text2, TargetType.Name), "value", innerException);
55 }
56 }
57 return base.ConvertFrom(context, culture, value);
58 }
59
60 public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType)
61 {
62 if (destinationType == null)
63 {
64 throw new ArgumentNullException("destinationType");
65 }
66 if (destinationType == typeof(string) && value != null && TargetType.IsInstanceOfType(value))
67 {
68 if (culture == null)
69 {
71 }
73 return ToString(value, numberFormatInfo);
74 }
75 if (destinationType.IsPrimitive)
76 {
77 return Convert.ChangeType(value, destinationType, culture);
78 }
79 return base.ConvertTo(context, culture, value, destinationType);
80 }
81
82 public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
83 {
84 if (destinationType != null && destinationType.IsPrimitive)
85 {
86 return true;
87 }
88 return base.CanConvertTo(context, destinationType);
89 }
90}
override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
object FromString(string value, int radix)
object FromString(string value, NumberFormatInfo formatInfo)
override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
override? object ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType)
string ToString(object value, NumberFormatInfo formatInfo)
override? object ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
static ? object ChangeType(object? value, TypeCode typeCode)
Definition Convert.cs:229
static CultureInfo CurrentCulture
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string ConvertInvalidPrimitive
Definition SR.cs:20
Definition SR.cs:7
virtual bool IsInstanceOfType([NotNullWhen(true)] object? o)
Definition Type.cs:1020
bool IsPrimitive
Definition Type.cs:231