Terraria v1.4.4.9
Terraria source code documentation
All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events Macros
RangeAttribute.cs
Go to the documentation of this file.
3
5
6[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
8{
9 public object Minimum { get; private set; }
10
11 public object Maximum { get; private set; }
12
13 [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
14 public Type OperandType { get; }
15
16 public bool ParseLimitsInInvariantCulture { get; set; }
17
18 public bool ConvertValueInInvariantCulture { get; set; }
19
20 private Func<object, object?>? Conversion { get; set; }
21
22 public RangeAttribute(int minimum, int maximum)
23 : base(() => System.SR.RangeAttribute_ValidationError)
24 {
25 Minimum = minimum;
26 Maximum = maximum;
27 OperandType = typeof(int);
28 }
29
30 public RangeAttribute(double minimum, double maximum)
31 : base(() => System.SR.RangeAttribute_ValidationError)
32 {
33 Minimum = minimum;
34 Maximum = maximum;
35 OperandType = typeof(double);
36 }
37
38 [RequiresUnreferencedCode("Generic TypeConverters may require the generic types to be annotated. For example, NullableConverter requires the underlying type to be DynamicallyAccessedMembers All.")]
39 public RangeAttribute([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type type, string minimum, string maximum)
40 : base(() => System.SR.RangeAttribute_ValidationError)
41 {
43 Minimum = minimum;
44 Maximum = maximum;
45 }
46
47 private void Initialize(IComparable minimum, IComparable maximum, Func<object, object> conversion)
48 {
49 if (minimum.CompareTo(maximum) > 0)
50 {
52 }
53 Minimum = minimum;
54 Maximum = maximum;
55 Conversion = conversion;
56 }
57
58 public override bool IsValid(object? value)
59 {
61 if (value != null)
62 {
63 string obj = value as string;
64 if (obj == null || obj.Length != 0)
65 {
66 object obj2;
67 try
68 {
69 obj2 = Conversion(value);
70 }
71 catch (FormatException)
72 {
73 return false;
74 }
76 {
77 return false;
78 }
80 {
81 return false;
82 }
84 IComparable comparable2 = (IComparable)Maximum;
85 if (comparable.CompareTo(obj2) <= 0)
86 {
87 return comparable2.CompareTo(obj2) >= 0;
88 }
89 return false;
90 }
91 }
92 return true;
93 }
94
95 public override string FormatErrorMessage(string name)
96 {
98 return string.Format(CultureInfo.CurrentCulture, base.ErrorMessageString, name, Minimum, Maximum);
99 }
100
101 private void SetupConversion()
102 {
103 if (Conversion != null)
104 {
105 return;
106 }
107 object minimum = Minimum;
108 object maximum = Maximum;
109 if (minimum == null || maximum == null)
110 {
112 }
113 Type type2 = minimum.GetType();
114 if (type2 == typeof(int))
115 {
116 Initialize((int)minimum, (int)maximum, (object v) => Convert.ToInt32(v, CultureInfo.InvariantCulture));
117 return;
118 }
119 if (type2 == typeof(double))
120 {
121 Initialize((double)minimum, (double)maximum, (object v) => Convert.ToDouble(v, CultureInfo.InvariantCulture));
122 return;
123 }
125 if (type == null)
126 {
128 }
129 Type typeFromHandle = typeof(IComparable);
130 if (!typeFromHandle.IsAssignableFrom(type))
131 {
133 }
135 IComparable minimum2 = (IComparable)(ParseLimitsInInvariantCulture ? converter.ConvertFromInvariantString((string)minimum) : converter.ConvertFromString((string)minimum));
136 IComparable maximum2 = (IComparable)(ParseLimitsInInvariantCulture ? converter.ConvertFromInvariantString((string)maximum) : converter.ConvertFromString((string)maximum));
137 Func<object, object> conversion = ((!ConvertValueInInvariantCulture) ? ((Func<object, object>)((object value) => (!(value.GetType() == type)) ? converter.ConvertFrom(value) : value)) : ((Func<object, object>)((object value) => (!(value.GetType() == type)) ? converter.ConvertFrom(null, CultureInfo.InvariantCulture, value) : value)));
138 Initialize(minimum2, maximum2, conversion);
139 }
140
141 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "The ctor that allows this code to be called is marked with RequiresUnreferencedCode.")]
146}
RangeAttribute([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type type, string minimum, string maximum)
void Initialize(IComparable minimum, IComparable maximum, Func< object, object > conversion)
static TypeConverter GetConverter(object component)
static int ToInt32(object? value)
Definition Convert.cs:1320
static double ToDouble(object? value)
Definition Convert.cs:1991
static CultureInfo CurrentCulture
static CultureInfo InvariantCulture
static string RangeAttribute_MinGreaterThanMax
Definition SR.cs:74
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string RangeAttribute_Must_Set_Min_And_Max
Definition SR.cs:76
static string RangeAttribute_ArbitraryTypeNotIComparable
Definition SR.cs:72
static string RangeAttribute_Must_Set_Operand_Type
Definition SR.cs:78
Definition SR.cs:7
virtual bool IsAssignableFrom([NotNullWhen(true)] Type? c)
Definition Type.cs:1561
string? FullName
Definition Type.cs:47
int CompareTo(object? obj)