Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
XmlDataContract.cs
Go to the documentation of this file.
3using System.Linq;
8using System.Xml;
12
14
15internal sealed class XmlDataContract : DataContract
16{
18 {
20
22
24
26
28
29 private bool _hasRoot;
30
31 private CreateXmlSerializableDelegate _createXmlSerializable;
32
34
36 {
37 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
38 get
39 {
40 if (!_isKnownTypeAttributeChecked && base.UnderlyingType != null)
41 {
42 lock (this)
43 {
45 {
49 }
50 }
51 }
53 }
54 set
55 {
57 }
58 }
59
61 {
62 get
63 {
64 return _xsdType;
65 }
66 set
67 {
69 }
70 }
71
72 internal bool IsAnonymous => _xsdType != null;
73
74 internal override bool HasRoot
75 {
76 get
77 {
78 return _hasRoot;
79 }
80 set
81 {
83 }
84 }
85
87 {
88 get
89 {
91 }
92 set
93 {
95 }
96 }
97
99 {
100 get
101 {
103 }
104 set
105 {
107 }
108 }
109
111 {
112 get
113 {
115 }
116 set
117 {
119 }
120 }
121
122 internal CreateXmlSerializableDelegate CreateXmlSerializableDelegate
123 {
124 get
125 {
127 }
128 set
129 {
131 }
132 }
133
134 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
136 : base(type)
137 {
138 if (type.IsDefined(Globals.TypeOfDataContractAttribute, inherit: false))
139 {
141 }
143 {
145 }
150 base.Name = xmlDictionary.Add(base.StableName.Name);
151 base.Namespace = xmlDictionary.Add(base.StableName.Namespace);
152 object[] array = ((base.UnderlyingType == null) ? null : base.UnderlyingType.GetCustomAttributes(Globals.TypeOfXmlRootAttribute, inherit: false).ToArray());
153 if (array == null || array.Length == 0)
154 {
155 if (hasRoot)
156 {
158 _topLevelElementNamespace = ((base.StableName.Namespace == "http://www.w3.org/2001/XMLSchema") ? DictionaryGlobals.EmptyString : base.Namespace);
160 }
161 return;
162 }
163 if (hasRoot)
164 {
167 string elementName = xmlRootAttribute.ElementName;
168 _topLevelElementName = ((elementName == null || elementName.Length == 0) ? base.Name : xmlDictionary.Add(DataContract.EncodeLocalName(elementName)));
169 string @namespace = xmlRootAttribute.Namespace;
170 _topLevelElementNamespace = ((@namespace == null || @namespace.Length == 0) ? DictionaryGlobals.EmptyString : xmlDictionary.Add(@namespace));
171 return;
172 }
174 }
175 }
176
178
180 {
181 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
182 get
183 {
185 }
186 set
187 {
189 }
190 }
191
193 {
194 get
195 {
196 return _helper.XsdType;
197 }
198 set
199 {
201 }
202 }
203
205
206 public override bool HasRoot
207 {
208 get
209 {
210 return _helper.HasRoot;
211 }
212 set
213 {
215 }
216 }
217
219 {
220 get
221 {
223 }
224 set
225 {
227 }
228 }
229
231 {
232 get
233 {
235 }
236 set
237 {
239 }
240 }
241
243 {
244 get
245 {
247 }
248 set
249 {
251 }
252 }
253
254 internal CreateXmlSerializableDelegate CreateXmlSerializableDelegate
255 {
256 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
257 get
258 {
260 {
262 {
263 lock (this)
264 {
266 {
270 }
271 }
272 }
274 }
275 return () => ReflectionCreateXmlSerializable(base.UnderlyingType);
276 }
277 }
278
279 internal override bool CanContainReferences => false;
280
281 public override bool IsBuiltInDataContract
282 {
283 get
284 {
285 if (!(base.UnderlyingType == Globals.TypeOfXmlElement))
286 {
288 }
289 return true;
290 }
291 }
292
293 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
299
301 {
302 if (base.UnderlyingType.IsValueType)
303 {
304 return null;
305 }
306 ConstructorInfo constructor = base.UnderlyingType.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, Type.EmptyTypes);
307 if (constructor == null)
308 {
310 }
311 return constructor;
312 }
313
314 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
315 internal CreateXmlSerializableDelegate GenerateCreateXmlSerializableDelegate()
316 {
317 Type underlyingType = base.UnderlyingType;
319 bool flag = RequiresMemberAccessForCreate(null) && !(underlyingType.FullName == "System.Xml.Linq.XElement");
320 try
321 {
322 codeGenerator.BeginMethod("Create" + DataContract.GetClrTypeFullName(underlyingType), typeof(CreateXmlSerializableDelegate), flag);
323 }
325 {
326 if (!flag)
327 {
328 throw;
329 }
331 }
332 if (underlyingType.IsValueType)
333 {
338 }
339 else
340 {
342 if (!constructorInfo.IsPublic && underlyingType.FullName == "System.Xml.Linq.XElement")
343 {
344 Type type = underlyingType.Assembly.GetType("System.Xml.Linq.XName");
345 if (type != null)
346 {
347 MethodInfo method = type.GetMethod("op_Implicit", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, new Type[1] { typeof(string) });
348 ConstructorInfo constructor = underlyingType.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, new Type[1] { type });
349 if (method != null && constructor != null)
350 {
351 codeGenerator.Ldstr("default");
352 codeGenerator.Call(method);
354 }
355 }
356 }
358 }
359 codeGenerator.ConvertValue(base.UnderlyingType, Globals.TypeOfIXmlSerializable);
360 codeGenerator.Ret();
361 return (CreateXmlSerializableDelegate)codeGenerator.EndMethod();
362 }
363
384
386 {
387 if (type.IsValueType)
388 {
389 throw new NotImplementedException("ReflectionCreateXmlSerializable - value type");
390 }
391 object obj = null;
392 if (type == typeof(XElement))
393 {
394 obj = new XElement("default");
395 }
396 else
397 {
399 obj = constructor.Invoke(Array.Empty<object>());
400 }
401 return (IXmlSerializable)obj;
402 }
403
404 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
406 {
407 if (context == null)
408 {
410 }
411 else
412 {
414 }
415 }
416
417 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
419 {
420 object obj;
421 if (context == null)
422 {
424 }
425 else
426 {
427 obj = context.ReadIXmlSerializable(xmlReader, this, isMemberType: true);
428 context.AddNewObject(obj);
429 }
430 xmlReader.ReadEndElement();
431 return obj;
432 }
433}
void Add(TKey key, TValue value)
static string EncodeLocalName(string localName)
static Dictionary< XmlQualifiedName, DataContract > ImportKnownTypeAttributes(Type type)
static string GetClrTypeFullName(Type type)
static bool ConstructorRequiresMemberAccess(ConstructorInfo ctor)
static readonly XmlDictionaryString EmptyString
static Type TypeOfCollectionDataContractAttribute
Definition Globals.cs:729
static void GetXmlTypeInfo(Type type, out XmlQualifiedName stableName, out XmlSchemaType xsdType, out bool hasRoot)
override Dictionary< XmlQualifiedName, DataContract > KnownDataContracts
XmlDataContractCriticalHelper([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors|DynamicallyAccessedMemberTypes.NonPublicConstructors|DynamicallyAccessedMemberTypes.PublicMethods|DynamicallyAccessedMemberTypes.NonPublicMethods|DynamicallyAccessedMemberTypes.PublicFields|DynamicallyAccessedMemberTypes.PublicProperties)] Type type)
IXmlSerializable ReflectionCreateXmlSerializable(Type type)
bool RequiresMemberAccessForCreate(SecurityException securityException)
readonly XmlDataContractCriticalHelper _helper
override void WriteXmlValue(XmlWriterDelegator xmlWriter, object obj, XmlObjectSerializerWriteContext context)
override object ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)
CreateXmlSerializableDelegate GenerateCreateXmlSerializableDelegate()
object ReadIXmlSerializable(XmlReaderDelegator xmlReader, XmlDataContract xmlDataContract, bool isMemberType)
static object ReadRootIXmlSerializable(XmlReaderDelegator xmlReader, XmlDataContract xmlDataContract, bool isMemberType)
static void WriteRootIXmlSerializable(XmlWriterDelegator xmlWriter, object obj)
static string IXmlSerializableCannotHaveDataContract
Definition SR.cs:182
static string IsAnyCannotHaveXmlRoot
Definition SR.cs:174
static string PartialTrustIXmlSerializableTypeNotPublic
Definition SR.cs:254
static string IXmlSerializableMustHaveDefaultConstructor
Definition SR.cs:188
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string PartialTrustIXmlSerialzableNoPublicConstructor
Definition SR.cs:256
static string IXmlSerializableCannotHaveCollectionDataContract
Definition SR.cs:180
Definition SR.cs:7
static readonly Type[] EmptyTypes
Definition Type.cs:19