2
0

XmlReflectionImporter.cs 3.8 KB

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