XmlDataContract_static.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Xml;
  6. using System.Xml.Serialization;
  7. namespace System.Runtime.Serialization
  8. {
  9. internal partial class XmlDataContract
  10. {
  11. internal CreateXmlSerializableDelegate GenerateCreateXmlSerializableDelegate()
  12. {
  13. return () => new XmlDataContractInterpreter (this).CreateXmlSerializable ();
  14. }
  15. }
  16. internal class XmlDataContractInterpreter
  17. {
  18. XmlDataContract contract;
  19. public XmlDataContractInterpreter (XmlDataContract contract)
  20. {
  21. this.contract = contract;
  22. }
  23. public IXmlSerializable CreateXmlSerializable ()
  24. {
  25. Type type = contract.UnderlyingType;
  26. object value = null;
  27. if (type.IsValueType)
  28. value = FormatterServices.GetUninitializedObject (type);
  29. else
  30. value = GetConstructor ().Invoke (new object [0]);
  31. return (IXmlSerializable) value;
  32. }
  33. ConstructorInfo GetConstructor ()
  34. {
  35. Type type = contract.UnderlyingType;
  36. if (type.IsValueType)
  37. return null;
  38. ConstructorInfo ctor = type.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, null, Globals.EmptyTypeArray, null);
  39. if (ctor == null)
  40. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.GetString(SR.IXmlSerializableMustHaveDefaultConstructor, DataContract.GetClrTypeFullName(type))));
  41. return ctor;
  42. }
  43. }
  44. }