Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
SerializationInfo.cs
Go to the documentation of this file.
3
5
6public sealed class SerializationInfo
7{
8 private string[] _names;
9
10 private object[] _values;
11
12 private Type[] _types;
13
14 private int _count;
15
17
19
20 private string _rootTypeName;
21
22 private string _rootTypeAssemblyName;
23
24 private Type _rootType;
25
28
29 public string FullTypeName
30 {
31 get
32 {
33 return _rootTypeName;
34 }
35 set
36 {
37 if (value == null)
38 {
39 throw new ArgumentNullException("value");
40 }
43 }
44 }
45
46 public string AssemblyName
47 {
48 get
49 {
51 }
52 set
53 {
54 if (value == null)
55 {
56 throw new ArgumentNullException("value");
57 }
60 }
61 }
62
63 public bool IsFullTypeNameSetExplicit { get; private set; }
64
65 public bool IsAssemblyNameSetExplicit { get; private set; }
66
67 public int MemberCount => _count;
68
70
72
73
74 public static bool DeserializationInProgress
75 {
76 get
77 {
79 {
80 return true;
81 }
83 return threadDeserializationTracker.DeserializationInProgress;
84 }
85 }
86
87 [CLSCompliant(false)]
89 {
90 if ((object)type == null)
91 {
92 throw new ArgumentNullException("type");
93 }
94 if (converter == null)
95 {
96 throw new ArgumentNullException("converter");
97 }
99 _rootTypeName = type.FullName;
100 _rootTypeAssemblyName = type.Module.Assembly.FullName;
101 _names = new string[4];
102 _values = new object[4];
103 _types = new Type[4];
106 }
107
108 [CLSCompliant(false)]
113
114 public void SetType(Type type)
115 {
116 if ((object)type == null)
117 {
118 throw new ArgumentNullException("type");
119 }
120 if ((object)_rootType != type)
121 {
122 _rootType = type;
123 _rootTypeName = type.FullName;
124 _rootTypeAssemblyName = type.Module.Assembly.FullName;
127 }
128 }
129
134
135 private void ExpandArrays()
136 {
137 int num = _count * 2;
139 {
140 num = int.MaxValue;
141 }
142 string[] array = new string[num];
143 object[] array2 = new object[num];
144 Type[] array3 = new Type[num];
148 _names = array;
149 _values = array2;
150 _types = array3;
151 }
152
153 public void AddValue(string name, object? value, Type type)
154 {
155 if (name == null)
156 {
157 throw new ArgumentNullException("name");
158 }
159 if ((object)type == null)
160 {
161 throw new ArgumentNullException("type");
162 }
164 }
165
166 public void AddValue(string name, object? value)
167 {
168 if (value == null)
169 {
170 AddValue(name, value, typeof(object));
171 }
172 else
173 {
174 AddValue(name, value, value.GetType());
175 }
176 }
177
178 public void AddValue(string name, bool value)
179 {
180 AddValue(name, value, typeof(bool));
181 }
182
183 public void AddValue(string name, char value)
184 {
185 AddValue(name, value, typeof(char));
186 }
187
188 [CLSCompliant(false)]
189 public void AddValue(string name, sbyte value)
190 {
191 AddValue(name, value, typeof(sbyte));
192 }
193
194 public void AddValue(string name, byte value)
195 {
196 AddValue(name, value, typeof(byte));
197 }
198
199 public void AddValue(string name, short value)
200 {
201 AddValue(name, value, typeof(short));
202 }
203
204 [CLSCompliant(false)]
205 public void AddValue(string name, ushort value)
206 {
207 AddValue(name, value, typeof(ushort));
208 }
209
210 public void AddValue(string name, int value)
211 {
212 AddValue(name, value, typeof(int));
213 }
214
215 [CLSCompliant(false)]
216 public void AddValue(string name, uint value)
217 {
218 AddValue(name, value, typeof(uint));
219 }
220
221 public void AddValue(string name, long value)
222 {
223 AddValue(name, value, typeof(long));
224 }
225
226 [CLSCompliant(false)]
227 public void AddValue(string name, ulong value)
228 {
229 AddValue(name, value, typeof(ulong));
230 }
231
232 public void AddValue(string name, float value)
233 {
234 AddValue(name, value, typeof(float));
235 }
236
237 public void AddValue(string name, double value)
238 {
239 AddValue(name, value, typeof(double));
240 }
241
242 public void AddValue(string name, decimal value)
243 {
244 AddValue(name, value, typeof(decimal));
245 }
246
247 public void AddValue(string name, DateTime value)
248 {
249 AddValue(name, value, typeof(DateTime));
250 }
251
252 internal void AddValueInternal(string name, object value, Type type)
253 {
254 if (!_nameToIndex.TryAdd(name, _count))
255 {
257 }
258 if (_count >= _names.Length)
259 {
260 ExpandArrays();
261 }
262 _names[_count] = name;
264 _types[_count] = type;
265 _count++;
266 }
267
268 public void UpdateValue(string name, object value, Type type)
269 {
270 int num = FindElement(name);
271 if (num < 0)
272 {
274 return;
275 }
276 _values[num] = value;
277 _types[num] = type;
278 }
279
280 private int FindElement(string name)
281 {
282 if (name == null)
283 {
284 throw new ArgumentNullException("name");
285 }
287 {
288 return value;
289 }
290 return -1;
291 }
292
293 private object GetElement(string name, out Type foundType)
294 {
295 int num = FindElement(name);
296 if (num == -1)
297 {
299 }
300 foundType = _types[num];
301 return _values[num];
302 }
303
304 private object GetElementNoThrow(string name, out Type foundType)
305 {
306 int num = FindElement(name);
307 if (num == -1)
308 {
309 foundType = null;
310 return null;
311 }
312 foundType = _types[num];
313 return _values[num];
314 }
315
316 public object? GetValue(string name, Type type)
317 {
318 if ((object)type == null)
319 {
320 throw new ArgumentNullException("type");
321 }
322 if (!type.IsRuntimeImplemented())
323 {
325 }
327 object element = GetElement(name, out foundType);
328 if ((object)foundType == type || type.IsAssignableFrom(foundType) || element == null)
329 {
330 return element;
331 }
332 return _converter.Convert(element, type);
333 }
334
335 internal object GetValueNoThrow(string name, Type type)
336 {
339 if (elementNoThrow == null)
340 {
341 return null;
342 }
343 if ((object)foundType == type || type.IsAssignableFrom(foundType))
344 {
345 return elementNoThrow;
346 }
348 }
349
350 public bool GetBoolean(string name)
351 {
353 object element = GetElement(name, out foundType);
354 if ((object)foundType != typeof(bool))
355 {
356 return _converter.ToBoolean(element);
357 }
358 return (bool)element;
359 }
360
361 public char GetChar(string name)
362 {
364 object element = GetElement(name, out foundType);
365 if ((object)foundType != typeof(char))
366 {
367 return _converter.ToChar(element);
368 }
369 return (char)element;
370 }
371
372 [CLSCompliant(false)]
373 public sbyte GetSByte(string name)
374 {
376 object element = GetElement(name, out foundType);
377 if ((object)foundType != typeof(sbyte))
378 {
379 return _converter.ToSByte(element);
380 }
381 return (sbyte)element;
382 }
383
384 public byte GetByte(string name)
385 {
387 object element = GetElement(name, out foundType);
388 if ((object)foundType != typeof(byte))
389 {
390 return _converter.ToByte(element);
391 }
392 return (byte)element;
393 }
394
395 public short GetInt16(string name)
396 {
398 object element = GetElement(name, out foundType);
399 if ((object)foundType != typeof(short))
400 {
401 return _converter.ToInt16(element);
402 }
403 return (short)element;
404 }
405
406 [CLSCompliant(false)]
407 public ushort GetUInt16(string name)
408 {
410 object element = GetElement(name, out foundType);
411 if ((object)foundType != typeof(ushort))
412 {
413 return _converter.ToUInt16(element);
414 }
415 return (ushort)element;
416 }
417
418 public int GetInt32(string name)
419 {
421 object element = GetElement(name, out foundType);
422 if ((object)foundType != typeof(int))
423 {
424 return _converter.ToInt32(element);
425 }
426 return (int)element;
427 }
428
429 [CLSCompliant(false)]
430 public uint GetUInt32(string name)
431 {
433 object element = GetElement(name, out foundType);
434 if ((object)foundType != typeof(uint))
435 {
436 return _converter.ToUInt32(element);
437 }
438 return (uint)element;
439 }
440
441 public long GetInt64(string name)
442 {
444 object element = GetElement(name, out foundType);
445 if ((object)foundType != typeof(long))
446 {
447 return _converter.ToInt64(element);
448 }
449 return (long)element;
450 }
451
452 [CLSCompliant(false)]
453 public ulong GetUInt64(string name)
454 {
456 object element = GetElement(name, out foundType);
457 if ((object)foundType != typeof(ulong))
458 {
459 return _converter.ToUInt64(element);
460 }
461 return (ulong)element;
462 }
463
464 public float GetSingle(string name)
465 {
467 object element = GetElement(name, out foundType);
468 if ((object)foundType != typeof(float))
469 {
470 return _converter.ToSingle(element);
471 }
472 return (float)element;
473 }
474
475 public double GetDouble(string name)
476 {
478 object element = GetElement(name, out foundType);
479 if ((object)foundType != typeof(double))
480 {
481 return _converter.ToDouble(element);
482 }
483 return (double)element;
484 }
485
486 public decimal GetDecimal(string name)
487 {
489 object element = GetElement(name, out foundType);
490 if ((object)foundType != typeof(decimal))
491 {
492 return _converter.ToDecimal(element);
493 }
494 return (decimal)element;
495 }
496
497 public DateTime GetDateTime(string name)
498 {
500 object element = GetElement(name, out foundType);
501 if ((object)foundType != typeof(DateTime))
502 {
503 return _converter.ToDateTime(element);
504 }
505 return (DateTime)element;
506 }
507
508 public string? GetString(string name)
509 {
511 object element = GetElement(name, out foundType);
512 if ((object)foundType != typeof(string) && element != null)
513 {
514 return _converter.ToString(element);
515 }
516 return (string)element;
517 }
518
523
531
533 {
534 if (cachedValue == 0)
535 {
536 if (AppContext.TryGetSwitch("Switch.System.Runtime.Serialization.SerializationGuard." + switchSuffix, out var isEnabled) && isEnabled)
537 {
538 cachedValue = 1;
539 }
540 else
541 {
542 cachedValue = -1;
543 }
544 }
545 if (cachedValue != 1)
546 {
547 if (cachedValue != -1)
548 {
549 throw new ArgumentOutOfRangeException("cachedValue");
550 }
552 {
553 throw new SerializationException(SR.Format(SR.Serialization_DangerousDeserialization_Switch, "Switch.System.Runtime.Serialization.SerializationGuard." + switchSuffix));
554 }
555 }
556 }
557
578}
static bool TryGetSwitch(string switchName, out bool isEnabled)
Definition AppContext.cs:74
static unsafe void Copy(Array sourceArray, Array destinationArray, int length)
Definition Array.cs:624
bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
bool TryAdd(TKey key, TValue value)
object GetElementNoThrow(string name, out Type foundType)
object GetElement(string name, out Type foundType)
readonly Dictionary< string, int > _nameToIndex
void AddValue(string name, object? value, Type type)
static DeserializationToken StartDeserialization()
static DeserializationTracker GetThreadDeserializationTracker()
SerializationInfo(Type type, IFormatterConverter converter, bool requireSameTokenInPartialTrust)
void UpdateValue(string name, object value, Type type)
SerializationInfo(Type type, IFormatterConverter converter)
void AddValueInternal(string name, object value, Type type)
static void ThrowIfDeserializationInProgress(string switchSuffix, ref int cachedValue)
void AddValue(string name, DateTime value)
static DeserializationTracker t_deserializationTracker
static AsyncLocal< bool > AsyncDeserializationInProgress
static string Serialization_DangerousDeserialization
Definition SR.cs:2110
static string Serialization_DangerousDeserialization_Switch
Definition SR.cs:2112
static string Serialization_NotFound
Definition SR.cs:1898
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string Serialization_SameNameTwice
Definition SR.cs:1904
static string Argument_MustBeRuntimeType
Definition SR.cs:782
Definition SR.cs:7
object Convert(object value, Type type)