summaryrefslogtreecommitdiff
path: root/mcs/class/System.Runtime.Serialization/ReferenceSources/XmlDataContract_static.cs
blob: c8829ad53ff9185d3607b1708593d630d9b675e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Xml;
using System.Xml.Serialization;

namespace System.Runtime.Serialization
{
	internal partial class XmlDataContract
	{
        internal CreateXmlSerializableDelegate GenerateCreateXmlSerializableDelegate()
        {
				return () => new XmlDataContractInterpreter (this).CreateXmlSerializable ();
		}
	}
	
	internal class XmlDataContractInterpreter
	{
		XmlDataContract contract;
		
		public XmlDataContractInterpreter (XmlDataContract contract)
		{
			this.contract = contract;
		}
		
		public IXmlSerializable CreateXmlSerializable ()
		{
			Type type = contract.UnderlyingType;
			object value = null;
			if (type.IsValueType)
				value = FormatterServices.GetUninitializedObject (type);
			else
				value = GetConstructor ().Invoke (new object [0]);
			return (IXmlSerializable) value;
		}

		ConstructorInfo GetConstructor ()
		{
			Type type = contract.UnderlyingType;

			if (type.IsValueType)
				return null;

			ConstructorInfo ctor = type.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, null, Globals.EmptyTypeArray, null);
			if (ctor == null)
				throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.GetString(SR.IXmlSerializableMustHaveDefaultConstructor, DataContract.GetClrTypeFullName(type))));

			return ctor;
		}
	}
}