ReflectionHelper.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // System.Xml.Serialization.ReflectionHelper
  3. //
  4. // Author:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // Copyright (C) 2003 Ximian, Inc.
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.Reflection;
  30. using System.Collections;
  31. namespace System.Xml.Serialization
  32. {
  33. internal class ReflectionHelper
  34. {
  35. Hashtable _clrTypes = new Hashtable ();
  36. Hashtable _schemaTypes = new Hashtable ();
  37. public void RegisterSchemaType (XmlTypeMapping map, string xmlType, string ns)
  38. {
  39. string mapKey = xmlType + "/" + ns;
  40. if (!_schemaTypes.ContainsKey (mapKey))
  41. _schemaTypes.Add (mapKey, map);
  42. }
  43. public XmlTypeMapping GetRegisteredSchemaType (string xmlType, string ns)
  44. {
  45. string mapKey = xmlType + "/" + ns;
  46. return _schemaTypes[mapKey] as XmlTypeMapping;
  47. }
  48. public void RegisterClrType (XmlTypeMapping map, Type type, string ns)
  49. {
  50. if (type == typeof(object)) ns = "";
  51. string mapKey = type.FullName + "/" + ns;
  52. if (!_clrTypes.ContainsKey (mapKey))
  53. _clrTypes.Add (mapKey, map);
  54. }
  55. public XmlTypeMapping GetRegisteredClrType (Type type, string ns)
  56. {
  57. if (type == typeof(object)) ns = "";
  58. string mapKey = type.FullName + "/" + ns;
  59. return _clrTypes[mapKey] as XmlTypeMapping;
  60. }
  61. public Exception CreateError (XmlTypeMapping map, string message)
  62. {
  63. return new InvalidOperationException ("There was an error reflecting '" + map.TypeFullName + "': " + message);
  64. }
  65. public static void CheckSerializableType (Type type, bool allowPrivateConstructors)
  66. {
  67. if (type.IsArray) return;
  68. if (!allowPrivateConstructors && type.GetConstructor (Type.EmptyTypes) == null && !type.IsAbstract && !type.IsValueType)
  69. throw new InvalidOperationException (type.FullName + " cannot be serialized because it does not have a default public constructor");
  70. if (type.IsInterface && !TypeTranslator.GetTypeData (type).IsListType)
  71. throw new InvalidOperationException (type.FullName + " cannot be serialized because it is an interface");
  72. Type t = type;
  73. Type oldt = null;
  74. do {
  75. if (!t.IsPublic && !t.IsNestedPublic)
  76. throw new InvalidOperationException (type.FullName + " is inaccessible due to its protection level. Only public types can be processed");
  77. oldt = t;
  78. t = t.DeclaringType;
  79. }
  80. while (t != null && t != oldt);
  81. }
  82. public static string BuildMapKey (Type type)
  83. {
  84. return type.FullName + "::";
  85. }
  86. public static string BuildMapKey (MethodInfo method, string tag)
  87. {
  88. string res = method.DeclaringType.FullName + ":" + method.ReturnType.FullName + " " + method.Name + "(";
  89. ParameterInfo[] pars = method.GetParameters ();
  90. for (int n=0; n<pars.Length; n++)
  91. {
  92. if (n > 0) res += ", ";
  93. res += pars[n].ParameterType.FullName;
  94. }
  95. res += ")";
  96. if (tag != null)
  97. res += ":" + tag;
  98. return res;
  99. }
  100. }
  101. }