Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
TypeConverter.cs
Go to the documentation of this file.
5
7
8public class TypeConverter
9{
11 {
12 public override Type ComponentType { get; }
13
14 public override bool IsReadOnly
15 {
16 [DynamicDependency(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor | DynamicallyAccessedMemberTypes.PublicFields, typeof(ReadOnlyAttribute))]
17 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "The DynamicDependency ensures the correct members are preserved.")]
18 get
19 {
21 }
22 }
23
24 public override Type PropertyType { get; }
25
26 protected SimplePropertyDescriptor(Type componentType, string name, Type propertyType)
27 : this(componentType, name, propertyType, Array.Empty<Attribute>())
28 {
29 }
30
31 protected SimplePropertyDescriptor(Type componentType, string name, Type propertyType, Attribute[]? attributes)
32 : base(name, attributes)
33 {
34 ComponentType = componentType;
35 PropertyType = propertyType;
36 }
37
38 public override bool CanResetValue(object component)
39 {
40 return ((DefaultValueAttribute)Attributes[typeof(DefaultValueAttribute)])?.Value.Equals(GetValue(component)) ?? false;
41 }
42
43 public override void ResetValue(object component)
44 {
46 if (defaultValueAttribute != null)
47 {
48 SetValue(component, defaultValueAttribute.Value);
49 }
50 }
51
52 public override bool ShouldSerializeValue(object component)
53 {
54 return false;
55 }
56 }
57
59 {
60 private readonly ICollection _values;
61
63
64 public int Count
65 {
66 get
67 {
68 if (_valueArray != null)
69 {
70 return _valueArray.Length;
71 }
72 return _values.Count;
73 }
74 }
75
76 public object? this[int index]
77 {
78 get
79 {
80 if (_valueArray != null)
81 {
83 }
84 if (_values is IList list)
85 {
86 return list[index];
87 }
88 _valueArray = new object[_values.Count];
91 }
92 }
93
94 bool ICollection.IsSynchronized => false;
95
96 object ICollection.SyncRoot => null;
97
99 {
100 if (values == null)
101 {
102 values = Array.Empty<object>();
103 }
104 if (values is Array valueArray)
105 {
106 _valueArray = valueArray;
107 }
108 _values = values;
109 }
110
111 public void CopyTo(Array array, int index)
112 {
114 }
115
117 {
118 return _values.GetEnumerator();
119 }
120 }
121
122 public bool CanConvertFrom(Type sourceType)
123 {
124 return CanConvertFrom(null, sourceType);
125 }
126
127 public virtual bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
128 {
129 return sourceType == typeof(InstanceDescriptor);
130 }
131
132 public bool CanConvertTo(Type destinationType)
133 {
134 return CanConvertTo(null, destinationType);
135 }
136
137 public virtual bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
138 {
139 return destinationType == typeof(string);
140 }
141
142 public object? ConvertFrom(object value)
143 {
145 }
146
147 public virtual object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
148 {
149 if (value is InstanceDescriptor instanceDescriptor)
150 {
151 return instanceDescriptor.Invoke();
152 }
154 }
155
156 public object? ConvertFromInvariantString(string text)
157 {
159 }
160
162 {
164 }
165
166 public object? ConvertFromString(string text)
167 {
168 return ConvertFrom(null, null, text);
169 }
170
171 public object? ConvertFromString(ITypeDescriptorContext? context, string text)
172 {
173 return ConvertFrom(context, CultureInfo.CurrentCulture, text);
174 }
175
177 {
178 return ConvertFrom(context, culture, text);
179 }
180
181 public object? ConvertTo(object? value, Type destinationType)
182 {
183 return ConvertTo(null, null, value, destinationType);
184 }
185
186 public virtual object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType)
187 {
188 if (destinationType == null)
189 {
190 throw new ArgumentNullException("destinationType");
191 }
192 if (destinationType == typeof(string))
193 {
194 if (value == null)
195 {
196 return string.Empty;
197 }
198 if (culture != null && culture != CultureInfo.CurrentCulture && value is IFormattable formattable)
199 {
200 return formattable.ToString(null, culture);
201 }
202 return value.ToString();
203 }
204 throw GetConvertToException(value, destinationType);
205 }
206
207 public string? ConvertToInvariantString(object? value)
208 {
210 }
211
212 public string? ConvertToInvariantString(ITypeDescriptorContext? context, object? value)
213 {
215 }
216
217 public string? ConvertToString(object? value)
218 {
219 return (string)ConvertTo(null, CultureInfo.CurrentCulture, value, typeof(string));
220 }
221
222 public string? ConvertToString(ITypeDescriptorContext? context, object? value)
223 {
224 return (string)ConvertTo(context, CultureInfo.CurrentCulture, value, typeof(string));
225 }
226
228 {
229 return (string)ConvertTo(context, culture, value, typeof(string));
230 }
231
232 public object? CreateInstance(IDictionary propertyValues)
233 {
234 return CreateInstance(null, propertyValues);
235 }
236
237 public virtual object? CreateInstance(ITypeDescriptorContext? context, IDictionary propertyValues)
238 {
239 return null;
240 }
241
243 {
244 string p = ((value == null) ? System.SR.Null : value.GetType().FullName);
246 }
247
248 protected Exception GetConvertToException(object? value, Type destinationType)
249 {
250 string p = ((value == null) ? System.SR.Null : value.GetType().FullName);
251 throw new NotSupportedException(System.SR.Format(System.SR.ConvertToException, GetType().Name, p, destinationType.FullName));
252 }
253
255 {
256 return GetCreateInstanceSupported(null);
257 }
258
260 {
261 return false;
262 }
263
264 [RequiresUnreferencedCode("The Type of value cannot be statically discovered.")]
266 {
267 return GetProperties(null, value);
268 }
269
270 [RequiresUnreferencedCode("The Type of value cannot be statically discovered.")]
271 [DynamicDependency(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor | DynamicallyAccessedMemberTypes.PublicFields, typeof(BrowsableAttribute))]
273 {
274 return GetProperties(context, value, new Attribute[1] { BrowsableAttribute.Yes });
275 }
276
277 [RequiresUnreferencedCode("The Type of value cannot be statically discovered. The public parameterless constructor or the 'Default' static field may be trimmed from the Attribute's Type.")]
278 public virtual PropertyDescriptorCollection? GetProperties(ITypeDescriptorContext? context, object value, Attribute[]? attributes)
279 {
280 return null;
281 }
282
284 {
285 return GetPropertiesSupported(null);
286 }
287
288 public virtual bool GetPropertiesSupported(ITypeDescriptorContext? context)
289 {
290 return false;
291 }
292
294 {
295 return GetStandardValues(null);
296 }
297
299 {
300 return null;
301 }
302
304 {
305 return GetStandardValuesExclusive(null);
306 }
307
309 {
310 return false;
311 }
312
314 {
315 return GetStandardValuesSupported(null);
316 }
317
319 {
320 return false;
321 }
322
323 public bool IsValid(object value)
324 {
325 return IsValid(null, value);
326 }
327
328 public virtual bool IsValid(ITypeDescriptorContext? context, object value)
329 {
330 bool result = true;
331 try
332 {
333 if (value == null || CanConvertFrom(context, value.GetType()))
334 {
336 }
337 else
338 {
339 result = false;
340 }
341 }
342 catch
343 {
344 result = false;
345 }
346 return result;
347 }
348
350 {
351 props.Sort(names);
352 return props;
353 }
354}
object? GetValue(params int[] indices)
Definition Array.cs:980
int Length
Definition Array.cs:430
virtual AttributeCollection Attributes
void SetValue(object? component, object? value)
object? GetValue(object? component)
static readonly ReadOnlyAttribute Yes
SimplePropertyDescriptor(Type componentType, string name, Type propertyType)
SimplePropertyDescriptor(Type componentType, string name, Type propertyType, Attribute[]? attributes)
PropertyDescriptorCollection SortProperties(PropertyDescriptorCollection props, string[] names)
PropertyDescriptorCollection? GetProperties(ITypeDescriptorContext? context, object value)
virtual ? object CreateInstance(ITypeDescriptorContext? context, IDictionary propertyValues)
object? CreateInstance(IDictionary propertyValues)
string? ConvertToString(ITypeDescriptorContext? context, object? value)
virtual ? object ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
string? ConvertToString(object? value)
bool CanConvertTo(Type destinationType)
object? ConvertFromString(ITypeDescriptorContext? context, string text)
string? ConvertToString(ITypeDescriptorContext? context, CultureInfo? culture, object? value)
string? ConvertToInvariantString(ITypeDescriptorContext? context, object? value)
virtual bool IsValid(ITypeDescriptorContext? context, object value)
object? ConvertFromInvariantString(string text)
virtual bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
virtual ? StandardValuesCollection GetStandardValues(ITypeDescriptorContext? context)
virtual ? PropertyDescriptorCollection GetProperties(ITypeDescriptorContext? context, object value, Attribute[]? attributes)
virtual bool GetPropertiesSupported(ITypeDescriptorContext? context)
virtual bool GetStandardValuesSupported(ITypeDescriptorContext? context)
object? ConvertFromInvariantString(ITypeDescriptorContext? context, string text)
virtual bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
bool CanConvertFrom(Type sourceType)
virtual ? object ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType)
PropertyDescriptorCollection? GetProperties(object value)
virtual bool GetStandardValuesExclusive(ITypeDescriptorContext? context)
object? ConvertTo(object? value, Type destinationType)
string? ConvertToInvariantString(object? value)
virtual bool GetCreateInstanceSupported(ITypeDescriptorContext? context)
Exception GetConvertToException(object? value, Type destinationType)
object? ConvertFromString(string text)
object? ConvertFromString(ITypeDescriptorContext? context, CultureInfo? culture, string text)
Exception GetConvertFromException(object? value)
static CultureInfo CurrentCulture
static CultureInfo InvariantCulture
static string ConvertToException
Definition SR.cs:22
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string ConvertFromException
Definition SR.cs:18
static string Null
Definition SR.cs:40
Definition SR.cs:7
string? FullName
Definition Type.cs:47
void CopyTo(Array array, int index)