XmlReflectionImporter.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // System.Xml.Serialization.XmlReflectionImporter
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. //
  7. // Copyright (C) Tim Coleman, 2002
  8. //
  9. using System.Reflection;
  10. namespace System.Xml.Serialization {
  11. public class XmlReflectionImporter {
  12. string defaultNamespace;
  13. XmlAttributeOverrides attributeOverrides;
  14. #region Constructors
  15. public XmlReflectionImporter ()
  16. : this (null, null)
  17. {
  18. }
  19. public XmlReflectionImporter (string defaultNamespace)
  20. : this (null, defaultNamespace)
  21. {
  22. }
  23. public XmlReflectionImporter (XmlAttributeOverrides attributeOverrides)
  24. : this (attributeOverrides, null)
  25. {
  26. }
  27. public XmlReflectionImporter (XmlAttributeOverrides attributeOverrides, string defaultNamespace)
  28. {
  29. if (defaultNamespace == null)
  30. this.defaultNamespace = String.Empty;
  31. else
  32. this.defaultNamespace = defaultNamespace;
  33. if (attributeOverrides == null)
  34. this.attributeOverrides = new XmlAttributeOverrides();
  35. else
  36. this.attributeOverrides = attributeOverrides;
  37. }
  38. #endregion // Constructors
  39. #region Methods
  40. [MonoTODO]
  41. public XmlMembersMapping ImportMembersMapping (string elementName,
  42. string ns,
  43. XmlReflectionMember [] members,
  44. bool hasWrapperElement)
  45. {
  46. throw new NotImplementedException ();
  47. }
  48. public XmlTypeMapping ImportTypeMapping (Type type)
  49. {
  50. return ImportTypeMapping (type, null, null);
  51. }
  52. public XmlTypeMapping ImportTypeMapping (Type type, string defaultNamespace)
  53. {
  54. return ImportTypeMapping (type, null, defaultNamespace);
  55. }
  56. public XmlTypeMapping ImportTypeMapping (Type type, XmlRootAttribute group)
  57. {
  58. return ImportTypeMapping (type, group, null);
  59. }
  60. public XmlTypeMapping ImportTypeMapping (Type type, XmlRootAttribute group, string defaultNamespace)
  61. {
  62. if (type == null)
  63. throw new ArgumentNullException ("type");
  64. if (type == typeof (void))
  65. throw new InvalidOperationException ("Type " + type.Name +
  66. " may not be serialized.");
  67. XmlAttributes atts = new XmlAttributes (type);
  68. TypeData data = TypeTranslator.GetTypeData (type);
  69. string elementName = data.ElementName;
  70. string typeName = data.TypeName;
  71. string typeFullName = data.FullTypeName;
  72. string nameSpc = (defaultNamespace != null) ? defaultNamespace : this.defaultNamespace;
  73. return new XmlTypeMapping (elementName, nameSpc, typeFullName, typeName);
  74. }
  75. private void ImportTypeMapping (TypeData data, string ns)
  76. {
  77. ImportTypeMapping (data.Type, null, ns);
  78. }
  79. public void IncludeType (Type type)
  80. {
  81. if (type == null)
  82. throw new ArgumentNullException ("type");
  83. TypeData data = TypeTranslator.GetTypeData (type);
  84. ImportTypeMapping (data, defaultNamespace);
  85. }
  86. public void IncludeTypes (ICustomAttributeProvider provider)
  87. {
  88. if (provider == null)
  89. throw new ArgumentNullException ("provider");
  90. Type ixml = typeof (IXmlSerializable);
  91. object [] customAttrs = provider.GetCustomAttributes (typeof (XmlIncludeAttribute), false);
  92. foreach (XmlIncludeAttribute att in customAttrs) {
  93. Type type = att.Type;
  94. if (ixml.IsAssignableFrom (type)) {
  95. string fmt = "Type {0} is derived from {1} and therefore cannot " +
  96. "be used with attribute XmlInclude";
  97. throw new InvalidOperationException (String.Format (fmt, type, ixml));
  98. }
  99. IncludeType (type);
  100. }
  101. }
  102. #endregion // Methods
  103. }
  104. }