[S.R.Serialization] implement XmlDataContract's interpreter method.
authorAtsushi Eno <atsushieno@gmail.com>
Tue, 14 Apr 2015 13:00:33 +0000 (22:00 +0900)
committerAtsushi Eno <atsushieno@gmail.com>
Fri, 24 Apr 2015 05:36:54 +0000 (14:36 +0900)
mcs/class/System.Runtime.Serialization/ReferenceSources/XmlDataContract_static.cs

index 6ff8070ca74610f29ef043278448eaaaf0bde76f..c8829ad53ff9185d3607b1708593d630d9b675e6 100644 (file)
@@ -3,6 +3,7 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Reflection;
 using System.Xml;
+using System.Xml.Serialization;
 
 namespace System.Runtime.Serialization
 {
@@ -10,7 +11,42 @@ namespace System.Runtime.Serialization
        {
         internal CreateXmlSerializableDelegate GenerateCreateXmlSerializableDelegate()
         {
-                       throw new NotImplementedException ();
+                               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;
                }
        }
 }